"""phase36 — scheduled_inspections + inspections.scheduled_inspection_id Planned/recurring inspection assignments (facility + template + inspector + due date). Completed inspections link back via inspections.scheduled_inspection_id. Uses INFORMATION_SCHEMA existence checks — safe to re-run. """ revision = 'phase36_scheduled_insp' down_revision = 'phase35_issue_handler' branch_labels = None depends_on = None from alembic import op import sqlalchemy as sa def _table_exists(conn, table): return conn.execute(sa.text( "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES " "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :t" ), {"t": table}).scalar() > 0 def _column_exists(conn, table, column): return conn.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}).scalar() > 0 def upgrade(): bind = op.get_bind() if not _table_exists(bind, 'scheduled_inspections'): op.create_table( 'scheduled_inspections', sa.Column('id', sa.Integer, primary_key=True), sa.Column('facility_id', sa.Integer, sa.ForeignKey('facilities.id', ondelete='CASCADE'), nullable=False), sa.Column('template_id', sa.Integer, sa.ForeignKey('inspection_templates.id', ondelete='CASCADE'), nullable=False), sa.Column('inspector_id', sa.Integer, sa.ForeignKey('users.id', ondelete='SET NULL'), nullable=True), sa.Column('frequency', sa.Enum('once', 'daily', 'weekly', 'monthly'), nullable=False, server_default='once'), sa.Column('next_due_date', sa.Date, nullable=False), sa.Column('active', sa.Boolean, nullable=False, server_default='1'), sa.Column('notes', sa.Text, nullable=True), sa.Column('created_by', sa.Integer, sa.ForeignKey('users.id', ondelete='SET NULL'), nullable=True), sa.Column('created_at', sa.DateTime, nullable=False), sa.Column('last_completed_at', sa.DateTime, nullable=True), sa.Column('advance_notified', sa.Boolean, nullable=False, server_default='0'), sa.Column('due_notified', sa.Boolean, nullable=False, server_default='0'), sa.Column('overdue_notified', sa.Boolean, nullable=False, server_default='0'), ) op.create_index('ix_sched_insp_facility', 'scheduled_inspections', ['facility_id']) op.create_index('ix_sched_insp_inspector', 'scheduled_inspections', ['inspector_id']) op.create_index('ix_sched_insp_due', 'scheduled_inspections', ['next_due_date']) if not _column_exists(bind, 'inspections', 'scheduled_inspection_id'): op.execute(sa.text( "ALTER TABLE inspections ADD COLUMN scheduled_inspection_id INT NULL" )) op.execute(sa.text( "ALTER TABLE inspections ADD CONSTRAINT fk_inspection_scheduled " "FOREIGN KEY (scheduled_inspection_id) REFERENCES scheduled_inspections(id) " "ON DELETE SET NULL" )) def downgrade(): bind = op.get_bind() if _column_exists(bind, 'inspections', 'scheduled_inspection_id'): op.execute(sa.text("ALTER TABLE inspections DROP FOREIGN KEY fk_inspection_scheduled")) op.execute(sa.text("ALTER TABLE inspections DROP COLUMN scheduled_inspection_id")) if _table_exists(bind, 'scheduled_inspections'): op.drop_table('scheduled_inspections')