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>
77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
"""The migration runner: file names, order and recorded names.
|
|
|
|
Run from the repository root: python3 -m unittest tests.test_migrate
|
|
"""
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from app import migrate
|
|
|
|
|
|
class Cursor:
|
|
def __init__(self, row=None):
|
|
self.row = row
|
|
|
|
def fetchone(self):
|
|
return self.row
|
|
|
|
|
|
class FakeConnection:
|
|
"""Records the SQL it is asked to run and the names already recorded."""
|
|
|
|
def __init__(self, recorded=()):
|
|
self.recorded = set(recorded)
|
|
self.calls = []
|
|
|
|
def execute(self, query, params=()):
|
|
self.calls.append((query, params))
|
|
if query.startswith('SELECT 1 FROM dtf_local.migrations'):
|
|
return Cursor((1,) if params[0] in self.recorded else None)
|
|
if query.startswith('INSERT INTO dtf_local.migrations'):
|
|
self.recorded.add(params[0])
|
|
return Cursor()
|
|
|
|
@property
|
|
def queries(self):
|
|
return [query for query, _ in self.calls]
|
|
|
|
|
|
class MigrateTest(unittest.TestCase):
|
|
def folder(self, names):
|
|
temp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(temp.cleanup)
|
|
for name in names:
|
|
Path(temp.name, name).write_text(f'-- {name}\n')
|
|
return temp.name
|
|
|
|
def test_runs_new_files_in_name_order(self):
|
|
directory = self.folder(['202611091202_b.sql', '202611091201_a.sql'])
|
|
c = FakeConnection()
|
|
self.assertEqual(migrate.apply(c, directory), ['202611091201_a.sql', '202611091202_b.sql'])
|
|
self.assertEqual(c.queries[0], 'SELECT pg_advisory_xact_lock(%s)')
|
|
ran = [query for query in c.queries if query.startswith('-- ')]
|
|
self.assertEqual(ran, ['-- 202611091201_a.sql\n', '-- 202611091202_b.sql\n'])
|
|
|
|
def test_recorded_files_are_skipped(self):
|
|
directory = self.folder(['202611091201_a.sql', '202611091202_b.sql'])
|
|
c = FakeConnection({'202611091201_a.sql'})
|
|
self.assertEqual(migrate.apply(c, directory), ['202611091202_b.sql'])
|
|
self.assertNotIn('-- 202611091201_a.sql\n', c.queries)
|
|
|
|
def test_a_recorded_name_without_a_file_is_ignored(self):
|
|
directory = self.folder(['202611091201_a.sql'])
|
|
c = FakeConnection({'209901010000_from-a-newer-image.sql'})
|
|
self.assertEqual(migrate.apply(c, directory), ['202611091201_a.sql'])
|
|
|
|
def test_an_unexpected_file_raises(self):
|
|
# A typo must stop the start, never be skipped as if it were applied.
|
|
for name in ('notes.txt', '20261109120_short.sql', '202611091201_Two-words.sql'):
|
|
with self.subTest(name=name):
|
|
with self.assertRaises(ValueError):
|
|
migrate.apply(FakeConnection(), self.folder([name]))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|