1+ from macrostrat .database import Database , Identifier
2+
13from macrostrat .core .migrations import Migration , _not , custom_type_exists
2- from macrostrat .database import Database
34
45
56def ingest_type_exists_in_wrong_schema (db : Database ) -> bool :
@@ -8,6 +9,9 @@ def ingest_type_exists_in_wrong_schema(db: Database) -> bool:
89 return any (conditions )
910
1011
12+ _schemas = ["macrostrat_backup" , "macrostrat" , "public" ]
13+
14+
1115class MapsIngestStateCustomTypeMigration (Migration ):
1216 name = "ingest-state-type"
1317 subsystem = "maps"
@@ -21,21 +25,23 @@ class MapsIngestStateCustomTypeMigration(Migration):
2125 postconditions = [
2226 custom_type_exists ("maps" , "ingest_state" ),
2327 custom_type_exists ("maps" , "ingest_type" ),
24- _not (custom_type_exists ("public" , "ingest_state" )),
25- _not (custom_type_exists ("public" , "ingest_type" )),
26- _not (custom_type_exists ("macrostrat" , "ingest_state" )),
27- _not (custom_type_exists ("macrostrat" , "ingest_type" )),
28+ * (_not (custom_type_exists (schema , "ingest_state" )) for schema in _schemas ),
29+ * (_not (custom_type_exists (schema , "ingest_type" )) for schema in _schemas ),
2830 ]
2931
3032 preconditions = [ingest_type_exists_in_wrong_schema ]
3133
3234 def apply (self , db : Database ):
3335 # Handle edge case where the MariaDB migration has already been applied
34- db .run_sql ("ALTER TYPE macrostrat_backup.ingest_state SET SCHEMA macrostrat" )
35- db .run_sql ("ALTER TYPE macrostrat.ingest_state SET SCHEMA maps" )
36-
37- db .run_sql ("ALTER TYPE macrostrat_backup.ingest_type SET SCHEMA macrostrat" )
38- db .run_sql ("ALTER TYPE macrostrat.ingest_type SET SCHEMA maps" )
3936
40- db .run_sql ("ALTER TYPE public.ingest_state SET SCHEMA maps" )
41- db .run_sql ("ALTER TYPE public.ingest_type SET SCHEMA maps" )
37+ for schema in ["macrostrat_backup" , "macrostrat" , "public" ]:
38+ db .run_sql (
39+ """
40+ ALTER TYPE {schema}.ingest_state SET SCHEMA maps;
41+ ALTER TYPE {schema}.ingest_type SET SCHEMA maps;
42+
43+ DROP TYPE IF EXISTS {schema}.ingest_state;
44+ DROP TYPE IF EXISTS {schema}.ingest_type;
45+ """ ,
46+ dict (schema = Identifier (schema )),
47+ )
0 commit comments