|
| 1 | +"""create_evaluation_run_table, batch_job_table, and evaluation_dataset_table |
| 2 | +
|
| 3 | +Revision ID: 6fe772038a5a |
| 4 | +Revises: 219033c644de |
| 5 | +Create Date: 2025-11-05 22:47:18.266070 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from sqlalchemy.dialects import postgresql |
| 11 | +import sqlmodel.sql.sqltypes |
| 12 | + |
| 13 | + |
| 14 | +# revision identifiers, used by Alembic. |
| 15 | +revision = "6fe772038a5a" |
| 16 | +down_revision = "219033c644de" |
| 17 | +branch_labels = None |
| 18 | +depends_on = None |
| 19 | + |
| 20 | + |
| 21 | +def upgrade(): |
| 22 | + # Create batch_job table first (as evaluation_run will reference it) |
| 23 | + op.create_table( |
| 24 | + "batch_job", |
| 25 | + sa.Column("id", sa.Integer(), nullable=False), |
| 26 | + sa.Column( |
| 27 | + "provider", |
| 28 | + sa.String(), |
| 29 | + nullable=False, |
| 30 | + comment="LLM provider name (e.g., 'openai', 'anthropic')", |
| 31 | + ), |
| 32 | + sa.Column( |
| 33 | + "job_type", |
| 34 | + sa.String(), |
| 35 | + nullable=False, |
| 36 | + comment="Type of batch job (e.g., 'evaluation', 'classification', 'embedding')", |
| 37 | + ), |
| 38 | + sa.Column( |
| 39 | + "config", |
| 40 | + postgresql.JSONB(astext_type=sa.Text()), |
| 41 | + nullable=False, |
| 42 | + server_default=sa.text("'{}'::jsonb"), |
| 43 | + comment="Complete batch configuration", |
| 44 | + ), |
| 45 | + sa.Column( |
| 46 | + "provider_batch_id", |
| 47 | + sa.String(), |
| 48 | + nullable=True, |
| 49 | + comment="Provider's batch job ID", |
| 50 | + ), |
| 51 | + sa.Column( |
| 52 | + "provider_file_id", |
| 53 | + sa.String(), |
| 54 | + nullable=True, |
| 55 | + comment="Provider's input file ID", |
| 56 | + ), |
| 57 | + sa.Column( |
| 58 | + "provider_output_file_id", |
| 59 | + sa.String(), |
| 60 | + nullable=True, |
| 61 | + comment="Provider's output file ID", |
| 62 | + ), |
| 63 | + sa.Column( |
| 64 | + "provider_status", |
| 65 | + sa.String(), |
| 66 | + nullable=True, |
| 67 | + comment="Provider-specific status (e.g., OpenAI: validating, in_progress, completed, failed)", |
| 68 | + ), |
| 69 | + sa.Column( |
| 70 | + "raw_output_url", |
| 71 | + sa.String(), |
| 72 | + nullable=True, |
| 73 | + comment="S3 URL of raw batch output file", |
| 74 | + ), |
| 75 | + sa.Column( |
| 76 | + "total_items", |
| 77 | + sa.Integer(), |
| 78 | + nullable=False, |
| 79 | + server_default=sa.text("0"), |
| 80 | + comment="Total number of items in the batch", |
| 81 | + ), |
| 82 | + sa.Column( |
| 83 | + "error_message", |
| 84 | + sa.Text(), |
| 85 | + nullable=True, |
| 86 | + comment="Error message if batch failed", |
| 87 | + ), |
| 88 | + sa.Column("organization_id", sa.Integer(), nullable=False), |
| 89 | + sa.Column("project_id", sa.Integer(), nullable=False), |
| 90 | + sa.Column("inserted_at", sa.DateTime(), nullable=False), |
| 91 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 92 | + sa.ForeignKeyConstraint( |
| 93 | + ["organization_id"], ["organization.id"], ondelete="CASCADE" |
| 94 | + ), |
| 95 | + sa.ForeignKeyConstraint(["project_id"], ["project.id"], ondelete="CASCADE"), |
| 96 | + sa.PrimaryKeyConstraint("id"), |
| 97 | + ) |
| 98 | + op.create_index( |
| 99 | + op.f("ix_batch_job_job_type"), "batch_job", ["job_type"], unique=False |
| 100 | + ) |
| 101 | + op.create_index( |
| 102 | + op.f("ix_batch_job_organization_id"), |
| 103 | + "batch_job", |
| 104 | + ["organization_id"], |
| 105 | + unique=False, |
| 106 | + ) |
| 107 | + op.create_index( |
| 108 | + op.f("ix_batch_job_project_id"), "batch_job", ["project_id"], unique=False |
| 109 | + ) |
| 110 | + op.create_index( |
| 111 | + "idx_batch_job_status_org", |
| 112 | + "batch_job", |
| 113 | + ["provider_status", "organization_id"], |
| 114 | + unique=False, |
| 115 | + ) |
| 116 | + op.create_index( |
| 117 | + "idx_batch_job_status_project", |
| 118 | + "batch_job", |
| 119 | + ["provider_status", "project_id"], |
| 120 | + unique=False, |
| 121 | + ) |
| 122 | + |
| 123 | + # Create evaluation_dataset table |
| 124 | + op.create_table( |
| 125 | + "evaluation_dataset", |
| 126 | + sa.Column("id", sa.Integer(), nullable=False), |
| 127 | + sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 128 | + sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=True), |
| 129 | + sa.Column( |
| 130 | + "dataset_metadata", |
| 131 | + postgresql.JSONB(astext_type=sa.Text()), |
| 132 | + nullable=False, |
| 133 | + server_default=sa.text("'{}'::jsonb"), |
| 134 | + ), |
| 135 | + sa.Column( |
| 136 | + "object_store_url", sqlmodel.sql.sqltypes.AutoString(), nullable=True |
| 137 | + ), |
| 138 | + sa.Column( |
| 139 | + "langfuse_dataset_id", |
| 140 | + sqlmodel.sql.sqltypes.AutoString(), |
| 141 | + nullable=True, |
| 142 | + ), |
| 143 | + sa.Column("organization_id", sa.Integer(), nullable=False), |
| 144 | + sa.Column("project_id", sa.Integer(), nullable=False), |
| 145 | + sa.Column("inserted_at", sa.DateTime(), nullable=False), |
| 146 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 147 | + sa.ForeignKeyConstraint( |
| 148 | + ["organization_id"], ["organization.id"], ondelete="CASCADE" |
| 149 | + ), |
| 150 | + sa.ForeignKeyConstraint(["project_id"], ["project.id"], ondelete="CASCADE"), |
| 151 | + sa.PrimaryKeyConstraint("id"), |
| 152 | + sa.UniqueConstraint( |
| 153 | + "name", |
| 154 | + "organization_id", |
| 155 | + "project_id", |
| 156 | + name="uq_evaluation_dataset_name_org_project", |
| 157 | + ), |
| 158 | + ) |
| 159 | + op.create_index( |
| 160 | + op.f("ix_evaluation_dataset_name"), |
| 161 | + "evaluation_dataset", |
| 162 | + ["name"], |
| 163 | + unique=False, |
| 164 | + ) |
| 165 | + |
| 166 | + # Create evaluation_run table with all columns and foreign key references |
| 167 | + op.create_table( |
| 168 | + "evaluation_run", |
| 169 | + sa.Column("run_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 170 | + sa.Column("dataset_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 171 | + sa.Column("config", sa.JSON(), nullable=False), |
| 172 | + sa.Column("batch_job_id", sa.Integer(), nullable=True), |
| 173 | + sa.Column( |
| 174 | + "embedding_batch_job_id", |
| 175 | + sa.Integer(), |
| 176 | + nullable=True, |
| 177 | + comment="Reference to the batch_job for embedding-based similarity scoring", |
| 178 | + ), |
| 179 | + sa.Column("dataset_id", sa.Integer(), nullable=False), |
| 180 | + sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False), |
| 181 | + sa.Column( |
| 182 | + "object_store_url", sqlmodel.sql.sqltypes.AutoString(), nullable=True |
| 183 | + ), |
| 184 | + sa.Column("total_items", sa.Integer(), nullable=False), |
| 185 | + sa.Column("score", sa.JSON(), nullable=True), |
| 186 | + sa.Column("error_message", sa.Text(), nullable=True), |
| 187 | + sa.Column("organization_id", sa.Integer(), nullable=False), |
| 188 | + sa.Column("project_id", sa.Integer(), nullable=False), |
| 189 | + sa.Column("id", sa.Integer(), nullable=False), |
| 190 | + sa.Column("inserted_at", sa.DateTime(), nullable=False), |
| 191 | + sa.Column("updated_at", sa.DateTime(), nullable=False), |
| 192 | + sa.ForeignKeyConstraint( |
| 193 | + ["batch_job_id"], |
| 194 | + ["batch_job.id"], |
| 195 | + ondelete="SET NULL", |
| 196 | + ), |
| 197 | + sa.ForeignKeyConstraint( |
| 198 | + ["embedding_batch_job_id"], |
| 199 | + ["batch_job.id"], |
| 200 | + name="fk_evaluation_run_embedding_batch_job_id", |
| 201 | + ondelete="SET NULL", |
| 202 | + ), |
| 203 | + sa.ForeignKeyConstraint( |
| 204 | + ["dataset_id"], |
| 205 | + ["evaluation_dataset.id"], |
| 206 | + name="fk_evaluation_run_dataset_id", |
| 207 | + ondelete="CASCADE", |
| 208 | + ), |
| 209 | + sa.ForeignKeyConstraint( |
| 210 | + ["organization_id"], ["organization.id"], ondelete="CASCADE" |
| 211 | + ), |
| 212 | + sa.ForeignKeyConstraint(["project_id"], ["project.id"], ondelete="CASCADE"), |
| 213 | + sa.PrimaryKeyConstraint("id"), |
| 214 | + ) |
| 215 | + op.create_index( |
| 216 | + op.f("ix_evaluation_run_run_name"), "evaluation_run", ["run_name"], unique=False |
| 217 | + ) |
| 218 | + op.create_index( |
| 219 | + "idx_eval_run_status_org", |
| 220 | + "evaluation_run", |
| 221 | + ["status", "organization_id"], |
| 222 | + unique=False, |
| 223 | + ) |
| 224 | + op.create_index( |
| 225 | + "idx_eval_run_status_project", |
| 226 | + "evaluation_run", |
| 227 | + ["status", "project_id"], |
| 228 | + unique=False, |
| 229 | + ) |
| 230 | + |
| 231 | + |
| 232 | +def downgrade(): |
| 233 | + # Drop evaluation_run table first (has foreign keys to batch_job and evaluation_dataset) |
| 234 | + op.drop_index("idx_eval_run_status_project", table_name="evaluation_run") |
| 235 | + op.drop_index("idx_eval_run_status_org", table_name="evaluation_run") |
| 236 | + op.drop_index(op.f("ix_evaluation_run_run_name"), table_name="evaluation_run") |
| 237 | + op.drop_table("evaluation_run") |
| 238 | + |
| 239 | + # Drop evaluation_dataset table |
| 240 | + op.drop_index(op.f("ix_evaluation_dataset_name"), table_name="evaluation_dataset") |
| 241 | + op.drop_table("evaluation_dataset") |
| 242 | + |
| 243 | + # Drop batch_job table |
| 244 | + op.drop_index("idx_batch_job_status_project", table_name="batch_job") |
| 245 | + op.drop_index("idx_batch_job_status_org", table_name="batch_job") |
| 246 | + op.drop_index(op.f("ix_batch_job_project_id"), table_name="batch_job") |
| 247 | + op.drop_index(op.f("ix_batch_job_organization_id"), table_name="batch_job") |
| 248 | + op.drop_index(op.f("ix_batch_job_job_type"), table_name="batch_job") |
| 249 | + op.drop_table("batch_job") |
0 commit comments