feat: auto-migrate SQLite data to Postgres on first deploy

- Add trumpsignal.db to git (one-time migration source)
- entrypoint.sh detects empty Postgres and runs migration automatically
- scripts/migrate_sqlite_to_postgres.py: posts, subscriptions, bot_trades
- Add psycopg2-binary to requirements for migration script

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
k
2026-04-21 20:48:05 +08:00
parent 1deb6a3918
commit 6021a3883d
6 changed files with 149 additions and 4 deletions
+30
View File
@@ -4,5 +4,35 @@ set -e
echo "[entrypoint] Running Alembic migrations..."
alembic upgrade head
# One-time migration: if trumpsignal.db exists and posts table is empty,
# migrate SQLite data into Postgres then remove the db file.
if [ -f "trumpsignal.db" ]; then
echo "[entrypoint] Found trumpsignal.db — checking if migration needed..."
POST_COUNT=$(python -c "
import asyncio, os
os.environ.setdefault('DATABASE_URL', '')
import psycopg2
url = os.environ.get('DATABASE_URL','').replace('+asyncpg','')
try:
conn = psycopg2.connect(url)
cur = conn.cursor()
cur.execute('SELECT COUNT(*) FROM posts')
print(cur.fetchone()[0])
conn.close()
except Exception as e:
print('0')
" 2>/dev/null || echo "0")
if [ "$POST_COUNT" = "0" ]; then
echo "[entrypoint] Postgres is empty — running SQLite migration..."
python scripts/migrate_sqlite_to_postgres.py
echo "[entrypoint] Migration done. Removing trumpsignal.db..."
rm -f trumpsignal.db
else
echo "[entrypoint] Postgres already has data (${POST_COUNT} posts) — skipping migration."
rm -f trumpsignal.db
fi
fi
echo "[entrypoint] Starting API server..."
exec uvicorn app.main:app --host 0.0.0.0 --port 8000