Skip to content

Commit 9c6168a

Browse files
committed
feat(docker): add entrypoint script for database readiness and migrations
- Introduced a new entrypoint script to manage database readiness and migrations before starting the Tux bot. - Implemented functions to wait for the database to be ready and handle migrations, including force migration options. - Enhanced startup process with clear logging for each step of the database handling.
1 parent 906ea5b commit 9c6168a

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

docker/adminer/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$_POST['auth'] = [
1414
'server' => getenv('ADMINER_DEFAULT_SERVER') ?: 'tux-postgres',
1515
'username' => getenv('ADMINER_DEFAULT_USERNAME') ?: 'tuxuser',
16-
'password' => getenv('ADMINER_DEFAULT_PASSWORD') ?: 'tuxpass',
16+
'password' => getenv('ADMINER_DEFAULT_PASSWORD') ?: 'ChangeThisToAStrongPassword123!',
1717
'driver' => getenv('ADMINER_DEFAULT_DRIVER') ?: 'pgsql',
1818
'db' => getenv('ADMINER_DEFAULT_DB') ?: 'tuxdb',
1919
];

docker/entrypoint.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
set -e
3+
4+
echo "🐧 Tux Docker Entrypoint"
5+
echo "========================"
6+
7+
# Function to check if database is ready (simple socket check)
8+
wait_for_db() {
9+
echo "⏳ Waiting for database to be ready..."
10+
until python -c "
11+
import socket
12+
import sys
13+
try:
14+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
15+
sock.settimeout(1)
16+
result = sock.connect_ex(('$POSTGRES_HOST', $POSTGRES_PORT))
17+
sock.close()
18+
sys.exit(0 if result == 0 else 1)
19+
except Exception:
20+
sys.exit(1)
21+
"; do
22+
echo "Database is unavailable - sleeping"
23+
sleep 2
24+
done
25+
echo "✅ Database is ready!"
26+
}
27+
28+
# Function to handle migrations
29+
handle_migrations() {
30+
echo "🔄 Handling database migrations..."
31+
32+
# Change to the app directory where alembic.ini is located
33+
cd /app
34+
35+
# Check if we need to force migration
36+
if [ "$FORCE_MIGRATE" = "true" ]; then
37+
echo "⚠️ WARNING: Force migration can cause data inconsistency!"
38+
echo "🔧 Force migrating database to head..."
39+
python -m alembic stamp head
40+
echo "✅ Database force migrated to head"
41+
else
42+
# Try normal migration
43+
echo "🔄 Running normal migrations..."
44+
if ! python -m alembic upgrade head; then
45+
echo "⚠️ Migration failed, attempting to fix..."
46+
echo "📊 Current migration status:"
47+
python -m alembic current
48+
echo "🔧 Attempting to stamp database as head..."
49+
python -m alembic stamp head
50+
echo "✅ Database stamped as head"
51+
else
52+
echo "✅ Migrations completed successfully"
53+
fi
54+
fi
55+
}
56+
57+
# Main execution
58+
echo "⏳ Waiting for database to be ready..."
59+
wait_for_db
60+
61+
echo "🔄 Handling database migrations..."
62+
handle_migrations
63+
64+
echo "🚀 Starting Tux bot..."
65+
exec tux start

0 commit comments

Comments
 (0)