Restructure backend and frontend modules; add story, library, and editor schema migrations. Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews. Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
120 lines
5.3 KiB
Python
120 lines
5.3 KiB
Python
from dataclasses import dataclass, field
|
|
from sqlalchemy import inspect, text, Boolean, String, Text
|
|
from sqlalchemy.dialects.mysql import TINYINT
|
|
|
|
|
|
@dataclass
|
|
class SchemaDiff:
|
|
differences: list[str] = field(default_factory=list)
|
|
|
|
@property
|
|
def matches(self):
|
|
return not self.differences
|
|
|
|
|
|
def compatible_type(expected, observed, dialect):
|
|
# Reflection includes inherited database collation; generic VARCHAR does not
|
|
# assert one. Explicit model collation/charset still remains a hard contract.
|
|
if isinstance(expected, Boolean) and dialect.name == "mysql":
|
|
return (
|
|
isinstance(observed, Boolean)
|
|
or isinstance(observed, TINYINT)
|
|
and observed.display_width == 1
|
|
)
|
|
if isinstance(expected, String) and isinstance(observed, String):
|
|
if isinstance(expected, Text) != isinstance(observed, Text):
|
|
return False
|
|
if expected.length != observed.length:
|
|
return False
|
|
for option in ("collation", "charset"):
|
|
desired = getattr(expected, option, None)
|
|
if desired is not None and desired != getattr(observed, option, None):
|
|
return False
|
|
return True
|
|
return expected.compile(dialect=dialect).upper() == observed.compile(dialect=dialect).upper()
|
|
|
|
|
|
def schema_preflight(connection, metadata):
|
|
inspector = inspect(connection)
|
|
diff = SchemaDiff()
|
|
tables = set(inspector.get_table_names())
|
|
for table in metadata.sorted_tables:
|
|
if table.name not in tables:
|
|
diff.differences.append("Missing table: " + table.name)
|
|
continue
|
|
if connection.dialect.name == "mysql":
|
|
options = inspector.get_table_options(table.name)
|
|
charset = (
|
|
options.get("mysql_default charset")
|
|
or options.get("mysql_default_charset")
|
|
or options.get("mysql_charset")
|
|
)
|
|
if charset and charset != "utf8mb4":
|
|
diff.differences.append("Unexpected charset: " + table.name)
|
|
actual = {c["name"]: c for c in inspector.get_columns(table.name)}
|
|
if set(actual) != set(table.columns.keys()):
|
|
diff.differences.append("Unexpected/missing columns: " + table.name)
|
|
if tuple(inspector.get_pk_constraint(table.name)["constrained_columns"]) != tuple(
|
|
c.name for c in table.primary_key.columns
|
|
):
|
|
diff.differences.append("Primary key: " + table.name)
|
|
for col in table.columns:
|
|
current = actual.get(col.name)
|
|
if not current:
|
|
diff.differences.append(f"Missing column: {table.name}.{col.name}")
|
|
continue
|
|
expected = col.type.compile(dialect=connection.dialect)
|
|
observed = current["type"].compile(dialect=connection.dialect)
|
|
if not compatible_type(col.type, current["type"], connection.dialect):
|
|
diff.differences.append(
|
|
f"Column type: {table.name}.{col.name}: {observed} != {expected}"
|
|
)
|
|
if not col.primary_key and col.nullable != current["nullable"]:
|
|
diff.differences.append(f"Nullability: {table.name}.{col.name}")
|
|
expected_fks = {
|
|
(
|
|
tuple(fk.parent.name for fk in constraint.elements),
|
|
constraint.elements[0].column.table.name,
|
|
tuple(fk.column.name for fk in constraint.elements),
|
|
)
|
|
for constraint in table.foreign_key_constraints
|
|
}
|
|
actual_fks = {
|
|
(tuple(fk["constrained_columns"]), fk["referred_table"], tuple(fk["referred_columns"]))
|
|
for fk in inspector.get_foreign_keys(table.name)
|
|
}
|
|
if not expected_fks <= actual_fks:
|
|
diff.differences.append("Missing foreign key: " + table.name)
|
|
expected_unique = {
|
|
tuple(c.name for c in constraint.columns)
|
|
for constraint in table.constraints
|
|
if constraint.__class__.__name__ == "UniqueConstraint"
|
|
}
|
|
observed_unique = {
|
|
tuple(c["column_names"]) for c in inspector.get_unique_constraints(table.name)
|
|
}
|
|
observed_unique |= {
|
|
tuple(c["column_names"]) for c in inspector.get_indexes(table.name) if c.get("unique")
|
|
}
|
|
if not expected_unique <= observed_unique:
|
|
diff.differences.append("Missing unique constraint: " + table.name)
|
|
expected_indexes = {tuple(c.name for c in index.columns) for index in table.indexes}
|
|
observed_indexes = {tuple(c["column_names"]) for c in inspector.get_indexes(table.name)}
|
|
if not expected_indexes <= observed_indexes:
|
|
diff.differences.append("Missing index: " + table.name)
|
|
return diff
|
|
|
|
|
|
def check_schema_revision(connection, supported):
|
|
inspector = inspect(connection)
|
|
if "alembic_version" not in inspector.get_table_names():
|
|
raise RuntimeError("Schema is unversioned; preflight and explicit migration required")
|
|
revisions = set(connection.execute(text("SELECT version_num FROM alembic_version")).scalars())
|
|
if len(revisions) != 1 or not revisions <= supported:
|
|
raise RuntimeError("Unsupported schema revision")
|
|
from app.models import Base
|
|
|
|
diff = schema_preflight(connection, Base.metadata)
|
|
if not diff.matches:
|
|
raise RuntimeError("Schema drift: " + "; ".join(diff.differences))
|