Files
TimeTrack/startup.sh
Jens Luedicke 667040d7f8 Squashed commit of the following:
commit cb82580f868b629902ba96c7f09f885b7d9c24dc
Author: Jens Luedicke <jens.luedicke@gmail.com>
Date:   Thu Jul 3 22:42:49 2025 +0200

    Fix for postgres db migration. #5

commit 6a4505e2db1cdb2cec65e630b63535ba08c02fc4
Author: Jens Luedicke <jens.luedicke@gmail.com>
Date:   Thu Jul 3 22:39:58 2025 +0200

    Fix for postgres db migration. #4

commit 7d9a5bb12c591182e67d7d52f90d6b1a45260d9f
Author: Jens Luedicke <jens.luedicke@gmail.com>
Date:   Thu Jul 3 22:38:02 2025 +0200

    Fix for postgres db migration. #3

commit 29dbb8b62d873dfbc4901b21e637a7181d545ec7
Author: Jens Luedicke <jens.luedicke@gmail.com>
Date:   Thu Jul 3 22:35:08 2025 +0200

    Fix for postgres db migration. #2

commit d5afc56290d05f53e06a77366214c605d0989c1d
Author: Jens Luedicke <jens.luedicke@gmail.com>
Date:   Thu Jul 3 22:33:09 2025 +0200

    Fix for postgres db migration.

commit 936008fe1c56b6e699c4a45b503507b6423e15eb
Author: Jens Luedicke <jens.luedicke@gmail.com>
Date:   Thu Jul 3 21:46:32 2025 +0200

    Add changes for gunicorn.

commit 464c71e5102117f35d05e1504165299ffa50c70c
Author: Jens Luedicke <jens.luedicke@gmail.com>
Date:   Thu Jul 3 20:30:29 2025 +0200

    Add changes for Postgres migration.
2025-07-03 22:50:37 +02:00

60 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
set -e
echo "Starting TimeTrack application..."
# Wait for PostgreSQL to be ready
echo "Waiting for PostgreSQL to be ready..."
while ! pg_isready -h db -p 5432 -U "$POSTGRES_USER" > /dev/null 2>&1; do
echo "PostgreSQL is not ready yet. Waiting..."
sleep 2
done
echo "PostgreSQL is ready!"
# Check if SQLite database exists and has data
SQLITE_PATH="/data/timetrack.db"
if [ -f "$SQLITE_PATH" ]; then
echo "SQLite database found at $SQLITE_PATH"
# Check if PostgreSQL database is empty
POSTGRES_TABLE_COUNT=$(psql "$DATABASE_URL" -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';" 2>/dev/null || echo "0")
if [ "$POSTGRES_TABLE_COUNT" -eq 0 ]; then
echo "PostgreSQL database is empty, running migration..."
# Create a backup of SQLite database
cp "$SQLITE_PATH" "${SQLITE_PATH}.backup.$(date +%Y%m%d_%H%M%S)"
echo "Created SQLite backup"
# Run migration
python migrate_sqlite_to_postgres.py
if [ $? -eq 0 ]; then
echo "Migration completed successfully!"
# Rename SQLite database to indicate it's been migrated
mv "$SQLITE_PATH" "${SQLITE_PATH}.migrated"
echo "SQLite database renamed to indicate migration completion"
else
echo "Migration failed! Check migration.log for details"
exit 1
fi
else
echo "PostgreSQL database already contains tables, skipping migration"
fi
else
echo "No SQLite database found, starting with fresh PostgreSQL database"
fi
# Initialize database tables if they don't exist
echo "Ensuring database tables exist..."
python -c "
from app import app, db
with app.app_context():
db.create_all()
print('Database tables created/verified')
"
# Start the Flask application with gunicorn
echo "Starting Flask application with gunicorn..."
exec gunicorn --bind 0.0.0.0:5000 --workers 4 --threads 2 --timeout 30 app:app