Backend
This commit is contained in:
@@ -84,6 +84,28 @@ def ensure_tenant_columns(engine: Engine) -> tuple[str, ...]:
|
||||
return tuple(added_columns)
|
||||
|
||||
|
||||
# Ad-hoc column additions for tables that pre-existed before a column was
|
||||
# introduced on the model. Each entry is (table, column, DDL fragment).
|
||||
_LEGACY_COLUMN_PATCHES: tuple[tuple[str, str, str], ...] = (
|
||||
("users", "password_hash", "VARCHAR(255)"),
|
||||
)
|
||||
|
||||
|
||||
def ensure_legacy_columns(engine: Engine) -> tuple[str, ...]:
|
||||
added: list[str] = []
|
||||
for table_name, column_name, ddl in _LEGACY_COLUMN_PATCHES:
|
||||
if not _table_exists(engine, table_name):
|
||||
continue
|
||||
if _has_column(engine, table_name, column_name):
|
||||
continue
|
||||
with engine.begin() as connection:
|
||||
connection.execute(
|
||||
text(f"ALTER TABLE {table_name} ADD COLUMN {column_name} {ddl}")
|
||||
)
|
||||
added.append(f"{table_name}.{column_name}")
|
||||
return tuple(added)
|
||||
|
||||
|
||||
def sync_tenant_ids(engine: Engine) -> dict[str, int]:
|
||||
existing_tables = set(inspect(engine).get_table_names())
|
||||
|
||||
@@ -339,5 +361,5 @@ def sync_tenant_ids(engine: Engine) -> dict[str, int]:
|
||||
|
||||
def bootstrap_schema(engine: Engine, metadata: MetaData) -> MigrationReport:
|
||||
created_tables = ensure_metadata_tables(engine, metadata)
|
||||
added_columns = ensure_tenant_columns(engine)
|
||||
added_columns = ensure_tenant_columns(engine) + ensure_legacy_columns(engine)
|
||||
return MigrationReport(created_tables=created_tables, added_columns=added_columns)
|
||||
|
||||
Reference in New Issue
Block a user