33Revision ID: bf6308af80aa
44Revises: 380002bcbb26
55Create Date: 2022-05-16 12:11:17.906600
6-
76"""
87import logging
98
@@ -74,7 +73,7 @@ def _FKConstraint(
7473 schema : str | None = None ,
7574 ondelete : str | None = None ,
7675) -> sa .schema .ForeignKeyConstraint :
77- """ Create foreign key constraint."""
76+ # Create foreign key constraint.
7877 fk_name = "_" .join (["fkey" , src_table , tgt_table ] + tgt_columns + src_columns )
7978 fk_name = shrinkDatabaseEntityName (fk_name , bind )
8079 tgt_columns = [f"{ tgt_table } .{ col } " for col in tgt_columns ]
@@ -85,7 +84,8 @@ def _FKConstraint(
8584
8685def _migrate_visit_definition () -> None :
8786 """Make new visit_system_membership table, fill from existing data
88- in visit_definition."""
87+ in visit_definition.
88+ """
8989
9090 mig_context = context .get_context ()
9191 bind = mig_context .bind
@@ -147,23 +147,38 @@ def _migrate_visit_definition() -> None:
147147 visit_definition .columns ["visit" ],
148148 ).distinct ()
149149 sql = visit_membership .insert ().from_select (["instrument" , "visit_system" , "visit" ], selection )
150- op .execute (sql ) # type: ignore[arg-type]
150+ op .execute (sql )
151151
152152 # Drop visit_system from visit_definition.
153153 with op .batch_alter_table ("visit_definition" , schema = schema ) as batch_op :
154154 # visit_system is in PK. Postgres drops PK when the column is dropped,
155155 # but sqlite complains that column cannot be dropped if it's in PK.
156- # The workaround is to create PK with new columns, but Postgres also
157- # requires existing PK to be dropped first, and in sqlite we do not
158- # even have name for PK constraint, so it cannot be dropped
159- # explicitly.
156+ # Dropping PK in SQLite cannot be done as we generate PKs without name.
157+ # Simply trying to replace PK using different columns generates
158+ # SAWarning. The workaround is to re-create PK with old columns and
159+ # name it, then drop that named PK. Postgres also requires existing PK
160+ # to be dropped first before re-creating it.
161+
162+ # Drop PK in Postgres.
160163 if bind .dialect .name == "postgresql" :
161- batch_op .drop_constraint ("visit_definition_pkey" ) # type: ignore[attr-defined]
162- batch_op .create_primary_key ( # type: ignore[attr-defined]
164+ batch_op .drop_constraint ("visit_definition_pkey" )
165+
166+ # Re-create PK and give it a name.
167+ batch_op .create_primary_key (
168+ "visit_definition_pkey" , ["instrument" , "visit_system" , "exposure" ]
169+ )
170+
171+ # Now drop named PK.
172+ batch_op .drop_constraint ("visit_definition_pkey" )
173+
174+ # Create PK with different columns.
175+ batch_op .create_primary_key (
163176 "visit_definition_pkey" , ["instrument" , "exposure" , "visit" ]
164177 )
165- batch_op .drop_index ("visit_definition_fkidx_instrument_visit_system" ) # type: ignore[attr-defined]
166- batch_op .drop_column ("visit_system" ) # type: ignore[attr-defined]
178+
179+ # Finally drop index and column.
180+ batch_op .drop_index ("visit_definition_fkidx_instrument_visit_system" )
181+ batch_op .drop_column ("visit_system" )
167182
168183
169184def _migrate_instrument () -> None :
@@ -202,10 +217,10 @@ def _migrate_visit() -> None:
202217
203218 _LOG .info ("migrating visit table" )
204219 with op .batch_alter_table ("visit" , schema = schema ) as batch_op :
205- batch_op .drop_index ("visit_fkidx_instrument_visit_system" ) # type: ignore[attr-defined]
206- batch_op .drop_column ("visit_system" ) # type: ignore[attr-defined]
207- batch_op .add_column (sa .Column ("seq_num" , sa .BigInteger )) # type: ignore[attr-defined]
208- batch_op .add_column (sa .Column ("azimuth" , sa .Float )) # type: ignore[attr-defined]
220+ batch_op .drop_index ("visit_fkidx_instrument_visit_system" )
221+ batch_op .drop_column ("visit_system" )
222+ batch_op .add_column (sa .Column ("seq_num" , sa .BigInteger ))
223+ batch_op .add_column (sa .Column ("azimuth" , sa .Float ))
209224
210225 # Fill seq_num column with the lowest value of matching exposure.seq_num.
211226 #
@@ -259,10 +274,10 @@ def _migrate_exposure(has_simulated: bool) -> None:
259274
260275 _LOG .info ("migrating exposure table" )
261276 with op .batch_alter_table ("exposure" , schema = schema ) as batch_op :
262- batch_op .add_column (sa .Column ("seq_start" , sa .BigInteger )) # type: ignore[attr-defined]
263- batch_op .add_column (sa .Column ("seq_end" , sa .BigInteger )) # type: ignore[attr-defined]
264- batch_op .add_column (sa .Column ("azimuth" , sa .Float )) # type: ignore[attr-defined]
265- batch_op .add_column (sa .Column ("has_simulated" , sa .Boolean )) # type: ignore[attr-defined]
277+ batch_op .add_column (sa .Column ("seq_start" , sa .BigInteger ))
278+ batch_op .add_column (sa .Column ("seq_end" , sa .BigInteger ))
279+ batch_op .add_column (sa .Column ("azimuth" , sa .Float ))
280+ batch_op .add_column (sa .Column ("has_simulated" , sa .Boolean ))
266281
267282 table = sa .schema .Table ("exposure" , metadata , autoload_with = bind , schema = schema )
268283 op .execute (
0 commit comments