|
| 1 | +"""create_model_registry_tables |
| 2 | +
|
| 3 | +Revision ID: a2f7b3c8d901 |
| 4 | +Revises: e1165ebcef61 |
| 5 | +Create Date: 2026-02-01 10:00:00.000000 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +from alembic import op |
| 12 | +import sqlalchemy as sa |
| 13 | +from sqlalchemy.dialects import postgresql |
| 14 | + |
| 15 | +# revision identifiers, used by Alembic. |
| 16 | +revision: str = "a2f7b3c8d901" |
| 17 | +down_revision: Union[str, None] = "e1165ebcef61" |
| 18 | +branch_labels: Union[str, Sequence[str], None] = None |
| 19 | +depends_on: Union[str, Sequence[str], None] = None |
| 20 | + |
| 21 | + |
| 22 | +def upgrade() -> None: |
| 23 | + """Apply migration - create model_run and deployment_alias tables.""" |
| 24 | + # Create model_run table |
| 25 | + op.create_table( |
| 26 | + "model_run", |
| 27 | + sa.Column("id", sa.Integer(), nullable=False), |
| 28 | + sa.Column("run_id", sa.String(length=32), nullable=False), |
| 29 | + sa.Column("status", sa.String(length=20), nullable=False, server_default="pending"), |
| 30 | + # Model configuration |
| 31 | + sa.Column("model_type", sa.String(length=50), nullable=False), |
| 32 | + sa.Column("model_config", postgresql.JSONB(astext_type=sa.Text()), nullable=False), |
| 33 | + sa.Column("feature_config", postgresql.JSONB(astext_type=sa.Text()), nullable=True), |
| 34 | + sa.Column("config_hash", sa.String(length=16), nullable=False), |
| 35 | + # Data window |
| 36 | + sa.Column("data_window_start", sa.Date(), nullable=False), |
| 37 | + sa.Column("data_window_end", sa.Date(), nullable=False), |
| 38 | + sa.Column("store_id", sa.Integer(), nullable=False), |
| 39 | + sa.Column("product_id", sa.Integer(), nullable=False), |
| 40 | + # Metrics |
| 41 | + sa.Column("metrics", postgresql.JSONB(astext_type=sa.Text()), nullable=True), |
| 42 | + # Artifact info |
| 43 | + sa.Column("artifact_uri", sa.String(length=500), nullable=True), |
| 44 | + sa.Column("artifact_hash", sa.String(length=64), nullable=True), |
| 45 | + sa.Column("artifact_size_bytes", sa.Integer(), nullable=True), |
| 46 | + # Environment & lineage |
| 47 | + sa.Column("runtime_info", postgresql.JSONB(astext_type=sa.Text()), nullable=True), |
| 48 | + sa.Column("agent_context", postgresql.JSONB(astext_type=sa.Text()), nullable=True), |
| 49 | + sa.Column("git_sha", sa.String(length=40), nullable=True), |
| 50 | + # Error tracking |
| 51 | + sa.Column("error_message", sa.String(length=2000), nullable=True), |
| 52 | + # Timing |
| 53 | + sa.Column("started_at", sa.DateTime(timezone=True), nullable=True), |
| 54 | + sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True), |
| 55 | + # Timestamps (from TimestampMixin) |
| 56 | + sa.Column( |
| 57 | + "created_at", |
| 58 | + sa.DateTime(timezone=True), |
| 59 | + server_default=sa.text("now()"), |
| 60 | + nullable=False, |
| 61 | + ), |
| 62 | + sa.Column( |
| 63 | + "updated_at", |
| 64 | + sa.DateTime(timezone=True), |
| 65 | + server_default=sa.text("now()"), |
| 66 | + nullable=False, |
| 67 | + ), |
| 68 | + # Constraints |
| 69 | + sa.PrimaryKeyConstraint("id"), |
| 70 | + sa.CheckConstraint( |
| 71 | + "status IN ('pending', 'running', 'success', 'failed', 'archived')", |
| 72 | + name="ck_model_run_valid_status", |
| 73 | + ), |
| 74 | + sa.CheckConstraint( |
| 75 | + "data_window_end >= data_window_start", |
| 76 | + name="ck_model_run_valid_data_window", |
| 77 | + ), |
| 78 | + ) |
| 79 | + |
| 80 | + # Create indexes for model_run |
| 81 | + op.create_index(op.f("ix_model_run_run_id"), "model_run", ["run_id"], unique=True) |
| 82 | + op.create_index(op.f("ix_model_run_status"), "model_run", ["status"], unique=False) |
| 83 | + op.create_index(op.f("ix_model_run_model_type"), "model_run", ["model_type"], unique=False) |
| 84 | + op.create_index(op.f("ix_model_run_config_hash"), "model_run", ["config_hash"], unique=False) |
| 85 | + op.create_index(op.f("ix_model_run_store_id"), "model_run", ["store_id"], unique=False) |
| 86 | + op.create_index(op.f("ix_model_run_product_id"), "model_run", ["product_id"], unique=False) |
| 87 | + |
| 88 | + # Composite indexes |
| 89 | + op.create_index( |
| 90 | + "ix_model_run_store_product", "model_run", ["store_id", "product_id"], unique=False |
| 91 | + ) |
| 92 | + op.create_index( |
| 93 | + "ix_model_run_data_window", |
| 94 | + "model_run", |
| 95 | + ["data_window_start", "data_window_end"], |
| 96 | + unique=False, |
| 97 | + ) |
| 98 | + |
| 99 | + # GIN indexes for JSONB containment queries |
| 100 | + op.create_index( |
| 101 | + "ix_model_run_model_config_gin", |
| 102 | + "model_run", |
| 103 | + ["model_config"], |
| 104 | + unique=False, |
| 105 | + postgresql_using="gin", |
| 106 | + ) |
| 107 | + op.create_index( |
| 108 | + "ix_model_run_metrics_gin", |
| 109 | + "model_run", |
| 110 | + ["metrics"], |
| 111 | + unique=False, |
| 112 | + postgresql_using="gin", |
| 113 | + ) |
| 114 | + |
| 115 | + # Create deployment_alias table |
| 116 | + op.create_table( |
| 117 | + "deployment_alias", |
| 118 | + sa.Column("id", sa.Integer(), nullable=False), |
| 119 | + sa.Column("alias_name", sa.String(length=100), nullable=False), |
| 120 | + sa.Column("run_id", sa.Integer(), nullable=False), |
| 121 | + sa.Column("description", sa.String(length=500), nullable=True), |
| 122 | + # Timestamps (from TimestampMixin) |
| 123 | + sa.Column( |
| 124 | + "created_at", |
| 125 | + sa.DateTime(timezone=True), |
| 126 | + server_default=sa.text("now()"), |
| 127 | + nullable=False, |
| 128 | + ), |
| 129 | + sa.Column( |
| 130 | + "updated_at", |
| 131 | + sa.DateTime(timezone=True), |
| 132 | + server_default=sa.text("now()"), |
| 133 | + nullable=False, |
| 134 | + ), |
| 135 | + # Constraints |
| 136 | + sa.PrimaryKeyConstraint("id"), |
| 137 | + sa.ForeignKeyConstraint(["run_id"], ["model_run.id"]), |
| 138 | + sa.UniqueConstraint("alias_name", name="uq_deployment_alias_name"), |
| 139 | + ) |
| 140 | + |
| 141 | + # Create indexes for deployment_alias |
| 142 | + op.create_index( |
| 143 | + op.f("ix_deployment_alias_alias_name"), |
| 144 | + "deployment_alias", |
| 145 | + ["alias_name"], |
| 146 | + unique=True, |
| 147 | + ) |
| 148 | + op.create_index( |
| 149 | + op.f("ix_deployment_alias_run_id"), "deployment_alias", ["run_id"], unique=False |
| 150 | + ) |
| 151 | + |
| 152 | + |
| 153 | +def downgrade() -> None: |
| 154 | + """Revert migration - drop model_run and deployment_alias tables.""" |
| 155 | + # Drop deployment_alias table and indexes |
| 156 | + op.drop_index(op.f("ix_deployment_alias_run_id"), table_name="deployment_alias") |
| 157 | + op.drop_index(op.f("ix_deployment_alias_alias_name"), table_name="deployment_alias") |
| 158 | + op.drop_table("deployment_alias") |
| 159 | + |
| 160 | + # Drop model_run indexes |
| 161 | + op.drop_index("ix_model_run_metrics_gin", table_name="model_run") |
| 162 | + op.drop_index("ix_model_run_model_config_gin", table_name="model_run") |
| 163 | + op.drop_index("ix_model_run_data_window", table_name="model_run") |
| 164 | + op.drop_index("ix_model_run_store_product", table_name="model_run") |
| 165 | + op.drop_index(op.f("ix_model_run_product_id"), table_name="model_run") |
| 166 | + op.drop_index(op.f("ix_model_run_store_id"), table_name="model_run") |
| 167 | + op.drop_index(op.f("ix_model_run_config_hash"), table_name="model_run") |
| 168 | + op.drop_index(op.f("ix_model_run_model_type"), table_name="model_run") |
| 169 | + op.drop_index(op.f("ix_model_run_status"), table_name="model_run") |
| 170 | + op.drop_index(op.f("ix_model_run_run_id"), table_name="model_run") |
| 171 | + |
| 172 | + # Drop model_run table |
| 173 | + op.drop_table("model_run") |
0 commit comments