"""phase21 — template active flag Adds `active` boolean column to `inspection_templates` so templates can be deactivated without deletion. Inactive templates are hidden from the inspection-start form but remain accessible in the template management UI. Safe to re-run — uses INFORMATION_SCHEMA column existence check. """ import sqlalchemy as sa from alembic import op revision = 'phase21_template_active' down_revision = 'phase20_inspector_assignments' branch_labels = None depends_on = None def _column_exists(bind, table, column): result = bind.execute(sa.text( "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS " "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :t AND COLUMN_NAME = :c" ), {"t": table, "c": column}) return result.scalar() > 0 def upgrade(): bind = op.get_bind() if not _column_exists(bind, 'inspection_templates', 'active'): op.add_column( 'inspection_templates', sa.Column('active', sa.Boolean(), nullable=False, server_default=sa.true()) ) def downgrade(): bind = op.get_bind() if _column_exists(bind, 'inspection_templates', 'active'): op.drop_column('inspection_templates', 'active')