|
| 1 | +"""Add external_url and scientific_artifact_external_url_link |
| 2 | +
|
| 3 | +Revision ID: 34a01809b4d4 |
| 4 | +Revises: 781c7a2474de |
| 5 | +Create Date: 2025-08-20 10:53:53.943089 |
| 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 = "34a01809b4d4" |
| 21 | +down_revision: Union[str, None] = "781c7a2474de" |
| 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("channelpedia", "modeldb", "icgenealogy", name="externalsource").create(op.get_bind()) |
| 29 | + op.create_table( |
| 30 | + "external_url", |
| 31 | + sa.Column( |
| 32 | + "source", |
| 33 | + postgresql.ENUM( |
| 34 | + "channelpedia", "modeldb", "icgenealogy", name="externalsource", create_type=False |
| 35 | + ), |
| 36 | + nullable=False, |
| 37 | + ), |
| 38 | + sa.Column("url", sa.String(), nullable=False), |
| 39 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 40 | + sa.Column("created_by_id", sa.Uuid(), nullable=False), |
| 41 | + sa.Column("updated_by_id", sa.Uuid(), nullable=False), |
| 42 | + sa.Column( |
| 43 | + "creation_date", |
| 44 | + sa.DateTime(timezone=True), |
| 45 | + server_default=sa.text("now()"), |
| 46 | + nullable=False, |
| 47 | + ), |
| 48 | + sa.Column( |
| 49 | + "update_date", |
| 50 | + sa.DateTime(timezone=True), |
| 51 | + server_default=sa.text("now()"), |
| 52 | + nullable=False, |
| 53 | + ), |
| 54 | + sa.Column("name", sa.String(), nullable=False), |
| 55 | + sa.Column("description", sa.String(), nullable=False), |
| 56 | + sa.Column("description_vector", postgresql.TSVECTOR(), nullable=True), |
| 57 | + sa.ForeignKeyConstraint( |
| 58 | + ["created_by_id"], ["agent.id"], name=op.f("fk_external_url_created_by_id_agent") |
| 59 | + ), |
| 60 | + sa.ForeignKeyConstraint( |
| 61 | + ["updated_by_id"], ["agent.id"], name=op.f("fk_external_url_updated_by_id_agent") |
| 62 | + ), |
| 63 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_external_url")), |
| 64 | + ) |
| 65 | + op.create_index( |
| 66 | + op.f("ix_external_url_created_by_id"), "external_url", ["created_by_id"], unique=False |
| 67 | + ) |
| 68 | + op.create_index( |
| 69 | + op.f("ix_external_url_creation_date"), "external_url", ["creation_date"], unique=False |
| 70 | + ) |
| 71 | + op.create_index( |
| 72 | + "ix_external_url_description_vector", |
| 73 | + "external_url", |
| 74 | + ["description_vector"], |
| 75 | + unique=False, |
| 76 | + postgresql_using="gin", |
| 77 | + ) |
| 78 | + op.create_index(op.f("ix_external_url_name"), "external_url", ["name"], unique=False) |
| 79 | + op.create_index( |
| 80 | + op.f("ix_external_url_updated_by_id"), "external_url", ["updated_by_id"], unique=False |
| 81 | + ) |
| 82 | + op.create_index(op.f("ix_external_url_url"), "external_url", ["url"], unique=True) |
| 83 | + op.create_table( |
| 84 | + "scientific_artifact_external_url_link", |
| 85 | + sa.Column("external_url_id", sa.Uuid(), nullable=False), |
| 86 | + sa.Column("scientific_artifact_id", sa.Uuid(), nullable=False), |
| 87 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 88 | + sa.Column("created_by_id", sa.Uuid(), nullable=False), |
| 89 | + sa.Column("updated_by_id", sa.Uuid(), nullable=False), |
| 90 | + sa.Column( |
| 91 | + "creation_date", |
| 92 | + sa.DateTime(timezone=True), |
| 93 | + server_default=sa.text("now()"), |
| 94 | + nullable=False, |
| 95 | + ), |
| 96 | + sa.Column( |
| 97 | + "update_date", |
| 98 | + sa.DateTime(timezone=True), |
| 99 | + server_default=sa.text("now()"), |
| 100 | + nullable=False, |
| 101 | + ), |
| 102 | + sa.ForeignKeyConstraint( |
| 103 | + ["created_by_id"], |
| 104 | + ["agent.id"], |
| 105 | + name=op.f("fk_scientific_artifact_external_url_link_created_by_id_agent"), |
| 106 | + ), |
| 107 | + sa.ForeignKeyConstraint( |
| 108 | + ["external_url_id"], |
| 109 | + ["external_url.id"], |
| 110 | + name=op.f("fk_scientific_artifact_external_url_link_external_url_id_external_url"), |
| 111 | + ), |
| 112 | + sa.ForeignKeyConstraint( |
| 113 | + ["scientific_artifact_id"], |
| 114 | + ["scientific_artifact.id"], |
| 115 | + name=op.f( |
| 116 | + "fk_scientific_artifact_external_url_link_scientific_artifact_id_scientific_artifact" |
| 117 | + ), |
| 118 | + ), |
| 119 | + sa.ForeignKeyConstraint( |
| 120 | + ["updated_by_id"], |
| 121 | + ["agent.id"], |
| 122 | + name=op.f("fk_scientific_artifact_external_url_link_updated_by_id_agent"), |
| 123 | + ), |
| 124 | + sa.PrimaryKeyConstraint("id", name=op.f("pk_scientific_artifact_external_url_link")), |
| 125 | + sa.UniqueConstraint( |
| 126 | + "external_url_id", |
| 127 | + "scientific_artifact_id", |
| 128 | + name="uq_scientific_artifact_external_url_link", |
| 129 | + ), |
| 130 | + ) |
| 131 | + op.create_index( |
| 132 | + op.f("ix_scientific_artifact_external_url_link_created_by_id"), |
| 133 | + "scientific_artifact_external_url_link", |
| 134 | + ["created_by_id"], |
| 135 | + unique=False, |
| 136 | + ) |
| 137 | + op.create_index( |
| 138 | + op.f("ix_scientific_artifact_external_url_link_creation_date"), |
| 139 | + "scientific_artifact_external_url_link", |
| 140 | + ["creation_date"], |
| 141 | + unique=False, |
| 142 | + ) |
| 143 | + op.create_index( |
| 144 | + op.f("ix_scientific_artifact_external_url_link_external_url_id"), |
| 145 | + "scientific_artifact_external_url_link", |
| 146 | + ["external_url_id"], |
| 147 | + unique=False, |
| 148 | + ) |
| 149 | + op.create_index( |
| 150 | + op.f("ix_scientific_artifact_external_url_link_scientific_artifact_id"), |
| 151 | + "scientific_artifact_external_url_link", |
| 152 | + ["scientific_artifact_id"], |
| 153 | + unique=False, |
| 154 | + ) |
| 155 | + op.create_index( |
| 156 | + op.f("ix_scientific_artifact_external_url_link_updated_by_id"), |
| 157 | + "scientific_artifact_external_url_link", |
| 158 | + ["updated_by_id"], |
| 159 | + unique=False, |
| 160 | + ) |
| 161 | + op.sync_enum_values( |
| 162 | + enum_schema="public", |
| 163 | + enum_name="entitytype", |
| 164 | + new_values=[ |
| 165 | + "analysis_software_source_code", |
| 166 | + "brain_atlas", |
| 167 | + "brain_atlas_region", |
| 168 | + "cell_composition", |
| 169 | + "electrical_cell_recording", |
| 170 | + "electrical_recording_stimulus", |
| 171 | + "emodel", |
| 172 | + "experimental_bouton_density", |
| 173 | + "experimental_neuron_density", |
| 174 | + "experimental_synapses_per_connection", |
| 175 | + "external_url", |
| 176 | + "ion_channel_model", |
| 177 | + "memodel", |
| 178 | + "mesh", |
| 179 | + "memodel_calibration_result", |
| 180 | + "me_type_density", |
| 181 | + "publication", |
| 182 | + "reconstruction_morphology", |
| 183 | + "simulation", |
| 184 | + "simulation_campaign", |
| 185 | + "simulation_campaign_generation", |
| 186 | + "simulation_execution", |
| 187 | + "simulation_result", |
| 188 | + "scientific_artifact", |
| 189 | + "single_neuron_simulation", |
| 190 | + "single_neuron_synaptome", |
| 191 | + "single_neuron_synaptome_simulation", |
| 192 | + "subject", |
| 193 | + "validation_result", |
| 194 | + "circuit", |
| 195 | + ], |
| 196 | + affected_columns=[ |
| 197 | + TableReference(table_schema="public", table_name="entity", column_name="type") |
| 198 | + ], |
| 199 | + enum_values_to_rename=[], |
| 200 | + ) |
| 201 | + # ### end Alembic commands ### |
| 202 | + |
| 203 | + |
| 204 | +def downgrade() -> None: |
| 205 | + # ### commands auto generated by Alembic - please adjust! ### |
| 206 | + op.sync_enum_values( |
| 207 | + enum_schema="public", |
| 208 | + enum_name="entitytype", |
| 209 | + new_values=[ |
| 210 | + "analysis_software_source_code", |
| 211 | + "brain_atlas", |
| 212 | + "brain_atlas_region", |
| 213 | + "cell_composition", |
| 214 | + "electrical_cell_recording", |
| 215 | + "electrical_recording_stimulus", |
| 216 | + "emodel", |
| 217 | + "experimental_bouton_density", |
| 218 | + "experimental_neuron_density", |
| 219 | + "experimental_synapses_per_connection", |
| 220 | + "ion_channel_model", |
| 221 | + "memodel", |
| 222 | + "mesh", |
| 223 | + "memodel_calibration_result", |
| 224 | + "me_type_density", |
| 225 | + "publication", |
| 226 | + "reconstruction_morphology", |
| 227 | + "simulation", |
| 228 | + "simulation_campaign", |
| 229 | + "simulation_campaign_generation", |
| 230 | + "simulation_execution", |
| 231 | + "simulation_result", |
| 232 | + "scientific_artifact", |
| 233 | + "single_neuron_simulation", |
| 234 | + "single_neuron_synaptome", |
| 235 | + "single_neuron_synaptome_simulation", |
| 236 | + "subject", |
| 237 | + "validation_result", |
| 238 | + "circuit", |
| 239 | + ], |
| 240 | + affected_columns=[ |
| 241 | + TableReference(table_schema="public", table_name="entity", column_name="type") |
| 242 | + ], |
| 243 | + enum_values_to_rename=[], |
| 244 | + ) |
| 245 | + op.drop_index( |
| 246 | + op.f("ix_scientific_artifact_external_url_link_updated_by_id"), |
| 247 | + table_name="scientific_artifact_external_url_link", |
| 248 | + ) |
| 249 | + op.drop_index( |
| 250 | + op.f("ix_scientific_artifact_external_url_link_scientific_artifact_id"), |
| 251 | + table_name="scientific_artifact_external_url_link", |
| 252 | + ) |
| 253 | + op.drop_index( |
| 254 | + op.f("ix_scientific_artifact_external_url_link_external_url_id"), |
| 255 | + table_name="scientific_artifact_external_url_link", |
| 256 | + ) |
| 257 | + op.drop_index( |
| 258 | + op.f("ix_scientific_artifact_external_url_link_creation_date"), |
| 259 | + table_name="scientific_artifact_external_url_link", |
| 260 | + ) |
| 261 | + op.drop_index( |
| 262 | + op.f("ix_scientific_artifact_external_url_link_created_by_id"), |
| 263 | + table_name="scientific_artifact_external_url_link", |
| 264 | + ) |
| 265 | + op.drop_table("scientific_artifact_external_url_link") |
| 266 | + op.drop_index(op.f("ix_external_url_url"), table_name="external_url") |
| 267 | + op.drop_index(op.f("ix_external_url_updated_by_id"), table_name="external_url") |
| 268 | + op.drop_index(op.f("ix_external_url_name"), table_name="external_url") |
| 269 | + op.drop_index( |
| 270 | + "ix_external_url_description_vector", table_name="external_url", postgresql_using="gin" |
| 271 | + ) |
| 272 | + op.drop_index(op.f("ix_external_url_creation_date"), table_name="external_url") |
| 273 | + op.drop_index(op.f("ix_external_url_created_by_id"), table_name="external_url") |
| 274 | + op.drop_table("external_url") |
| 275 | + sa.Enum("channelpedia", "modeldb", "icgenealogy", name="externalsource").drop(op.get_bind()) |
| 276 | + # ### end Alembic commands ### |
0 commit comments