|
| 1 | +"""Add three-layer infrastructure model (credentials, services, slots). |
| 2 | +
|
| 3 | +Replaces the single organization_credentials table (which bundled |
| 4 | +secrets and config) with: |
| 5 | +- organization_credentials: provider-level encrypted secrets only |
| 6 | +- organization_services: non-secret config + credential reference |
| 7 | +- Organization slot columns: publishing_store_label, staging_store_label, |
| 8 | + cdn_service_label, dns_service_label |
| 9 | +
|
| 10 | +Revision ID: f4a5b6c7d8e9 |
| 11 | +Revises: e4f5a6b7c8d9 |
| 12 | +Create Date: 2026-03-19 00:00:00.000000+00:00 |
| 13 | +""" |
| 14 | + |
| 15 | +import sqlalchemy as sa |
| 16 | +from sqlalchemy.dialects import postgresql |
| 17 | + |
| 18 | +from alembic import op |
| 19 | + |
| 20 | +# revision identifiers, used by Alembic. |
| 21 | +revision: str = "f4a5b6c7d8e9" |
| 22 | +down_revision: str | None = "e4f5a6b7c8d9" |
| 23 | +branch_labels: str | None = None |
| 24 | +depends_on: str | None = None |
| 25 | + |
| 26 | + |
| 27 | +def upgrade() -> None: |
| 28 | + # --- Recreate organization_credentials with new schema --- |
| 29 | + # Drop old table (pre-production, no data to migrate) |
| 30 | + op.drop_table("organization_credentials") |
| 31 | + |
| 32 | + # Create new credentials table with provider-level auth |
| 33 | + op.create_table( |
| 34 | + "organization_credentials", |
| 35 | + sa.Column("id", sa.Integer, primary_key=True, autoincrement=True), |
| 36 | + sa.Column( |
| 37 | + "organization_id", |
| 38 | + sa.Integer, |
| 39 | + sa.ForeignKey("organizations.id", ondelete="CASCADE"), |
| 40 | + nullable=False, |
| 41 | + ), |
| 42 | + sa.Column("label", sa.String(128), nullable=False), |
| 43 | + sa.Column("provider", sa.String(32), nullable=False), |
| 44 | + sa.Column("encrypted_credentials", sa.LargeBinary, nullable=False), |
| 45 | + sa.Column( |
| 46 | + "date_created", |
| 47 | + sa.DateTime(timezone=True), |
| 48 | + server_default=sa.func.now(), |
| 49 | + nullable=False, |
| 50 | + ), |
| 51 | + sa.Column( |
| 52 | + "date_updated", |
| 53 | + sa.DateTime(timezone=True), |
| 54 | + server_default=sa.func.now(), |
| 55 | + nullable=False, |
| 56 | + ), |
| 57 | + sa.UniqueConstraint( |
| 58 | + "organization_id", "label", name="uq_org_credential_label" |
| 59 | + ), |
| 60 | + ) |
| 61 | + |
| 62 | + # --- Create organization_services table --- |
| 63 | + op.create_table( |
| 64 | + "organization_services", |
| 65 | + sa.Column("id", sa.Integer, primary_key=True, autoincrement=True), |
| 66 | + sa.Column( |
| 67 | + "organization_id", |
| 68 | + sa.Integer, |
| 69 | + sa.ForeignKey("organizations.id", ondelete="CASCADE"), |
| 70 | + nullable=False, |
| 71 | + ), |
| 72 | + sa.Column("label", sa.String(128), nullable=False), |
| 73 | + sa.Column("category", sa.String(32), nullable=False), |
| 74 | + sa.Column("provider", sa.String(32), nullable=False), |
| 75 | + sa.Column( |
| 76 | + "config", |
| 77 | + postgresql.JSONB, |
| 78 | + nullable=False, |
| 79 | + server_default="{}", |
| 80 | + ), |
| 81 | + sa.Column("credential_label", sa.String(128), nullable=False), |
| 82 | + sa.Column( |
| 83 | + "date_created", |
| 84 | + sa.DateTime(timezone=True), |
| 85 | + server_default=sa.func.now(), |
| 86 | + nullable=False, |
| 87 | + ), |
| 88 | + sa.Column( |
| 89 | + "date_updated", |
| 90 | + sa.DateTime(timezone=True), |
| 91 | + server_default=sa.func.now(), |
| 92 | + nullable=False, |
| 93 | + ), |
| 94 | + sa.UniqueConstraint( |
| 95 | + "organization_id", "label", name="uq_org_service_label" |
| 96 | + ), |
| 97 | + ) |
| 98 | + |
| 99 | + # --- Update organization slot columns --- |
| 100 | + # Remove old credential label columns |
| 101 | + op.drop_column("organizations", "publishing_credential_label") |
| 102 | + op.drop_column("organizations", "staging_credential_label") |
| 103 | + |
| 104 | + # Add new service slot columns |
| 105 | + op.add_column( |
| 106 | + "organizations", |
| 107 | + sa.Column("publishing_store_label", sa.String(128), nullable=True), |
| 108 | + ) |
| 109 | + op.add_column( |
| 110 | + "organizations", |
| 111 | + sa.Column("staging_store_label", sa.String(128), nullable=True), |
| 112 | + ) |
| 113 | + op.add_column( |
| 114 | + "organizations", |
| 115 | + sa.Column("cdn_service_label", sa.String(128), nullable=True), |
| 116 | + ) |
| 117 | + op.add_column( |
| 118 | + "organizations", |
| 119 | + sa.Column("dns_service_label", sa.String(128), nullable=True), |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +def downgrade() -> None: |
| 124 | + # Remove new slot columns |
| 125 | + op.drop_column("organizations", "dns_service_label") |
| 126 | + op.drop_column("organizations", "cdn_service_label") |
| 127 | + op.drop_column("organizations", "staging_store_label") |
| 128 | + op.drop_column("organizations", "publishing_store_label") |
| 129 | + |
| 130 | + # Restore old credential label columns |
| 131 | + op.add_column( |
| 132 | + "organizations", |
| 133 | + sa.Column( |
| 134 | + "publishing_credential_label", sa.String(128), nullable=True |
| 135 | + ), |
| 136 | + ) |
| 137 | + op.add_column( |
| 138 | + "organizations", |
| 139 | + sa.Column("staging_credential_label", sa.String(128), nullable=True), |
| 140 | + ) |
| 141 | + |
| 142 | + # Drop services table |
| 143 | + op.drop_table("organization_services") |
| 144 | + |
| 145 | + # Recreate old credentials table |
| 146 | + op.drop_table("organization_credentials") |
| 147 | + op.create_table( |
| 148 | + "organization_credentials", |
| 149 | + sa.Column("id", sa.Integer, primary_key=True, autoincrement=True), |
| 150 | + sa.Column( |
| 151 | + "organization_id", |
| 152 | + sa.Integer, |
| 153 | + sa.ForeignKey("organizations.id", ondelete="CASCADE"), |
| 154 | + nullable=False, |
| 155 | + ), |
| 156 | + sa.Column("label", sa.String(128), nullable=False), |
| 157 | + sa.Column("service_type", sa.String(32), nullable=False), |
| 158 | + sa.Column("encrypted_credential", sa.LargeBinary, nullable=False), |
| 159 | + sa.Column( |
| 160 | + "date_created", |
| 161 | + sa.DateTime(timezone=True), |
| 162 | + server_default=sa.func.now(), |
| 163 | + nullable=False, |
| 164 | + ), |
| 165 | + sa.Column( |
| 166 | + "date_updated", |
| 167 | + sa.DateTime(timezone=True), |
| 168 | + server_default=sa.func.now(), |
| 169 | + nullable=False, |
| 170 | + ), |
| 171 | + sa.UniqueConstraint( |
| 172 | + "organization_id", "label", name="uq_org_credential_label" |
| 173 | + ), |
| 174 | + ) |
0 commit comments