Self-contained ingestion module (mirrors fleettickets) for the WhatsApp fuel-record feed in the rustfs `fuel` bucket: - import_fuel.py — snapshot/changes/file modes, raw-jsonb upsert on id - migrations/01_fuel_schema.sql — fuel schema, plate/fuel-type/department normalizers, trigger-derived columns, reporting.v_fuel_fills + v_fuel_efficiency, grafana_ro grants - s3util.py / shared.py / run_migrations.py — rustfs client + DB helpers - docs/plan.html — implementation plan Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""
|
|
run_migrations.py — fleetfuel · apply SQL migrations in order.
|
|
|
|
Applies migrations/*.sql (lexical order) against DATABASE_URL, tracking applied
|
|
files in fuel.schema_migrations. Migrations are idempotent, so re-running is
|
|
safe. Run: `python run_migrations.py`.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import glob
|
|
import os
|
|
|
|
import psycopg2
|
|
|
|
MIG_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "migrations")
|
|
|
|
|
|
def main() -> None:
|
|
dsn = os.environ.get("DATABASE_URL")
|
|
if not dsn:
|
|
raise SystemExit("DATABASE_URL is not set")
|
|
conn = psycopg2.connect(dsn)
|
|
conn.autocommit = False
|
|
try:
|
|
with conn.cursor() as cur:
|
|
cur.execute("CREATE SCHEMA IF NOT EXISTS fuel")
|
|
cur.execute(
|
|
"CREATE TABLE IF NOT EXISTS fuel.schema_migrations "
|
|
"(filename text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())"
|
|
)
|
|
conn.commit()
|
|
cur.execute("SELECT filename FROM fuel.schema_migrations")
|
|
applied = {r[0] for r in cur.fetchall()}
|
|
|
|
for path in sorted(glob.glob(os.path.join(MIG_DIR, "*.sql"))):
|
|
fn = os.path.basename(path)
|
|
if fn in applied:
|
|
print(f" skip {fn}")
|
|
continue
|
|
print(f" apply {fn}")
|
|
with open(path, encoding="utf-8") as f:
|
|
cur.execute(f.read())
|
|
cur.execute(
|
|
"INSERT INTO fuel.schema_migrations (filename) VALUES (%s) "
|
|
"ON CONFLICT DO NOTHING",
|
|
(fn,),
|
|
)
|
|
conn.commit()
|
|
print("migrations up to date.")
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|