-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_data.sh
48 lines (43 loc) · 1.74 KB
/
insert_data.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /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 games, teams")
cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
# echo $YEAR, $ROUND, $WINNER, $OPPONENT, $WINNER_GOALS, $OPPONENT_GOALS
if [[ $YEAR != year ]]
then
# get WINNING team_id
WINNING_TEAM_ID=$($PSQL "select team_id from teams where name = '$WINNER'")
OPPONENT_TEAM_ID=$($PSQL "select team_id from teams where name = '$OPPONENT'")
# if winning team id not found
if [[ -z $WINNING_TEAM_ID ]]
then
INSERT_WINNER_RESULT=$($PSQL "INSERT INTO teams(name) values('$WINNER')")
if [[ $INSERT_WINNER_RESULT == "INSERT 0 1" ]]
then
echo "Inserted winner $WINNER into teams."
fi
#get new winning_team_id
WINNING_TEAM_ID=$($PSQL "select team_id from teams where name = '$WINNER'")
fi
#if opponent team id not found
if [[ -z $OPPONENT_TEAM_ID ]]
then
INSERT_OPPONENT_RESULT=$($PSQL "INSERT INTO teams(name) values('$OPPONENT')")
if [[ $INSERT_OPPONENT_RESULT == "INSERT 0 1" ]]
then
echo "Inserted opponent $OPPONENT into teams."
fi
#get new opponent_team_id
OPPONENT_TEAM_ID=$($PSQL "select team_id from teams where name = '$OPPONENT'")
fi
# insert into games
INSERT_GAME_RESULTS=$($PSQL "insert into games(year, round, winner_id, opponent_id, winner_goals, opponent_goals) values ($YEAR, '$ROUND', $WINNING_TEAM_ID, $OPPONENT_TEAM_ID, $WINNER_GOALS, $OPPONENT_GOALS)")
fi
done