|
| 1 | +"""Add generic campaign models |
| 2 | +
|
| 3 | +Revision ID: c3208a1f2c6b |
| 4 | +Revises: 523e523531a7 |
| 5 | +Create Date: 2026-02-18 10:46:17.576165 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +from alembic import op |
| 12 | +import sqlalchemy as sa |
| 13 | +from alembic_postgresql_enum import TableReference |
| 14 | +from sqlalchemy.dialects import postgresql |
| 15 | + |
| 16 | +from sqlalchemy import Text |
| 17 | +import app.db.types |
| 18 | + |
| 19 | +# revision identifiers, used by Alembic. |
| 20 | +revision: str = "c3208a1f2c6b" |
| 21 | +down_revision: Union[str, None] = "523e523531a7" |
| 22 | +branch_labels: Union[str, Sequence[str], None] = None |
| 23 | +depends_on: Union[str, Sequence[str], None] = None |
| 24 | + |
| 25 | + |
| 26 | +def upgrade() -> None: |
| 27 | + # ### commands auto generated by Alembic - please adjust! ### |
| 28 | + sa.Enum( |
| 29 | + "circuit_simulation", |
| 30 | + "circuit_extraction", |
| 31 | + "ion_channel_modeling", |
| 32 | + "skeletonization", |
| 33 | + "ion_channel_simulation", |
| 34 | + "em_synapse_mapping", |
| 35 | + name="tasktype", |
| 36 | + ).create(op.get_bind()) |
| 37 | + op.sync_enum_values( |
| 38 | + enum_schema="public", |
| 39 | + enum_name="activitytype", |
| 40 | + new_values=[ |
| 41 | + "simulation_execution", |
| 42 | + "simulation_generation", |
| 43 | + "validation", |
| 44 | + "calibration", |
| 45 | + "analysis_notebook_execution", |
| 46 | + "ion_channel_modeling_execution", |
| 47 | + "ion_channel_modeling_config_generation", |
| 48 | + "circuit_extraction_config_generation", |
| 49 | + "circuit_extraction_execution", |
| 50 | + "skeletonization_execution", |
| 51 | + "skeletonization_config_generation", |
| 52 | + "config_generation", |
| 53 | + "config_execution", |
| 54 | + ], |
| 55 | + affected_columns=[ |
| 56 | + TableReference(table_schema="public", table_name="activity", column_name="type") |
| 57 | + ], |
| 58 | + enum_values_to_rename=[], |
| 59 | + ) |
| 60 | + op.sync_enum_values( |
| 61 | + enum_schema="public", |
| 62 | + enum_name="entitytype", |
| 63 | + new_values=[ |
| 64 | + "analysis_software_source_code", |
| 65 | + "brain_atlas", |
| 66 | + "brain_atlas_region", |
| 67 | + "cell_composition", |
| 68 | + "cell_morphology", |
| 69 | + "cell_morphology_protocol", |
| 70 | + "electrical_cell_recording", |
| 71 | + "electrical_recording", |
| 72 | + "electrical_recording_stimulus", |
| 73 | + "emodel", |
| 74 | + "experimental_bouton_density", |
| 75 | + "experimental_neuron_density", |
| 76 | + "experimental_synapses_per_connection", |
| 77 | + "external_url", |
| 78 | + "ion_channel_model", |
| 79 | + "ion_channel_modeling_campaign", |
| 80 | + "ion_channel_modeling_config", |
| 81 | + "ion_channel_recording", |
| 82 | + "memodel", |
| 83 | + "memodel_calibration_result", |
| 84 | + "me_type_density", |
| 85 | + "simulation", |
| 86 | + "simulation_campaign", |
| 87 | + "simulation_result", |
| 88 | + "scientific_artifact", |
| 89 | + "single_neuron_simulation", |
| 90 | + "single_neuron_synaptome", |
| 91 | + "single_neuron_synaptome_simulation", |
| 92 | + "subject", |
| 93 | + "validation_result", |
| 94 | + "circuit", |
| 95 | + "circuit_extraction_campaign", |
| 96 | + "circuit_extraction_config", |
| 97 | + "em_dense_reconstruction_dataset", |
| 98 | + "em_cell_mesh", |
| 99 | + "analysis_notebook_template", |
| 100 | + "analysis_notebook_environment", |
| 101 | + "analysis_notebook_result", |
| 102 | + "skeletonization_config", |
| 103 | + "skeletonization_campaign", |
| 104 | + "campaign", |
| 105 | + "item_config", |
| 106 | + ], |
| 107 | + affected_columns=[ |
| 108 | + TableReference(table_schema="public", table_name="entity", column_name="type"), |
| 109 | + TableReference( |
| 110 | + table_schema="public", table_name="measurement_label", column_name="entity_type" |
| 111 | + ), |
| 112 | + ], |
| 113 | + enum_values_to_rename=[], |
| 114 | + ) |
| 115 | + op.create_table( |
| 116 | + "campaign", |
| 117 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 118 | + sa.Column( |
| 119 | + "task_type", |
| 120 | + postgresql.ENUM( |
| 121 | + "circuit_simulation", |
| 122 | + "circuit_extraction", |
| 123 | + "ion_channel_modeling", |
| 124 | + "skeletonization", |
| 125 | + "ion_channel_simulation", |
| 126 | + "em_synapse_mapping", |
| 127 | + name="tasktype", |
| 128 | + create_type=False, |
| 129 | + ), |
| 130 | + nullable=False, |
| 131 | + ), |
| 132 | + sa.Column( |
| 133 | + "scan_parameters", |
| 134 | + postgresql.JSONB(astext_type=sa.Text()), |
| 135 | + server_default="{}", |
| 136 | + nullable=False, |
| 137 | + ), |
| 138 | + sa.Column("name", sa.String(), nullable=False), |
| 139 | + sa.Column("description", sa.String(), nullable=False), |
| 140 | + sa.Column("description_vector", postgresql.TSVECTOR(), nullable=True), |
| 141 | + sa.ForeignKeyConstraint(["id"], ["entity.id"], name=op.f("fk_campaign_id_entity")), |
| 142 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_campaign")), |
| 143 | + ) |
| 144 | + op.create_index( |
| 145 | + "ix_campaign_description_vector", |
| 146 | + "campaign", |
| 147 | + ["description_vector"], |
| 148 | + unique=False, |
| 149 | + postgresql_using="gin", |
| 150 | + ) |
| 151 | + op.create_index(op.f("ix_campaign_name"), "campaign", ["name"], unique=False) |
| 152 | + op.create_index(op.f("ix_campaign_task_type"), "campaign", ["task_type"], unique=False) |
| 153 | + op.create_table( |
| 154 | + "config_execution", |
| 155 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 156 | + sa.Column( |
| 157 | + "executor", |
| 158 | + postgresql.ENUM( |
| 159 | + "single_node_job", |
| 160 | + "distributed_job", |
| 161 | + "jupyter_notebook", |
| 162 | + name="executortype", |
| 163 | + create_type=False, |
| 164 | + ), |
| 165 | + nullable=True, |
| 166 | + ), |
| 167 | + sa.Column("execution_id", sa.Uuid(), nullable=True), |
| 168 | + sa.ForeignKeyConstraint( |
| 169 | + ["id"], ["activity.id"], name=op.f("fk_config_execution_id_activity") |
| 170 | + ), |
| 171 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_config_execution")), |
| 172 | + ) |
| 173 | + op.create_table( |
| 174 | + "config_generation", |
| 175 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 176 | + sa.ForeignKeyConstraint( |
| 177 | + ["id"], ["activity.id"], name=op.f("fk_config_generation_id_activity") |
| 178 | + ), |
| 179 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_config_generation")), |
| 180 | + ) |
| 181 | + op.create_table( |
| 182 | + "entity__campaign", |
| 183 | + sa.Column("entity_id", sa.Uuid(), nullable=False), |
| 184 | + sa.Column("campaign_id", sa.Uuid(), nullable=False), |
| 185 | + sa.ForeignKeyConstraint( |
| 186 | + ["campaign_id"], |
| 187 | + ["campaign.id"], |
| 188 | + name=op.f("fk_entity__campaign_campaign_id_campaign"), |
| 189 | + ondelete="CASCADE", |
| 190 | + ), |
| 191 | + sa.ForeignKeyConstraint( |
| 192 | + ["entity_id"], |
| 193 | + ["entity.id"], |
| 194 | + name=op.f("fk_entity__campaign_entity_id_entity"), |
| 195 | + ondelete="CASCADE", |
| 196 | + ), |
| 197 | + sa.PrimaryKeyConstraint("entity_id", "campaign_id", name=op.f("pk_entity__campaign")), |
| 198 | + ) |
| 199 | + op.create_table( |
| 200 | + "item_config", |
| 201 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 202 | + sa.Column( |
| 203 | + "task_type", |
| 204 | + postgresql.ENUM( |
| 205 | + "circuit_simulation", |
| 206 | + "circuit_extraction", |
| 207 | + "ion_channel_modeling", |
| 208 | + "skeletonization", |
| 209 | + "ion_channel_simulation", |
| 210 | + "em_synapse_mapping", |
| 211 | + name="tasktype", |
| 212 | + create_type=False, |
| 213 | + ), |
| 214 | + nullable=False, |
| 215 | + ), |
| 216 | + sa.Column( |
| 217 | + "scan_parameters", |
| 218 | + postgresql.JSONB(astext_type=sa.Text()), |
| 219 | + server_default="{}", |
| 220 | + nullable=False, |
| 221 | + ), |
| 222 | + sa.Column("campaign_id", sa.Uuid(), nullable=False), |
| 223 | + sa.Column("name", sa.String(), nullable=False), |
| 224 | + sa.Column("description", sa.String(), nullable=False), |
| 225 | + sa.Column("description_vector", postgresql.TSVECTOR(), nullable=True), |
| 226 | + sa.ForeignKeyConstraint( |
| 227 | + ["campaign_id"], ["campaign.id"], name=op.f("fk_item_config_campaign_id_campaign") |
| 228 | + ), |
| 229 | + sa.ForeignKeyConstraint(["id"], ["entity.id"], name=op.f("fk_item_config_id_entity")), |
| 230 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_item_config")), |
| 231 | + ) |
| 232 | + op.create_index( |
| 233 | + op.f("ix_item_config_campaign_id"), "item_config", ["campaign_id"], unique=False |
| 234 | + ) |
| 235 | + op.create_index( |
| 236 | + "ix_item_config_description_vector", |
| 237 | + "item_config", |
| 238 | + ["description_vector"], |
| 239 | + unique=False, |
| 240 | + postgresql_using="gin", |
| 241 | + ) |
| 242 | + op.create_index(op.f("ix_item_config_name"), "item_config", ["name"], unique=False) |
| 243 | + op.create_index(op.f("ix_item_config_task_type"), "item_config", ["task_type"], unique=False) |
| 244 | + op.create_table( |
| 245 | + "entity__item_config", |
| 246 | + sa.Column("entity_id", sa.Uuid(), nullable=False), |
| 247 | + sa.Column("item_config_id", sa.Uuid(), nullable=False), |
| 248 | + sa.ForeignKeyConstraint( |
| 249 | + ["entity_id"], |
| 250 | + ["entity.id"], |
| 251 | + name=op.f("fk_entity__item_config_entity_id_entity"), |
| 252 | + ondelete="CASCADE", |
| 253 | + ), |
| 254 | + sa.ForeignKeyConstraint( |
| 255 | + ["item_config_id"], |
| 256 | + ["item_config.id"], |
| 257 | + name=op.f("fk_entity__item_config_item_config_id_item_config"), |
| 258 | + ondelete="CASCADE", |
| 259 | + ), |
| 260 | + sa.PrimaryKeyConstraint("entity_id", "item_config_id", name=op.f("pk_entity__item_config")), |
| 261 | + ) |
| 262 | + # ### end Alembic commands ### |
| 263 | + |
| 264 | + |
| 265 | +def downgrade() -> None: |
| 266 | + # ### commands auto generated by Alembic - please adjust! ### |
| 267 | + op.drop_table("entity__item_config") |
| 268 | + op.drop_index(op.f("ix_item_config_task_type"), table_name="item_config") |
| 269 | + op.drop_index(op.f("ix_item_config_name"), table_name="item_config") |
| 270 | + op.drop_index( |
| 271 | + "ix_item_config_description_vector", table_name="item_config", postgresql_using="gin" |
| 272 | + ) |
| 273 | + op.drop_index(op.f("ix_item_config_campaign_id"), table_name="item_config") |
| 274 | + op.drop_table("item_config") |
| 275 | + op.drop_table("entity__campaign") |
| 276 | + op.drop_table("config_generation") |
| 277 | + op.drop_table("config_execution") |
| 278 | + op.drop_index(op.f("ix_campaign_task_type"), table_name="campaign") |
| 279 | + op.drop_index(op.f("ix_campaign_name"), table_name="campaign") |
| 280 | + op.drop_index("ix_campaign_description_vector", table_name="campaign", postgresql_using="gin") |
| 281 | + op.drop_table("campaign") |
| 282 | + op.sync_enum_values( |
| 283 | + enum_schema="public", |
| 284 | + enum_name="entitytype", |
| 285 | + new_values=[ |
| 286 | + "analysis_software_source_code", |
| 287 | + "brain_atlas", |
| 288 | + "brain_atlas_region", |
| 289 | + "cell_composition", |
| 290 | + "cell_morphology", |
| 291 | + "cell_morphology_protocol", |
| 292 | + "electrical_cell_recording", |
| 293 | + "electrical_recording", |
| 294 | + "electrical_recording_stimulus", |
| 295 | + "emodel", |
| 296 | + "experimental_bouton_density", |
| 297 | + "experimental_neuron_density", |
| 298 | + "experimental_synapses_per_connection", |
| 299 | + "external_url", |
| 300 | + "ion_channel_model", |
| 301 | + "ion_channel_modeling_campaign", |
| 302 | + "ion_channel_modeling_config", |
| 303 | + "ion_channel_recording", |
| 304 | + "memodel", |
| 305 | + "memodel_calibration_result", |
| 306 | + "me_type_density", |
| 307 | + "simulation", |
| 308 | + "simulation_campaign", |
| 309 | + "simulation_result", |
| 310 | + "scientific_artifact", |
| 311 | + "single_neuron_simulation", |
| 312 | + "single_neuron_synaptome", |
| 313 | + "single_neuron_synaptome_simulation", |
| 314 | + "subject", |
| 315 | + "validation_result", |
| 316 | + "circuit", |
| 317 | + "circuit_extraction_campaign", |
| 318 | + "circuit_extraction_config", |
| 319 | + "em_dense_reconstruction_dataset", |
| 320 | + "em_cell_mesh", |
| 321 | + "analysis_notebook_template", |
| 322 | + "analysis_notebook_environment", |
| 323 | + "analysis_notebook_result", |
| 324 | + "skeletonization_config", |
| 325 | + "skeletonization_campaign", |
| 326 | + ], |
| 327 | + affected_columns=[ |
| 328 | + TableReference(table_schema="public", table_name="entity", column_name="type"), |
| 329 | + TableReference( |
| 330 | + table_schema="public", table_name="measurement_label", column_name="entity_type" |
| 331 | + ), |
| 332 | + ], |
| 333 | + enum_values_to_rename=[], |
| 334 | + ) |
| 335 | + op.sync_enum_values( |
| 336 | + enum_schema="public", |
| 337 | + enum_name="activitytype", |
| 338 | + new_values=[ |
| 339 | + "simulation_execution", |
| 340 | + "simulation_generation", |
| 341 | + "validation", |
| 342 | + "calibration", |
| 343 | + "analysis_notebook_execution", |
| 344 | + "ion_channel_modeling_execution", |
| 345 | + "ion_channel_modeling_config_generation", |
| 346 | + "circuit_extraction_config_generation", |
| 347 | + "circuit_extraction_execution", |
| 348 | + "skeletonization_execution", |
| 349 | + "skeletonization_config_generation", |
| 350 | + ], |
| 351 | + affected_columns=[ |
| 352 | + TableReference(table_schema="public", table_name="activity", column_name="type") |
| 353 | + ], |
| 354 | + enum_values_to_rename=[], |
| 355 | + ) |
| 356 | + sa.Enum( |
| 357 | + "circuit_simulation", |
| 358 | + "circuit_extraction", |
| 359 | + "ion_channel_modeling", |
| 360 | + "skeletonization", |
| 361 | + "ion_channel_simulation", |
| 362 | + "em_synapse_mapping", |
| 363 | + name="tasktype", |
| 364 | + ).drop(op.get_bind()) |
| 365 | + # ### end Alembic commands ### |
0 commit comments