aboutsummaryrefslogtreecommitdiff
path: root/db/scripts/init.sh
blob: 1a8bbdef782946b56c35030664c2c76d0c823b04 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 "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"