"""navi_users gains columns only through the boot-time migration list.
An existing database never re-runs CREATE TABLE (IF NOT EXISTS is a no-op
there), so a column added to the DDL but forgotten in
_NAVI_USERS_MIGRATION_COLUMNS is silently absent on every deployed instance
while working fine on a fresh one.
"""
import re
from navi.auth._ddl import _DDL, _NAVI_USERS_MIGRATION_COLUMNS
# The columns navi_users was created with; everything else has to be migratable.
_ORIGINAL_COLUMNS = {
"id", "email", "display_name", "role", "permissions", "created_at", "updated_at",
}
def _declared_columns() -> set[str]:
block = re.search(r"CREATE TABLE IF NOT EXISTS navi_users \((.*?)\n\);", _DDL, re.S)
assert block, "navi_users is no longer declared in _DDL — update this test"
return {line.split()[0] for line in block.group(1).splitlines() if line.strip()}
def test_every_migrated_column_exists_in_the_ddl():
declared = _declared_columns()
for name, _type in _NAVI_USERS_MIGRATION_COLUMNS:
assert name in declared, f"{name} is migrated but never declared"
def test_every_added_column_is_migratable():
migrated = {name for name, _type in _NAVI_USERS_MIGRATION_COLUMNS}
assert _declared_columns() - _ORIGINAL_COLUMNS <= migrated