-
Notifications
You must be signed in to change notification settings - Fork 27
/
migration.sh
executable file
·90 lines (75 loc) · 2.49 KB
/
migration.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <database_file>"
exit 1
fi
db_file="$1"
confirm_action() {
local prompt="$1"
read -p "$prompt (y/n): " confirm
if [ "$confirm" != "y" ]; then
echo "Action aborted."
exit 1
fi
}
run_sql() {
local sql="$1"
if ! sqlite3 "$db_file" <<< "$sql"; then
echo "SQL execution failed."
exit 1
fi
}
execute_migration() {
local description="$1"
local sql="$2"
confirm_action "$description '$db_file'. Do you want to proceed?"
echo "Executing migration..."
run_sql "BEGIN;"
run_sql "$sql"
confirm_action "Migration completed, commit?"
if ! run_sql "COMMIT;"; then
echo "Commit failed."
exit 1
fi
echo "Changes committed successfully"
}
migrations=(
"04/11/2023:This migration as of 04/11/23 adds the UserVips, LiveChatDeletions tables and deletionId column to LiveChatMessages.:\
ALTER TABLE LiveChatMessages ADD COLUMN deletionId INTEGER;\
CREATE TABLE IF NOT EXISTS LiveChatDeletions (\
deletionId INTEGER PRIMARY KEY,\
moderatorIntId INTEGER NOT NULL,\
reason TEXT,\
deletionDate INTEGER,\
FOREIGN KEY (messageId) REFERENCES LiveChatMessages(messageId),\
FOREIGN KEY (moderatorIntId) REFERENCES Users(intId)\
);\
CREATE TABLE IF NOT EXISTS UserVips (\
userIntId INTEGER NOT NULL,\
keyHash TEXT NOT NULL,\
lastUsed INTEGER,\
FOREIGN KEY(userIntId) REFERENCES Users(intId)\
);"
"20/06/2024:This migration as of 20/06/24 adds userAgent column to KnownIps and a Linkages table.:\
ALTER TABLE KnownIps ADD COLUMN userAgent TEXT;\
CREATE TABLE IF NOT EXISTS Linkages (\
userIntId INTEGER UNIQUE,\
accountId INTEGER,\
linkDate INTEGER,\
FOREIGN KEY (userIntId) REFERENCES Users(intId)\
);"
)
echo "Please choose a migration to execute:"
for i in "${!migrations[@]}"; do
migration_date=$(echo "${migrations[i]}" | cut -d':' -f1)
echo "$((i + 1))) Migration for $migration_date"
done
read -p "Enter the number of the migration you want to execute: " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || (( choice < 1 || choice > ${#migrations[@]} )); then
echo "Invalid choice."
exit 1
fi
migration_data="${migrations[$((choice - 1))]}"
migration_description=$(echo "$migration_data" | cut -d':' -f2)
migration_sql=$(echo "$migration_data" | cut -d':' -f3)
execute_migration "$migration_description" "$migration_sql"