From f91870e5a7a210acb7efd7c2355220f5bcc6c5be Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Sat, 16 Mar 2024 11:23:03 -0400 Subject: write init script for database. --- db/scripts/init.sh | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file 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 @@ -sqlite3 $DB_PATH < ../sql/tables.sql +PROJECT_DIR=$(pwd) + +DB_PATH="$PROJECT_DIR/db/test.sqlite3" + +SQL_DIR="$PROJECT_DIR/db/sql" + + insert_user() { -read -p "Enter a username: " username -read -sp - } + read -p "Enter a username: " username + read -sp "Enter a password: " password + hash=$(mkpasswd -m bcrypt "$password") + + if [ $? -ne 0 ]; then + echo "Error: Failed to hash password." + exit 1 + fi + + uuid=$(uuidgen) + + sqlite3 $DB_PATH "INSERT INTO users (name, password, uuid) VALUES ('$username', '$hash', '$uuid');" + + if [ $? -ne 0 ]; then + echo "Error: Failed to insert user into the database." + exit 1 + fi +} + +sqlite3 $DB_PATH < "$SQL_DIR/tables.sql" + +if [ $? -ne 0 ]; then + echo "Error: Failed to create tables in the database." + exit 1 +fi echo "Before continuing you must create users. The reset of the schema depends on them" +while true; do + insert_user + echo -e "\nDo you want to add anoter user? (y/n):" + read choice + if [ "$choice" != "y" ]; then + break + fi +done + +echo -e '\nUsers inserted, now setting up the rest of the database.' + +echo -e "\nAdding views:" +sqlite3 $DB_PATH < "$SQL_DIR/views.sql" + +if [ $? -ne 0 ]; then + echo "Error: Failed to add views." + exit 1 +fi + +echo -e "\nAdding triggers:" +sqlite3 $DB_PATH < "$SQL_DIR/triggers.sql" + +if [ $? -ne 0 ]; then + echo "Error: Failed to add triggers." + exit 1 +fi + +echo -e "\nSeeding the database:" +sqlite3 $DB_PATH < "$SQL_DIR/seed.sql" + +if [ $? -ne 0 ]; then + echo "Error: Failed to seed the database." + exit 1 +fi + +echo -e "\nThe database schema" +sqlite3 $DB_PATH .schema + +if [ $? -ne 0 ]; then + echo "Error: Failed to print the database schema." + exit 1 +fi + +echo -e "\n+------------------------------------+" + +echo -e "| Database initialization completed. |" + +echo -e "+------------------------------------+\n" \ No newline at end of file -- cgit v1.1