aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZach Berwaldt <zberwaldt@tutamail.com>2024-03-16 11:23:03 -0400
committerZach Berwaldt <zberwaldt@tutamail.com>2024-03-16 11:25:29 -0400
commitf91870e5a7a210acb7efd7c2355220f5bcc6c5be (patch)
tree542b78e943effe43a26c21687370c20074b96617
parentb32419dfe996fbc9731b48ba528bae67535f4839 (diff)
write init script for database.
-rw-r--r--db/scripts/init.sh84
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 @@
1sqlite3 $DB_PATH < ../sql/tables.sql 1PROJECT_DIR=$(pwd)
2
3DB_PATH="$PROJECT_DIR/db/test.sqlite3"
4
5SQL_DIR="$PROJECT_DIR/db/sql"
6
7
2 8
3insert_user() { 9insert_user() {
4read -p "Enter a username: " username 10 read -p "Enter a username: " username
5read -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
29sqlite3 $DB_PATH < "$SQL_DIR/tables.sql"
30
31if [ $? -ne 0 ]; then
32 echo "Error: Failed to create tables in the database."
33 exit 1
34fi
7 35
8echo "Before continuing you must create users. The reset of the schema depends on them" 36echo "Before continuing you must create users. The reset of the schema depends on them"
9 37
38while 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
45done
46
47echo -e '\nUsers inserted, now setting up the rest of the database.'
48
49echo -e "\nAdding views:"
50sqlite3 $DB_PATH < "$SQL_DIR/views.sql"
51
52if [ $? -ne 0 ]; then
53 echo "Error: Failed to add views."
54 exit 1
55fi
56
57echo -e "\nAdding triggers:"
58sqlite3 $DB_PATH < "$SQL_DIR/triggers.sql"
59
60if [ $? -ne 0 ]; then
61 echo "Error: Failed to add triggers."
62 exit 1
63fi
64
65echo -e "\nSeeding the database:"
66sqlite3 $DB_PATH < "$SQL_DIR/seed.sql"
67
68if [ $? -ne 0 ]; then
69 echo "Error: Failed to seed the database."
70 exit 1
71fi
72
73echo -e "\nThe database schema"
74sqlite3 $DB_PATH .schema
75
76if [ $? -ne 0 ]; then
77 echo "Error: Failed to print the database schema."
78 exit 1
79fi
80
81echo -e "\n+------------------------------------+"
82
83echo -e "| Database initialization completed. |"
84
85echo -e "+------------------------------------+\n" \ No newline at end of file