forked from parkib/flaskm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.sh
executable file
·65 lines (50 loc) · 2.05 KB
/
migrate.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
#!/bin/bash
# Set environment variables
export FLASK_APP=main
export PYTHONPATH=.:$PYTHONPATH
# Check if sqlite3 is installed
if ! command -v sqlite3 &> /dev/null; then
echo "Error: sqlite3 is not installed. Please install it before running this script."
exit 1
fi
# Check if python3 is installed
if ! command -v python3 &> /dev/null; then
echo "Error: python3 is not installed. Please install it before running this script."
exit 1
fi
# Check if Flask is installed
if ! python3 -m flask --version &> /dev/null; then
echo "Error: Flask is not installed. Please install it before running this script."
exit 1
fi
# Check if the migration directory exists
if [ ! -d "migrations" ]; then
echo "Initializing migration for the first time..."
python3 -m flask db init
fi
# Check if sqlite.db does not exists and there is a sqlite-backup.db file
# . restore from backup before migration
if [ ! -e "instance/volumes/sqlite.db" ] && [ -e "instance/volumes/sqlite-backup.db" ]; then
echo "No sqlite.db found, using sqlite-backup.db to generate the file."
# Copy backup file to primary (sqlite.db)
cp "instance/volumes/sqlite-backup.db" "instance/volumes/sqlite.db"
# Extract the new Alembic version from the backup database
backup_version=$(sqlite3 instance/volumes/sqlite.db "SELECT version_num FROM alembic_version;")
echo "Version ${backup_version} detected"
python3 -m flask db stamp "${backup_version}"
# Check if sqlite.db exists
# . backup before migration
elif [ -e "instance/volumes/sqlite.db" ]; then
# Create a timestamp for the backup file
timestamp=$(date "+%Y%m%d%H%M%S")
backup_file="instance/volumes/sqlite-backup-${timestamp}.db"
# Backup SQLite database
sqlite3 instance/volumes/sqlite.db ".backup instance/volumes/sqlite-backup.db"
sqlite3 instance/volumes/sqlite.db ".backup ${backup_file}"
fi
# Perform database migrations
python3 -m flask db migrate
# Perform database upgrade
python3 -m flask db upgrade
# Run a custom command to generate data
python3 -m flask custom generate_data