diff options
author | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-16 11:23:03 -0400 |
---|---|---|
committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-16 11:25:29 -0400 |
commit | f91870e5a7a210acb7efd7c2355220f5bcc6c5be (patch) | |
tree | 542b78e943effe43a26c21687370c20074b96617 | |
parent | b32419dfe996fbc9731b48ba528bae67535f4839 (diff) |
write init script for database.
-rw-r--r-- | db/scripts/init.sh | 84 |
1 files changed, 80 insertions, 4 deletions
diff --git a/db/scripts/init.sh b/db/scripts/init.sh index e55292b..1a8bbde 100644 --- a/db/scripts/init.sh +++ b/db/scripts/init.sh | |||
@@ -1,9 +1,85 @@ | |||
1 | sqlite3 $DB_PATH < ../sql/tables.sql | 1 | PROJECT_DIR=$(pwd) |
2 | |||
3 | DB_PATH="$PROJECT_DIR/db/test.sqlite3" | ||
4 | |||
5 | SQL_DIR="$PROJECT_DIR/db/sql" | ||
6 | |||
7 | |||
2 | 8 | ||
3 | insert_user() { | 9 | insert_user() { |
4 | read -p "Enter a username: " username | 10 | read -p "Enter a username: " username |
5 | read -sp | 11 | read -sp "Enter a password: " password |
6 | } | 12 | hash=$(mkpasswd -m bcrypt "$password") |
13 | |||
14 | if [ $? -ne 0 ]; then | ||
15 | echo "Error: Failed to hash password." | ||
16 | exit 1 | ||
17 | fi | ||
18 | |||
19 | uuid=$(uuidgen) | ||
20 | |||
21 | sqlite3 $DB_PATH "INSERT INTO users (name, password, uuid) VALUES ('$username', '$hash', '$uuid');" | ||
22 | |||
23 | if [ $? -ne 0 ]; then | ||
24 | echo "Error: Failed to insert user into the database." | ||
25 | exit 1 | ||
26 | fi | ||
27 | } | ||
28 | |||
29 | sqlite3 $DB_PATH < "$SQL_DIR/tables.sql" | ||
30 | |||
31 | if [ $? -ne 0 ]; then | ||
32 | echo "Error: Failed to create tables in the database." | ||
33 | exit 1 | ||
34 | fi | ||
7 | 35 | ||
8 | echo "Before continuing you must create users. The reset of the schema depends on them" | 36 | echo "Before continuing you must create users. The reset of the schema depends on them" |
9 | 37 | ||
38 | while true; do | ||
39 | insert_user | ||
40 | echo -e "\nDo you want to add anoter user? (y/n):" | ||
41 | read choice | ||
42 | if [ "$choice" != "y" ]; then | ||
43 | break | ||
44 | fi | ||
45 | done | ||
46 | |||
47 | echo -e '\nUsers inserted, now setting up the rest of the database.' | ||
48 | |||
49 | echo -e "\nAdding views:" | ||
50 | sqlite3 $DB_PATH < "$SQL_DIR/views.sql" | ||
51 | |||
52 | if [ $? -ne 0 ]; then | ||
53 | echo "Error: Failed to add views." | ||
54 | exit 1 | ||
55 | fi | ||
56 | |||
57 | echo -e "\nAdding triggers:" | ||
58 | sqlite3 $DB_PATH < "$SQL_DIR/triggers.sql" | ||
59 | |||
60 | if [ $? -ne 0 ]; then | ||
61 | echo "Error: Failed to add triggers." | ||
62 | exit 1 | ||
63 | fi | ||
64 | |||
65 | echo -e "\nSeeding the database:" | ||
66 | sqlite3 $DB_PATH < "$SQL_DIR/seed.sql" | ||
67 | |||
68 | if [ $? -ne 0 ]; then | ||
69 | echo "Error: Failed to seed the database." | ||
70 | exit 1 | ||
71 | fi | ||
72 | |||
73 | echo -e "\nThe database schema" | ||
74 | sqlite3 $DB_PATH .schema | ||
75 | |||
76 | if [ $? -ne 0 ]; then | ||
77 | echo "Error: Failed to print the database schema." | ||
78 | exit 1 | ||
79 | fi | ||
80 | |||
81 | echo -e "\n+------------------------------------+" | ||
82 | |||
83 | echo -e "| Database initialization completed. |" | ||
84 | |||
85 | echo -e "+------------------------------------+\n" \ No newline at end of file | ||