55Create Date: 2026-01-07 20:25:34.314026
66
77Notes: This process was made more complicated by some assumptions made by the history tracking code.
8- In particular it assumes that the primary and history table have the same column order with the
8+ In particular it assumes that the primary and history table have the same column order with the
99exception that history tables have additional columns at the end. When adding a new column it is added
1010at the end and therefore breaks the assumption. To work around this, we have to recreate the history table
1111with the correct column order. This involves renaming the existing history table, creating a new one with
@@ -55,19 +55,17 @@ def upgrade():
5555 """
5656 )
5757 )
58-
58+
5959 # Drop existing triggers before modifying table structure so that we don't accidentally track
6060 # the intermediate states
6161 drop_history_triggers ("meta_network" )
62-
62+
6363 # Rename the existing history table to preserve existing history data
6464 # We'll copy data from this into the new table with the correct column order
6565 op .execute (
66- text (
67- f"ALTER TABLE { schema_name } .meta_network_hx RENAME TO meta_network_hx_old"
68- )
66+ text (f"ALTER TABLE { schema_name } .meta_network_hx RENAME TO meta_network_hx_old" )
6967 )
70-
68+
7169 op .add_column (
7270 "meta_network" ,
7371 sa .Column (
@@ -77,7 +75,7 @@ def upgrade():
7775 ),
7876 schema = schema_name ,
7977 )
80-
78+
8179 op .execute (
8280 text (
8381 f"""
@@ -86,14 +84,14 @@ def upgrade():
8684 """
8785 )
8886 )
89-
87+
9088 op .create_unique_constraint (
9189 "uq_meta_network_network_key" ,
9290 "meta_network" ,
9391 ["network_key" ],
9492 schema = schema_name ,
9593 )
96-
94+
9795 # Create a trigger function to auto-populate network_key on INSERT. Must be a trigger as
9896 # Deault values can't call functions that access other columns.
9997 op .execute (
@@ -113,7 +111,7 @@ def upgrade():
113111 """
114112 )
115113 )
116-
114+
117115 # Create trigger to run before INSERT
118116 op .execute (
119117 text (
@@ -125,11 +123,11 @@ def upgrade():
125123 """
126124 )
127125 )
128-
126+
129127 # Recreate the history table with the new column structure
130128 create_history_table ("meta_network" , foreign_tables = None )
131129 grant_standard_table_privileges (f"{ schema_name } .meta_network_hx" )
132-
130+
133131 # Copy existing history data from the old table to the new one
134132 op .execute (
135133 text (
@@ -148,7 +146,7 @@ def upgrade():
148146 """
149147 )
150148 )
151-
149+
152150 # Reset the sequence to continue from the last ID
153151 op .execute (
154152 text (
@@ -160,10 +158,10 @@ def upgrade():
160158 """
161159 )
162160 )
163-
161+
164162 # Update foreign key references in dependent tables to point to the new history table
165163 # meta_station_hx and meta_vars_hx have foreign keys to meta_network_hx
166-
164+
167165 # Drop the foreign key constraints from dependent tables
168166 op .execute (
169167 text (
@@ -175,10 +173,10 @@ def upgrade():
175173 f"ALTER TABLE { schema_name } .meta_vars_hx DROP CONSTRAINT meta_vars_hx_meta_network_hx_id_fkey"
176174 )
177175 )
178-
176+
179177 # Drop the old history table now that data has been copied and FKs removed
180178 op .execute (text (f"DROP TABLE { schema_name } .meta_network_hx_old" ))
181-
179+
182180 # Recreate the foreign key constraints pointing to the new history table
183181 op .execute (
184182 text (
@@ -200,13 +198,15 @@ def upgrade():
200198 """
201199 )
202200 )
203-
201+
204202 # Recreate the history tracking triggers
205203 create_primary_table_triggers ("meta_network" )
206204 create_history_table_triggers ("meta_network" , foreign_tables = None )
207-
205+
208206 # Create indexes on the history table
209- create_history_table_indexes ("meta_network" , "network_id" , foreign_tables = None , extras = None )
207+ create_history_table_indexes (
208+ "meta_network" , "network_id" , foreign_tables = None , extras = None
209+ )
210210
211211
212212def downgrade ():
@@ -216,10 +216,8 @@ def downgrade():
216216 f"DROP TRIGGER IF EXISTS set_network_key_default_trigger ON { schema_name } .meta_network"
217217 )
218218 )
219- op .execute (
220- text (f"DROP FUNCTION IF EXISTS { schema_name } .set_network_key_default()" )
221- )
222-
219+ op .execute (text (f"DROP FUNCTION IF EXISTS { schema_name } .set_network_key_default()" ))
220+
223221 # Drop the constraint and column from primary table
224222 op .drop_constraint (
225223 "uq_meta_network_network_key" ,
@@ -228,13 +226,13 @@ def downgrade():
228226 schema = schema_name ,
229227 )
230228
231- # When dropping we don't have the same issues with column order so we can safely just drop the
229+ # When dropping we don't have the same issues with column order so we can safely just drop the
232230 # column to return to the pre-migration state
233231 op .drop_column ("meta_network" , "network_key" , schema = schema_name )
234-
232+
235233 # Drop the column from history table
236234 op .drop_column ("meta_network_hx" , "network_key" , schema = schema_name )
237-
235+
238236 # Drop the key generation function
239237 op .execute (
240238 text (f"DROP FUNCTION IF EXISTS { schema_name } .gen_network_key_from_name(text)" )
0 commit comments