48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
"""phase20 — inspector contract assignments
|
|
|
|
Adds inspector_assignments table so each inspector can be scoped to one or
|
|
more contracts (projects). Inspectors with no assignments see nothing.
|
|
|
|
Safe to re-run — uses INFORMATION_SCHEMA existence check.
|
|
"""
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision = 'phase20_inspector_assignments'
|
|
down_revision = 'phase19_issue_mobile_photos'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _table_exists(bind, table):
|
|
result = bind.execute(sa.text(
|
|
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES "
|
|
"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :t"
|
|
), {"t": table})
|
|
return result.scalar() > 0
|
|
|
|
|
|
def upgrade():
|
|
bind = op.get_bind()
|
|
if not _table_exists(bind, 'inspector_assignments'):
|
|
op.create_table(
|
|
'inspector_assignments',
|
|
sa.Column('id', sa.Integer(), nullable=False, autoincrement=True),
|
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
|
sa.Column('project_id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ondelete='CASCADE'),
|
|
sa.UniqueConstraint('user_id', 'project_id', name='uq_inspector_project'),
|
|
)
|
|
op.create_index('ix_inspector_assignments_user_id',
|
|
'inspector_assignments', ['user_id'])
|
|
|
|
|
|
def downgrade():
|
|
bind = op.get_bind()
|
|
if _table_exists(bind, 'inspector_assignments'):
|
|
op.drop_table('inspector_assignments')
|