SUBTASKS 1.1 "before all" hook for ":1 "worldcup" database should exist"

Finally! I’ve got it!
Everything was in place, insert_data and queries files. After start a new session in this afternoon, waited the CodeRoad appear, then clicked in the RUN button first. It failed.
Then I opened the terminal and executed the insert_data script, wait a litle bit and clicked in the RUN button again. This time it passed! :grinning:

2 Likes

This is so weird. In my case, I tried a gazillion times to re-run the script, comment out certain lines (that seemed to trigger the error, although the code was ok) and when it eventually worked, it seemed random.

Is there any solution on this?, i literally tried to create the database manually 5-6 times and then when i tried to run the insert_data.sh file it says " [SUBTASKS 1.1 “before all” hook for “:1 “worldcup” database should exist”]"…
Somebody help me get through this :slight_smile:

Same. Blocked here. The time limit seems unreasonable since no matter how I tried it always failed.

Hi Everyone, di anyone found the solution to this issue, can’t pass all the tests it fails with
" [SUBTASKS 1.1 “before all” hook for “:1 “worldcup” database should exist”]"

I seem to be having the same issue, has anyone found a reliable fix to this? I’ve been refreshing/restarting all day.

Did you start this project a while ago and are coming back to it today?
Can you connect to your database and view its contents ok?

If yes to both of these questions, I’d recommend creating a dump of your database and then saving both the resulting sql file and your sh file(s) locally, then deleting the project container on CodeAlly and starting completely afresh.

You can then rebuild your database from the sql file and put your sh file(s) back and everything should work as intended.

Hi @igorgetmeabrain - I’ve tried all of that with no luck. I’ve even tried with another other user’s code who said it was able to pass after I cleared everything out of my container.
Link to the comment with the repo that I’ve tried with no success

EDIT: I’ve tried multiple user repo’s and I’m getting the same error every time: “SUBTASKS 1.1 “before all” hook for “:1 “worldcup” database should exist””

I am having the same issue and error unfortunately. I’ve tried the reseting the container and rebuilding the database from scratch and everything like I’ve seen some posts online here suggest but it’s still to no avail.

What I’ve noticed is that clearing out insert_data.sh causes the tests to all work and pass (but of course only the ones relating to insert data fail) and I’m getting the same output failed test log as seen in the picture in the original post. It does take quite a long time (took about 47 seconds alone) when I run the insert data script in the terminal so it definitely seems to me that it’s an issue of the 20 second limit, which was mentioned in the instructions.

The thing is, I’m not sure how much tighter I can make my code, so if there are any suggestions there I’ll greatly appreciate that and include my code below. The most I can get for the tests to not show that ‘database should exist’ error is the first two SELECT statements. Anything I add after gives that error. Sadly I kinda need those INSERT statements for the teams and games.

# Truncate all tables
echo $($PSQL "TRUNCATE TABLE teams, games")

cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
  # Skip first row
  if [[ $YEAR != "year" ]]
  then
    # Retrieve team_id for winner and opponent
    TEAM_ID_W=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
    TEAM_ID_O=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")

    # Add winner team to teams table and update variable if it does not already exist
    if [[ -z $TEAM_ID_W ]]
    then
      INSERT_WINNER_RESULT=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")
      TEAM_ID_W=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
    fi

    # Add opponent team to teams table and update variable if it does not already exist
    if [[ -z $TEAM_ID_O ]]
    then
      INSERT_OPPONENT_RESULT=$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')")
      TEAM_ID_O=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")
    fi

    # Add game details to games table
    INSERT_GAME_RESULT=$($PSQL "INSERT INTO games(year, round, winner_id, opponent_id, winner_goals, opponent_goals) VALUES($YEAR, '$ROUND', $TEAM_ID_W, $TEAM_ID_O, $WINNER_GOALS, $OPPONENT_GOALS)")
  fi
done

I’m not sure how the whole online codespace/code editor CodeRoad CodeAlly stuff works in the backend but maybe it’s a thing where performance varies throughout the day so my last hunch I’ll try is trying the tests late at night or something. Either way perhaps it’d be nice to up that time limit to like maybe a minute?

2 Likes

Also for me. The insert_data.sh take quite a long time 50-60 seconds when I run the insert data script in the terminal. But that file contains only inserts, it cannot be reduced any more. Perhaps some performance issue. Please, can be the time limit set to a higher value that 20sec?

So update: it finally decided to let me have all of the passes test! I have been using the time ./insert_data.sh command in the terminal throughout my attempts at different times to keep trying and there was a wide range of results ranging from the 40s, 50s, and even all the way up to 1min and 1min30s.

Today around Thu May 11 15:35:14 CST 2023 I decided to attempt again and got 12s! Running the tests still for some reason didn’t pass the queries and one of the insert_data checkmarks so I still had to reset the container again. After starting it fresh, I rebuilt the database from my dumped file, copy pasted my insert_data and the queries scripts, gave permissions to both, ran the insert_data script once in the terminal, then finally ran the tests which let me through.

So my recommendation for this problem if it’s still a problem for you (and for future folks if it’s still a problem then) is to try the tests later throughout different times or days. You can soft check with time ./insert_data.sh in the terminal and when it shows a nice time then reset the container instead of constantly resetting the container and repasting your code just to run the tests.

The whole performance at different times thing is still just speculation on my part since I tried super late at night and super early morning to no avail so there may be other factors at play but I’d say it’s not something to stress too much. You can always just continue on to the next course in the meantime. Best of luck and I hope everyone else’s tests pass!

2 Likes

Thanks for sharing the time command, @andreshungbz !

Currently (May 12 , 2023 12:36h CEST) I get the following after running time ./insert_data:

Let’s hope we all find a good time to pass this!

1 Like

You need to modify your script to reduce the length of the script

In my case, the worldcup database is there, complete with the teams and games data on it, but somehow the code road can’t detect it

I’ve try to reset the progress and yeah, it works, but then crashed again after I finished my insert_data.sh script.

then I’ve try to delete the database and start over to create the database again, but still, after I create the worldcup database, code road can’t detect it

I wanna try to delete the session and start over again, but after see some people done doing it but having no good result, I’m afraid that’s not the main problem. So I guess freecodecamp team should look for deeper analysis about this since this struggle us to proceed to the next step of this course

I hope this explanation helps

When you hit Run on the CodeRoad tests can you share a screenshot of what appears in the test output?


(Open a terminal, click the OUTPUT tab, select CodeRoad (Tests) and then hit Run on the tests).

Here are the output for CodeRoad (Test):

I found out that when I reset the content inside insert_data.sh manually (deleting all query line except some preserved line in the beginning), the CddeRun start to works normally, so I think it’s related to my code or something. Since I’m not sure about it, I’ll put my code for insert_data.sh for further inspection

#! /bin/bash

if [[ $1 == "test" ]]
then
  PSQL="psql --username=postgres --dbname=worldcuptest -t --no-align -c"
else
  PSQL="psql --username=freecodecamp --dbname=worldcup -t --no-align -c"
fi

# Do not change code above this line. Use the PSQL variable above to query your database.

echo $($PSQL "TRUNCATE teams, games")

cat games.csv | while IFS="," read year round winner opponent winner_goals opponent_goals
do
  if [[ $year != "year" ]]
  then
    # get winner_id
    winner_id=$($PSQL "SELECT team_id FROM teams WHERE name='$winner'")

    # if not found
    if [[ -z $winner_id ]]
    then
      # insert major
      echo "$($PSQL "INSERT INTO teams(name) VALUES('$winner')")"
      # if [[ $INSERT_TEAM_RESULT == "INSERT 0 1" ]]
      # then
      #   echo Inserted into teams, $winner
      # fi

      # get new major_id
      winner_id=$($PSQL "SELECT team_id FROM teams WHERE name='$winner'")
    fi

    opponent_id=$($PSQL "SELECT team_id FROM teams WHERE name='$opponent'")

    # if not found
    if [[ -z $opponent_id ]]
    then
      # insert major
      echo "$($PSQL "INSERT INTO teams(name) VALUES('$opponent')")"
      # if [[ $INSERT_TEAM_RESULT == "INSERT 0 1" ]]
      # then
      #   echo Inserted into teams, $opponent
      # fi

      # get new major_id
      opponent_id=$($PSQL "SELECT team_id FROM teams WHERE name='$opponent'")
    fi

    echo "$($PSQL "INSERT INTO games(year, round, winner_goals, opponent_goals, winner_id, opponent_id) VALUES($year, '$round', $winner_goals, $opponent_goals, $winner_id, $opponent_id)")"
    # if [[ $INSERT_game_RESULT == "INSERT 0 1" ]]
    # then
    #   echo Inserted into games, $winner vs $opponent
    # fi

  fi
done

At a cursory glance, I can’t see anything wrong with your code.
The issue is most likely that the tests are running too slowly and causing timeouts.

Your error message appears to relate to the default mocha timeout, which can be changed in package.json (in the .freeCodeCamp directory).

The quickest way to adjust this is to open terminal and enter the following command:

sed 's/20000/50000/' -i /home/codeally/project/.freeCodeCamp/package.json

As you’ll see, the timeout has been adjust to 50secs now.
If you uncomment all of your code and Run the tests again, you should now have more luck!

1 Like

I’ve tried it, but with some customization (Instead of 50000, I’m using 100000, since 50000 still can’t solve the problem :sweat_smile: ) and IT WORKS for me!!

Thank you so much for your support @igorgetmeabrain , it helps a lot!!

tried this with 120000 and finally works!!!

For me, it worked when I added echo in front of the insert commands in insert_data.sh script.