schema.sql dropped and re-added three CHECK constraints on every start, so an older image narrowed the lists and failed on rows written by a newer one (an Expedição user, for example). They are now created only when missing, and app/migrate.py applies timestamped files from app/migrations/ once each, recording them in dtf_local.migrations; an older image skips names it does not know. The first migration sets today's full lists. A CI step rehearses the rollback on the real database inside a rolled-back transaction. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
27 lines
950 B
Python
27 lines
950 B
Python
import os
|
|
from pathlib import Path
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
|
|
from .. import migrate
|
|
|
|
def connect():
|
|
# Production passes credentials as discrete libpq fields. This avoids
|
|
# treating characters in a generated password as URL syntax. Local and
|
|
# test environments retain DATABASE_URL compatibility.
|
|
if os.environ.get('DATABASE_HOST'):
|
|
return psycopg.connect(
|
|
host=os.environ['DATABASE_HOST'],
|
|
dbname=os.environ['DATABASE_NAME'],
|
|
user=os.environ['DATABASE_USER'],
|
|
password=os.environ['DATABASE_PASSWORD'],
|
|
row_factory=dict_row,
|
|
)
|
|
return psycopg.connect(os.environ['DATABASE_URL'], row_factory=dict_row)
|
|
|
|
def initialize():
|
|
with connect() as c:
|
|
# The schema lives at the package root, one level up from core/.
|
|
c.execute((Path(__file__).resolve().parent.parent / 'schema.sql').read_text())
|
|
migrate.apply(c)
|