fix: a rollback never stops the database from starting; schema changes are migration files
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>
This commit is contained in:
59
tests/migrations_test.py
Normal file
59
tests/migrations_test.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""Rollback safety on the real database: an image at or after the timestamped
|
||||
migrations starts on data written under a newer, wider CHECK list.
|
||||
|
||||
Uses the database administrator's connection, so it runs in the db-init
|
||||
container: docker compose -f compose.local.yaml run --rm --no-deps db-init \
|
||||
python -m tests.migrations_test
|
||||
|
||||
Everything happens in one transaction that is rolled back at the end, so the
|
||||
widening, its migration record and the test operator never remain.
|
||||
"""
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from app import migrate
|
||||
from app.bootstrap import admin_connect
|
||||
|
||||
SCHEMA = Path(__file__).resolve().parent.parent.joinpath('app', 'schema.sql')
|
||||
WIDEN = """ALTER TABLE dtf_local.operators DROP CONSTRAINT IF EXISTS operators_role;
|
||||
ALTER TABLE dtf_local.operators ADD CONSTRAINT operators_role
|
||||
CHECK (role IN ('superadmin','admin','gestao','designer','operador','revisor','vendedor','financeiro','expedicao','tester'));
|
||||
"""
|
||||
|
||||
|
||||
def check(condition, message):
|
||||
if not condition:
|
||||
raise AssertionError(message)
|
||||
print('PASS:', message)
|
||||
|
||||
|
||||
def main():
|
||||
c = admin_connect()
|
||||
try:
|
||||
c.execute(SCHEMA.read_text())
|
||||
migrated = migrate.apply(c)
|
||||
check(True, f'the real schema and migrations run ({", ".join(migrated) or "none pending"})')
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
name = '209901010000_widen-operators-role.sql'
|
||||
Path(folder, name).write_text(WIDEN)
|
||||
applied = migrate.apply(c, folder)
|
||||
check(applied == [name], 'a temporary migration widens operators_role')
|
||||
email = f'tester-{uuid4().hex[:8]}@example.test'
|
||||
c.execute("INSERT INTO dtf_local.operators(id,email,password_hash,role) VALUES(%s,%s,'test','tester')",
|
||||
(uuid4(), email))
|
||||
check(True, 'an operator with the newer role is accepted')
|
||||
# The rollback rehearsal: an older image at or after the migrations
|
||||
# change runs its schema.sql and its own folder, with no later file,
|
||||
# and must start on the current data without narrowing the list.
|
||||
c.execute(SCHEMA.read_text())
|
||||
migrate.apply(c)
|
||||
check(True, 'the older image starts and leaves the widened list alone')
|
||||
finally:
|
||||
c.rollback()
|
||||
c.close()
|
||||
print('migrations_test OK')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user