forked from QuivrHQ/quivr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.sh
executable file
·70 lines (58 loc) · 2.79 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
#!/bin/bash
# Exit script immediately on first error.
set -e
# Function to run SQL file
run_sql_file() {
local file="$1"
PGPASSWORD=${DB_PASSWORD} psql -h "${DB_HOST}" -p "${DB_PORT}" -d "${DB_NAME}" -U "${DB_USER}" -f "$file"
}
# Flag to indicate whether to prompt for DB info
prompt_for_db_info=false
# Check if .migration_info exists and source it
if [ -f .migration_info ]; then
source .migration_info
# Check if any of the variables are empty
if [ -z "$DB_HOST" ] || [ -z "$DB_NAME" ] || [ -z "$DB_PORT" ] || [ -z "$DB_USER" ] || [ -z "$DB_PASSWORD" ]; then
prompt_for_db_info=true # Some values are empty, so prompt user for values
fi
else
prompt_for_db_info=true # No .migration_info file, so prompt user for values
fi
# If .migration_info doesn't exist or any of the variables are empty, prompt for DB info
if $prompt_for_db_info ; then
echo "Please enter the following database connection information that can be found in Supabase in Settings -> database:"
DB_HOST=$(gum input --placeholder "Host")
DB_NAME=$(gum input --placeholder "Database name")
DB_PORT=$(gum input --placeholder "Port")
DB_USER=$(gum input --placeholder "User")
DB_PASSWORD=$(gum input --placeholder "Password" --password)
# Save the inputs in .migration_info file
echo "DB_HOST=$DB_HOST" > .migration_info
echo "DB_NAME=$DB_NAME" >> .migration_info
echo "DB_PORT=$DB_PORT" >> .migration_info
echo "DB_USER=$DB_USER" >> .migration_info
echo "DB_PASSWORD=$DB_PASSWORD" >> .migration_info
fi
# Ask user whether to create tables or run migrations
CHOICE=$(gum choose --header "Choose an option" "Create all tables" "Run Migrations")
if [ "$CHOICE" == "Create all tables" ]; then
# Running the tables.sql file to create tables
run_sql_file "scripts/tables.sql"
else
# Get the last migration that was executed
LAST_MIGRATION=$(PGPASSWORD=${DB_PASSWORD} psql -h "${DB_HOST}" -p "${DB_PORT}" -d "${DB_NAME}" -U "${DB_USER}" -tAc "SELECT name FROM migrations ORDER BY executed_at DESC LIMIT 1;")
echo "Last migration executed: $LAST_MIGRATION"
# Iterate through the migration files
for file in $(ls scripts | grep -E '^[0-9]+.*\.sql$' | sort); do
MIGRATION_ID=$(basename "$file" ".sql")
# Only run migrations that are newer than the last executed migration
if [ -z "$LAST_MIGRATION" ] || [ "$MIGRATION_ID" \> "$LAST_MIGRATION" ]; then
# Run the migration
echo "Running migration $file"
run_sql_file "scripts/$file"
# And record it as having been run
PGPASSWORD=${DB_PASSWORD} psql -h "${DB_HOST}" -p "${DB_PORT}" -d "${DB_NAME}" -U "${DB_USER}" -c "INSERT INTO migrations (id) VALUES ('${MIGRATION_ID}');"
fi
done
fi
echo "Migration script completed."