-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.sh
More file actions
71 lines (54 loc) · 2.56 KB
/
Copy pathmigrate.sh
File metadata and controls
71 lines (54 loc) · 2.56 KB
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
#!/bin/bash
set -e
# --- SETTINGS ---
# Using example: ./migrate.sh "migration_description"
# File name (example: docker-compose.dev.yml):
COMPOSE_FILE="docker-compose.dev.yml"
# User and database host (from your .env or compose)
DB_USER="shinobi"
DB_HOST="postgres_db"
# 1. Automatic transition to the folder where the script itself is located
# This ensures that docker compose finds the file next to the script
cd "$(dirname "$0")" || { echo "Error: Failed to navigate to script directory"; exit 1; }
# 2. Argument checking (migration description)
if [ -z "$1" ]; then
echo "Error: Please enter a migration description!"
echo "Example: ./migrate.sh 'remove_email_field'"
exit 1
fi
MESSAGE=$1
# 3. Checking the presence of a configuration file
if [ ! -f "$COMPOSE_FILE" ]; then
echo "Error: File $COMPOSE_FILE not found in directory $(pwd)!"
exit 1
fi
# 4. Raising the base
echo "[DEV] Starting database $COMPOSE_FILE..."
docker compose -f "$COMPOSE_FILE" up "$DB_HOST" -d
# 5. Waiting ready Postgres
echo "Waiting for Postgres to wake up..."
until docker compose -f "$COMPOSE_FILE" exec "$DB_HOST" pg_isready -U "$DB_USER" > /dev/null 2>&1; do
echo -n "."
sleep 1
done
echo -e "\nDatabase is ready."
# 6. Creating migration
echo "Generating a migration file..."
docker compose -f "$COMPOSE_FILE" run --rm --no-deps bot alembic revision --autogenerate -m "$MESSAGE" || { echo "Generation error!"; exit 1; }
# 7. TEST_DRIVE: Checking the stability of the migration
echo "Running a migration test (Test Drive)..."
echo " -> 1/3 Upgrade (Upgrade head)..."
docker compose -f "$COMPOSE_FILE" run --rm --no-deps bot alembic upgrade head || { echo "Application error!"; exit 1; }
echo " -> 2/3 Rollback (Downgrade -1)..."
docker compose -f "$COMPOSE_FILE" run --rm --no-deps bot alembic downgrade -1 || { echo "Rollback error! Check the downgrade() method in the migration file"; exit 1; }
echo " -> 3/3 Final upgrade..."
docker compose -f "$COMPOSE_FILE" run --rm --no-deps bot alembic upgrade head || { echo "Final application error!"; exit 1; }
# 8. Checking for "forgotten" fields in models
echo "Checking the compliance of models and database (alembic check)..."
docker compose -f "$COMPOSE_FILE" run --rm --no-deps bot alembic check || { echo "Attention: Models do not match current migrations!"; exit 1; }
# 9. Stopping base
echo "Stopping the base..."
docker compose -f "$COMPOSE_FILE" stop "$DB_HOST"
echo "--------------------------------------------------"
echo "Migration '$MESSAGE' created, tested and applied."
echo "!!!Check correctness migration file in folder alembic/versions/"