-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfetch-f1db-data.sh
executable file
·66 lines (52 loc) · 2.21 KB
/
fetch-f1db-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
63
64
65
66
#!/usr/bin/env bash
# Pull the f1db racing database and load it into PostgreSQL.
# From The Art of Postgres: Chapter X, YYYYYYYYYY
set -e
NC='\033[0m' # No color (reset)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
F1DB_DB_NAME=f1db
F1DB_SQLITE_DATASOURCE=http://ergast.com/downloads/f1db.sql.gz
echo -e "${GREEN}Fetching latest version of f1db...${NC}"
wget $F1DB_SQLITE_DATASOURCE -Nq
gunzip -kf f1db.sql.gz
echo -e "${GREEN}Installing and starting MariaDB...${NC}"
sudo apt-get update > /dev/null
sudo apt-get install -y mariadb-server mariadb-client > /dev/null
sudo /etc/init.d/mysql start > /dev/null
until pg_isready > /dev/null && pgrep mysql | wc -l > /dev/null; do
echo -e "${YELLOW}Waiting for PostgreSQL and MySQL to start${NC}"
sleep 1
done
echo -e "${GREEN}Creating intermediary MariaDB database and importing f1db data...${NC}"
sudo mysql -u root -e "DROP DATABASE IF EXISTS $F1DB_DB_NAME;"
sudo mysql -u root -e "CREATE DATABASE $F1DB_DB_NAME;"
sudo mysql -u root -e "USE $F1DB_DB_NAME; SOURCE f1db.sql;"
sudo mysql -e "CREATE USER IF NOT EXISTS 'postgres'@'localhost';"
sudo mysql -e "CREATE USER IF NOT EXISTS 'postgres'@'$HOST_IP';"
sudo mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'postgres'@'localhost';"
sudo mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'postgres'@'$HOST_IP';"
if psql -lqt | cut -d \| -f 1 | grep -qw $F1DB_DB_NAME; then
if [[ $1 == "--recreate" ]]; then
echo -e "${YELLOW}--recreate flag was given, dropping database $F1DB_DB_NAME${NC}"
dropdb $F1DB_DB_NAME
else
echo -e "${RED}$F1DB_DB_NAME PostgreSQL database already exists, skipping${NC}"
echo -e "${RED}Include the --recreate flag if you want to drop and reseed the database${NC}"
exit 1
fi
fi
echo -e "${GREEN}Creating PostgreSQL database...${NC}"
createdb $F1DB_DB_NAME
echo -e "${GREEN}Migrating f1db data from MySQL to PostgreSQL${NC}"
pgloader \
mysql://postgres@localhost/$F1DB_DB_NAME \
pgsql://postgres@localhost/$F1DB_DB_NAME \
> /dev/null
echo -e "${GREEN}Cleaning up...${NC}"
sudo mysql -u root -e "DROP DATABASE IF EXISTS $F1DB_DB_NAME;"
sudo /etc/init.d/mysql stop > /dev/null
yes | sudo apt-get autoremove --purge mariadb-server mariadb-client > /dev/null
rm f1db.sql
echo -e "${GREEN}Done! 🎉${NC}"