Commit 4b5c72cd authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Move prod's primary datastore from SQLite to PostgreSQL

SQLite was only ever meant for testing; prod now has real concurrent
features (Rooms/DM chat, websockets) that benefit from proper
multi-writer concurrency. Migrations were already dialect-agnostic (no
op.batch_alter_table or sqlite_* kwargs anywhere), so this was a clean
schema-only cutover - prod had zero real users, so nothing needed
migrating, just a fresh `alembic upgrade head` against an empty Postgres
database.

Also fixed two real SQLite-only assumptions in database.py that the
Postgres cutover exposed: `connect_args={"check_same_thread": False}`
applied unconditionally (psycopg has no such option and raises on it),
and the db-directory-creation logic ran DATABASE_URL through a
sqlite-only string replace regardless of dialect. Both now scoped to an
`_is_sqlite` check, matching the existing WAL-pragma guard.

deploy.sh's automated .env regeneration path also had a silent
`sqlite:///...` fallback for DATABASE_URL - removed in favor of a hard
failure (same posture as JWT_SECRET) so a future CI deploy can never
quietly revert prod back to SQLite by omission.

Redis was considered (explicitly asked about) and deliberately not
added: the only in-memory real-time state in Rooms/DMs is live
WebSocket objects (can't live in Redis regardless, tied to one
process's event loop) and small per-room ephemeral lists already
documented as intentionally discardable - Redis's actual value only
appears once there's more than one backend process, which isn't the
case yet.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent 446f0f1f
Pipeline #1260 failed with stage
...@@ -75,6 +75,13 @@ pip install --prefer-binary -q -r requirements-remix.txt ...@@ -75,6 +75,13 @@ pip install --prefer-binary -q -r requirements-remix.txt
# redeploy with no secrets in the environment never wipes a working .env ------------------ # redeploy with no secrets in the environment never wipes a working .env ------------------
if [ -n "${STAX_PROD_JWT_SECRET:-}" ]; then if [ -n "${STAX_PROD_JWT_SECRET:-}" ]; then
log "Writing $DEPLOY_DIR/.env from CI-supplied variables" log "Writing $DEPLOY_DIR/.env from CI-supplied variables"
# DATABASE_URL has no fallback default (same as JWT_SECRET below) - prod runs Postgres
# now, not SQLite, and a silent default here would quietly revert a real deploy back to
# SQLite the moment someone forgets to set STAX_PROD_DATABASE_URL as a CI/CD variable.
if [ -z "${STAX_PROD_DATABASE_URL:-}" ]; then
log "FAILED: STAX_PROD_DATABASE_URL not set - refusing to silently fall back to SQLite"
exit 1
fi
umask 077 umask 077
cat > .env.new <<EOF cat > .env.new <<EOF
JWT_SECRET=${STAX_PROD_JWT_SECRET} JWT_SECRET=${STAX_PROD_JWT_SECRET}
...@@ -84,7 +91,7 @@ TELEGRAM_BOT_TOKEN=${STAX_PROD_TELEGRAM_BOT_TOKEN:-} ...@@ -84,7 +91,7 @@ TELEGRAM_BOT_TOKEN=${STAX_PROD_TELEGRAM_BOT_TOKEN:-}
LALAL_API_KEY=${STAX_PROD_LALAL_API_KEY:-} LALAL_API_KEY=${STAX_PROD_LALAL_API_KEY:-}
DOCS_USERNAME=${STAX_PROD_DOCS_USERNAME:-} DOCS_USERNAME=${STAX_PROD_DOCS_USERNAME:-}
DOCS_PASSWORD=${STAX_PROD_DOCS_PASSWORD:-} DOCS_PASSWORD=${STAX_PROD_DOCS_PASSWORD:-}
DATABASE_URL=${STAX_PROD_DATABASE_URL:-sqlite:////root/stax_backend_api/db/stax.db} DATABASE_URL=${STAX_PROD_DATABASE_URL}
LOCAL_STORAGE_ROOT=${STAX_PROD_LOCAL_STORAGE_ROOT:-/root/stax_backend_api/var/storage} LOCAL_STORAGE_ROOT=${STAX_PROD_LOCAL_STORAGE_ROOT:-/root/stax_backend_api/var/storage}
EOF EOF
mv .env.new .env mv .env.new .env
......
...@@ -14,6 +14,8 @@ websockets>=13.0 # uvicorn's WebSocket protocol implementation - without this, ...@@ -14,6 +14,8 @@ websockets>=13.0 # uvicorn's WebSocket protocol implementation - without this,
# silently has no WebSocket support at all (no error, connections just # silently has no WebSocket support at all (no error, connections just
# fail); needed for the Pass-the-Aux room websocket routes # fail); needed for the Pass-the-Aux room websocket routes
sqlalchemy>=2.0.0 sqlalchemy>=2.0.0
psycopg[binary]>=3.1 # Postgres driver - prod runs Postgres, the test suite stays on SQLite's
# stdlib sqlite3 (needs nothing added) so this is additive, not a swap.
httpx>=0.27.0 httpx>=0.27.0
python-multipart>=0.0.9 python-multipart>=0.0.9
pytest>=8.0.0 pytest>=8.0.0
......
...@@ -11,13 +11,19 @@ from datetime import datetime, timezone ...@@ -11,13 +11,19 @@ from datetime import datetime, timezone
load_dotenv() load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./stax.db") DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./stax.db")
db_dir = os.path.dirname(DATABASE_URL.replace("sqlite:///", "")) _is_sqlite = DATABASE_URL.startswith("sqlite")
if db_dir and not os.path.exists(db_dir):
if _is_sqlite:
db_dir = os.path.dirname(DATABASE_URL.replace("sqlite:///", ""))
if db_dir and not os.path.exists(db_dir):
os.makedirs(db_dir, exist_ok=True) os.makedirs(db_dir, exist_ok=True)
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) # check_same_thread is a SQLite-only DBAPI option (relaxes its default single-thread
# restriction) - psycopg has no such option and raises on an unrecognized connect_args key,
# so this must stay scoped to sqlite rather than applying unconditionally.
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False} if _is_sqlite else {})
if DATABASE_URL.startswith("sqlite"): if _is_sqlite:
# WAL lets readers (cache lookups, library reads) proceed without blocking behind # WAL lets readers (cache lookups, library reads) proceed without blocking behind
# writers, instead of sqlite's default rollback-journal mode which locks both ways. # writers, instead of sqlite's default rollback-journal mode which locks both ways.
@event.listens_for(engine, "connect") @event.listens_for(engine, "connect")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment