45 lines
1.6 KiB
Bash
45 lines
1.6 KiB
Bash
#!/bin/sh
|
|
set -eu
|
|
|
|
echo "Checking migration baseline..."
|
|
python - <<'PY'
|
|
from sqlalchemy import create_engine, inspect, text
|
|
|
|
from gitea_codex_bot.config import get_settings
|
|
|
|
settings = get_settings()
|
|
engine = create_engine(settings.sqlalchemy_url)
|
|
|
|
with engine.connect() as conn:
|
|
inspector = inspect(conn)
|
|
tables = set(inspector.get_table_names())
|
|
|
|
has_alembic_version = "alembic_version" in tables
|
|
has_review_jobs = "review_jobs" in tables
|
|
has_webhook_events = "webhook_events" in tables
|
|
stamped_revision = None
|
|
|
|
if has_alembic_version:
|
|
row = conn.execute(text("SELECT version_num FROM alembic_version LIMIT 1")).fetchone()
|
|
if row and row[0]:
|
|
stamped_revision = row[0]
|
|
|
|
if (not has_alembic_version or not stamped_revision) and (has_review_jobs or has_webhook_events):
|
|
revision = "0001_initial"
|
|
if has_review_jobs:
|
|
columns = {c["name"] for c in inspector.get_columns("review_jobs")}
|
|
if "trigger_comment_body" in columns:
|
|
revision = "0002_trigger_comment_body"
|
|
conn.execute(text("CREATE TABLE IF NOT EXISTS alembic_version (version_num VARCHAR(32) NOT NULL, PRIMARY KEY (version_num))"))
|
|
conn.execute(text("DELETE FROM alembic_version"))
|
|
conn.execute(text("INSERT INTO alembic_version (version_num) VALUES (:revision)"), {"revision": revision})
|
|
conn.commit()
|
|
print(f"Stamped legacy database at revision {revision}")
|
|
PY
|
|
|
|
echo "Running database migrations..."
|
|
alembic upgrade head
|
|
|
|
echo "Starting API server..."
|
|
exec uvicorn gitea_codex_bot.main:app --host 0.0.0.0 --port 8000
|