-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_data.sh
executable file
·62 lines (53 loc) · 1.63 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /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.
# clear data
echo "$($PSQL "TRUNCATE TABLE games, teams")"
# set file name
if [[ $1 == "fast" ]]
then
CSV="games_test.csv"
else
CSV="games.csv"
fi
# read in file
# year,round,winner,opponent,winner_goals,opponent_goals
cat $CSV | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
# skip the column headers
if [[ $YEAR == "year" ]]
then
continue
fi
# debug
echo $YEAR $ROUND $WINNER $OPPONENT $WINNER_GOALS $OPPONENT_GOALS
# find winner ID
RESPONSE="$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")"
# if not exist:
if [[ -z $RESPONSE ]]
then
# add team
echo "$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")"
# get new team ID
RESPONSE="$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")"
fi
WINNER_ID=$RESPONSE
# find opponent ID
RESPONSE="$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")"
# if not exist:
if [[ -z $RESPONSE ]]
then
# add team
echo "$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')")"
# get new team ID
RESPONSE="$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")"
fi
OPPONENT_ID=$RESPONSE
# add new game record
echo "$($PSQL "INSERT INTO games(year,round,winner_id,opponent_id,winner_goals,opponent_goals) VALUES('$YEAR','$ROUND','$WINNER_ID','$OPPONENT_ID','$WINNER_GOALS','$OPPONENT_GOALS')")"
done