add blocker presets
This commit is contained in:
12 files changed
+426
-83
No files matched your search
+27
-2
@@ -11,7 +11,7 @@ from __future__ import annotations
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import create_engine, inspect, text
|
||||
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
||||
|
||||
from app.config import env_settings
|
||||
@@ -32,11 +32,36 @@ engine = create_engine(env_settings().database_url, **_engine_kwargs(env_setting
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
|
||||
|
||||
|
||||
# Columns a refactor later dropped from the model but, with no migration tool
|
||||
# in this project, left behind on any database that was already running —
|
||||
# still `NOT NULL` with no server-side default, so every insert that no
|
||||
# longer sets them fails. `create_all` only ever adds; this is the other half.
|
||||
_DEAD_COLUMNS: dict[str, tuple[str, ...]] = {
|
||||
"tasks": ("completed_here",),
|
||||
}
|
||||
|
||||
|
||||
def _drop_dead_columns() -> None:
|
||||
inspector = inspect(engine)
|
||||
existing_tables = set(inspector.get_table_names())
|
||||
with engine.begin() as conn:
|
||||
for table, columns in _DEAD_COLUMNS.items():
|
||||
if table not in existing_tables:
|
||||
continue
|
||||
existing_columns = {c["name"] for c in inspector.get_columns(table)}
|
||||
for column in columns:
|
||||
if column in existing_columns:
|
||||
conn.execute(text(f'ALTER TABLE {table} DROP COLUMN {column}'))
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
"""Create any missing tables. The schema is additive, so this is enough."""
|
||||
"""Create any missing tables. The schema is additive, so this is enough —
|
||||
plus the one-time cleanup above for columns a refactor removed from the
|
||||
model."""
|
||||
from app import models # noqa: F401 (register the mappers)
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
_drop_dead_columns()
|
||||
|
||||
|
||||
@contextmanager
|
||||
|
||||
Reference in new issue
Block a user