|
| 1 | +"""security_level_integer - Convert security_level from text to integer |
| 2 | +
|
| 3 | +Revision ID: 003 |
| 4 | +Revises: 002 |
| 5 | +Create Date: 2025-01-21 00:00:00.000000 |
| 6 | +
|
| 7 | +This migration converts security_level from text-based values to integers: |
| 8 | +- 'insecure' -> 0 (Lowest security) |
| 9 | +- 'sensitive' -> 1 (Medium security) |
| 10 | +- 'secure' -> 2 (Highest security) |
| 11 | +
|
| 12 | +Affected tables: |
| 13 | +- providers: security_level NOT NULL with default 1 |
| 14 | +- models: security_level NULLABLE |
| 15 | +""" |
| 16 | +from typing import Sequence, Union |
| 17 | + |
| 18 | +from alembic import op |
| 19 | +import sqlalchemy as sa |
| 20 | + |
| 21 | +# revision identifiers, used by Alembic. |
| 22 | +revision: str = '003' |
| 23 | +down_revision: Union[str, None] = '002' |
| 24 | +branch_labels: Union[str, Sequence[str], None] = None |
| 25 | +depends_on: Union[str, Sequence[str], None] = None |
| 26 | + |
| 27 | + |
| 28 | +def upgrade() -> None: |
| 29 | + # ========================================================================== |
| 30 | + # 1. Providers table: Convert security_level from VARCHAR to INTEGER |
| 31 | + # ========================================================================== |
| 32 | + |
| 33 | + # Drop existing check constraint |
| 34 | + op.execute("ALTER TABLE providers DROP CONSTRAINT IF EXISTS check_security_level;") |
| 35 | + |
| 36 | + # Add temporary column for integer values |
| 37 | + op.add_column('providers', sa.Column('security_level_new', sa.Integer(), nullable=True)) |
| 38 | + |
| 39 | + # Migrate data: 'insecure' -> 0, 'sensitive' -> 1, 'secure' -> 2 |
| 40 | + op.execute(""" |
| 41 | + UPDATE providers SET security_level_new = CASE |
| 42 | + WHEN security_level = 'insecure' THEN 0 |
| 43 | + WHEN security_level = 'sensitive' THEN 1 |
| 44 | + WHEN security_level = 'secure' THEN 2 |
| 45 | + ELSE 1 |
| 46 | + END; |
| 47 | + """) |
| 48 | + |
| 49 | + # Drop old column |
| 50 | + op.drop_column('providers', 'security_level') |
| 51 | + |
| 52 | + # Rename new column to security_level |
| 53 | + op.execute("ALTER TABLE providers RENAME COLUMN security_level_new TO security_level;") |
| 54 | + |
| 55 | + # Set NOT NULL and default |
| 56 | + op.execute("ALTER TABLE providers ALTER COLUMN security_level SET NOT NULL;") |
| 57 | + op.execute("ALTER TABLE providers ALTER COLUMN security_level SET DEFAULT 1;") |
| 58 | + |
| 59 | + # Add check constraint for integer values |
| 60 | + op.execute(""" |
| 61 | + ALTER TABLE providers ADD CONSTRAINT check_security_level |
| 62 | + CHECK (security_level IN (0, 1, 2)); |
| 63 | + """) |
| 64 | + |
| 65 | + # Recreate index for security_level |
| 66 | + op.execute("DROP INDEX IF EXISTS idx_providers_security;") |
| 67 | + op.execute("CREATE INDEX idx_providers_security ON providers (security_level);") |
| 68 | + |
| 69 | + # ========================================================================== |
| 70 | + # 2. Models table: Convert security_level from VARCHAR to INTEGER (nullable) |
| 71 | + # ========================================================================== |
| 72 | + |
| 73 | + # Add temporary column for integer values |
| 74 | + op.add_column('models', sa.Column('security_level_new', sa.Integer(), nullable=True)) |
| 75 | + |
| 76 | + # Migrate data: 'insecure' -> 0, 'sensitive' -> 1, 'secure' -> 2, NULL stays NULL |
| 77 | + op.execute(""" |
| 78 | + UPDATE models SET security_level_new = CASE |
| 79 | + WHEN security_level = 'insecure' THEN 0 |
| 80 | + WHEN security_level = 'sensitive' THEN 1 |
| 81 | + WHEN security_level = 'secure' THEN 2 |
| 82 | + ELSE NULL |
| 83 | + END; |
| 84 | + """) |
| 85 | + |
| 86 | + # Drop old column |
| 87 | + op.drop_column('models', 'security_level') |
| 88 | + |
| 89 | + # Rename new column to security_level |
| 90 | + op.execute("ALTER TABLE models RENAME COLUMN security_level_new TO security_level;") |
| 91 | + |
| 92 | + # Add check constraint for integer values (allowing NULL) |
| 93 | + op.execute(""" |
| 94 | + ALTER TABLE models ADD CONSTRAINT check_model_security_level |
| 95 | + CHECK (security_level IS NULL OR security_level IN (0, 1, 2)); |
| 96 | + """) |
| 97 | + |
| 98 | + |
| 99 | +def downgrade() -> None: |
| 100 | + # ========================================================================== |
| 101 | + # 1. Models table: Revert to VARCHAR |
| 102 | + # ========================================================================== |
| 103 | + |
| 104 | + # Drop check constraint |
| 105 | + op.execute("ALTER TABLE models DROP CONSTRAINT IF EXISTS check_model_security_level;") |
| 106 | + |
| 107 | + # Add temporary column for text values |
| 108 | + op.add_column('models', sa.Column('security_level_old', sa.String(50), nullable=True)) |
| 109 | + |
| 110 | + # Migrate data back: 0 -> 'insecure', 1 -> 'sensitive', 2 -> 'secure' |
| 111 | + op.execute(""" |
| 112 | + UPDATE models SET security_level_old = CASE |
| 113 | + WHEN security_level = 0 THEN 'insecure' |
| 114 | + WHEN security_level = 1 THEN 'sensitive' |
| 115 | + WHEN security_level = 2 THEN 'secure' |
| 116 | + ELSE NULL |
| 117 | + END; |
| 118 | + """) |
| 119 | + |
| 120 | + # Drop integer column |
| 121 | + op.drop_column('models', 'security_level') |
| 122 | + |
| 123 | + # Rename old column back |
| 124 | + op.execute("ALTER TABLE models RENAME COLUMN security_level_old TO security_level;") |
| 125 | + |
| 126 | + # ========================================================================== |
| 127 | + # 2. Providers table: Revert to VARCHAR |
| 128 | + # ========================================================================== |
| 129 | + |
| 130 | + # Drop check constraint and index |
| 131 | + op.execute("ALTER TABLE providers DROP CONSTRAINT IF EXISTS check_security_level;") |
| 132 | + op.execute("DROP INDEX IF EXISTS idx_providers_security;") |
| 133 | + |
| 134 | + # Add temporary column for text values |
| 135 | + op.add_column('providers', sa.Column('security_level_old', sa.String(20), nullable=True)) |
| 136 | + |
| 137 | + # Migrate data back: 0 -> 'insecure', 1 -> 'sensitive', 2 -> 'secure' |
| 138 | + op.execute(""" |
| 139 | + UPDATE providers SET security_level_old = CASE |
| 140 | + WHEN security_level = 0 THEN 'insecure' |
| 141 | + WHEN security_level = 1 THEN 'sensitive' |
| 142 | + WHEN security_level = 2 THEN 'secure' |
| 143 | + ELSE 'sensitive' |
| 144 | + END; |
| 145 | + """) |
| 146 | + |
| 147 | + # Drop integer column |
| 148 | + op.drop_column('providers', 'security_level') |
| 149 | + |
| 150 | + # Rename old column back |
| 151 | + op.execute("ALTER TABLE providers RENAME COLUMN security_level_old TO security_level;") |
| 152 | + |
| 153 | + # Set NOT NULL |
| 154 | + op.execute("ALTER TABLE providers ALTER COLUMN security_level SET NOT NULL;") |
| 155 | + |
| 156 | + # Recreate original check constraint |
| 157 | + op.execute(""" |
| 158 | + ALTER TABLE providers ADD CONSTRAINT check_security_level |
| 159 | + CHECK (security_level IN ('secure', 'sensitive', 'insecure')); |
| 160 | + """) |
| 161 | + |
| 162 | + # Recreate index |
| 163 | + op.execute("CREATE INDEX idx_providers_security ON providers (security_level);") |
0 commit comments