diff --git a/backend/lcfs/db/migrations/versions/2025-01-20-14-40_ec826b9226df.py b/backend/lcfs/db/migrations/versions/2025-01-20-14-40_ec826b9226df.py index 2cf2fee72..5a80389c0 100644 --- a/backend/lcfs/db/migrations/versions/2025-01-20-14-40_ec826b9226df.py +++ b/backend/lcfs/db/migrations/versions/2025-01-20-14-40_ec826b9226df.py @@ -1,9 +1,5 @@ """ -Consolidated Migration: Squashed data updates from historical migrations - -Combines multiple migration files into one, preserving the chronological order -of their 'upgrade' steps and reversing it for 'downgrade'. Comments mark the -original revision IDs. +Initial seeding of application data Revision ID: ec826b9226df Revises: f217cd32474b @@ -24,365 +20,10 @@ def upgrade(): """ - Consolidated UPGRADE logic from all migrations, in ascending date order: - (1) 043c52082a3b (2024-11-27) - (2) aeaa26f5cdd5 (2024-12-02) - (3) 9206124a098b (2024-12-04) - (4) 8491890dd688 (2024-12-04) - (5) d4104af84f2b (2024-12-05) - (6) 26ab15f8ab18 (2024-12-06) - (7) 7ae38a8413ab (2024-12-09) - (8) 5d729face5ab (2024-12-12) - (9) 851e09cf8661 (2024-12-17) - (10) 59873cafbcd8 (2024-12-20) - (11) 5fbcb508c1be (2024-12-21) - (12) ab04810d4d7c (2024-12-22) - (13) d9cdd9fca0ce (2024-12-24) - (14) bfa7bbb1eea3 (2025-01-03) - (15) e883ad1f0f60 (2025-01-03) - (16) 94306eca5261 (2025-01-06) - (17) fa98709e7952 (2025-01-06) - (18) 998929392c8b (2025-01-07) - (19) fe03799b4018 (2025-01-14) - (20) 5bc0ef48739a (2025-01-15) - """ - - # - # (1) 043c52082a3b (2024-11-27): Add 'Marine-domestic' and 'Marine-international' transport modes - # - current_time = datetime.now() - # Update Marine -> Marine-domestic - op.execute( - f""" - UPDATE transport_mode - SET transport_mode = 'Marine-domestic', - update_date = '{current_time}', - update_user = 'no-user' - WHERE transport_mode = 'Marine' - """ - ) - - # Insert Marine-international - op.execute( - f""" - INSERT INTO transport_mode (transport_mode, create_date, update_date, create_user, update_user) - VALUES ('Marine-international', '{current_time}', '{current_time}', 'no_user', 'no_user') - """ - ) - - # - # (2) aeaa26f5cdd5 (2024-12-02): Replace "Other" in level_of_equipment with long description - # - op.execute( - """ - UPDATE level_of_equipment - SET name = 'Other - Additional information provided in notes field' - WHERE name = 'Other' - """ - ) - - # - # (4) 8491890dd688 (2024-12-04): Data fixes for FuelType and AllocationTransactionType - # (Originally used Python model references; replaced with raw SQL.) - # - # - fuel_type_id = 6 -> units=Kilograms - # - fuel_type_id = 20 -> fossil_derived=False - # - allocation_transaction_type_id = 2 -> "Allocated to" - # - allocation_transaction_type_id = 1 -> "Allocated from" - # - op.execute( - """ - UPDATE fuel_type - SET units = 'Kilograms' - WHERE fuel_type_id = 6 - """ - ) - op.execute( - """ - UPDATE fuel_type - SET fossil_derived = false - WHERE fuel_type_id = 20 - """ - ) - op.execute( - """ - UPDATE allocation_transaction_type - SET type = 'Allocated to', - description = 'Fuel allocated to another supplier under an allocation agreement' - WHERE allocation_transaction_type_id = 2 - """ - ) - op.execute( - """ - UPDATE allocation_transaction_type - SET type = 'Allocated from', - description = 'Fuel allocated from another supplier under an allocation agreement' - WHERE allocation_transaction_type_id = 1 + Initial seeding of application data """ - ) - - # - # (5) d4104af84f2b (2024-12-05): Update Notification Types and remove old data/enum - # - # 1) DELETE FROM notification_type - # 2) Convert name column to VARCHAR from old enum - # 3) DROP TYPE notification_type_enum_v2 if exists - # - op.execute("DELETE FROM notification_type;") - - with op.batch_alter_table("notification_type") as batch_op: - batch_op.alter_column( - "name", - existing_type=sa.String(length=255), # in the final script, assume string - type_=sa.String(length=255), - existing_nullable=False, - ) - - op.execute("DROP TYPE IF EXISTS notification_type_enum_v2;") - # - # (6) 26ab15f8ab18 (2024-12-06): Update end_use_type (IDs 14-21) and adjust additional_carbon_intensity, EER - # - current_time = datetime.now() - updates = [ - (14, "Aircraft"), - (15, "Compression-ignition engine- Marine, general"), - ( - 16, - "Compression-ignition engine- Marine, operated within 51 to 75% of load range", - ), - ( - 17, - "Compression-ignition engine- Marine, operated within 76 to 100% of load range", - ), - ( - 18, - "Compression-ignition engine- Marine, with methane slip reduction kit- General", - ), - ( - 19, - "Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 51 to 75% of load range", - ), - ( - 20, - "Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 76 to 100% of load range", - ), - ( - 21, - "Compression-ignition engine- Marine, unknown whether kit is installed or average operating load range", - ), - ] - for end_use_id, new_type in updates: - op.execute( - f""" - UPDATE end_use_type - SET type = '{new_type}', - sub_type = NULL, - intended_use = true, - update_date = '{current_time}', - update_user = 'no_user' - WHERE end_use_type_id = {end_use_id} - """ - ) - - # Adjust additional_carbon_intensity (IDs 1-9) - uci_updates = [ - (1, 7, 5, None, 0), - (2, None, 5, None, 0), - (3, 7, 5, 15, 27.3), - (4, 7, 5, 16, 17.8), - (5, 7, 5, 17, 12.2), - (6, 7, 5, 18, 10.6), - (7, 7, 5, 19, 8.4), - (8, 7, 5, 20, 8.0), - (9, 7, 5, 21, 27.3), - ] - for uci_id, fuel_type_id, uom_id, end_use_type_id, intensity in uci_updates: - # If 'None' for end_use_type_id or fuel_type_id, set them to NULL - if fuel_type_id and end_use_type_id: - op.execute( - f""" - UPDATE additional_carbon_intensity - SET fuel_type_id = {fuel_type_id}, - uom_id = {uom_id}, - end_use_type_id = {end_use_type_id}, - intensity = {intensity}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE additional_uci_id = {uci_id} - """ - ) - elif fuel_type_id: - op.execute( - f""" - UPDATE additional_carbon_intensity - SET fuel_type_id = {fuel_type_id}, - uom_id = {uom_id}, - end_use_type_id = NULL, - intensity = {intensity}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE additional_uci_id = {uci_id} - """ - ) - else: - op.execute( - f""" - UPDATE additional_carbon_intensity - SET fuel_type_id = NULL, - uom_id = {uom_id}, - end_use_type_id = NULL, - intensity = {intensity}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE additional_uci_id = {uci_id} - """ - ) - - # Adjust EER for IDs 14-24 - eer_updates = [ - (14, 2, 3, 10, 2.8), - (15, 2, 3, 11, 2.4), - (16, 2, 3, 2, 1.0), - (17, 2, 6, 3, 1.8), - (18, 2, 6, 2, 0.9), - (19, 2, 13, None, 0.9), - (20, 3, 3, None, 2.5), - (21, 3, 11, None, 1.0), - (22, 2, 7, 15, 1.0), - (23, 2, 7, 16, 1.0), - (24, 2, 7, 17, 1.0), - ] - for eer_id, fc_id, ft_id, eut_id, ratio in eer_updates: - if eut_id: - op.execute( - f""" - UPDATE energy_effectiveness_ratio - SET fuel_category_id = {fc_id}, - fuel_type_id = {ft_id}, - end_use_type_id = {eut_id}, - ratio = {ratio}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE eer_id = {eer_id} - """ - ) - else: - op.execute( - f""" - UPDATE energy_effectiveness_ratio - SET fuel_category_id = {fc_id}, - fuel_type_id = {ft_id}, - end_use_type_id = NULL, - ratio = {ratio}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE eer_id = {eer_id} - """ - ) - - # - # (7) 7ae38a8413ab (2024-12-09): Mark volume-based fuel types as other_uses_fossil_derived = true, and gas-based as false - # - current_time = datetime.now() - op.execute( - f""" - UPDATE fuel_type - SET other_uses_fossil_derived = true, - update_date = '{current_time}', - update_user = 'no_user' - WHERE fuel_type IN ( - 'Alternative jet fuel', - 'Biodiesel', - 'Ethanol', - 'HDRD', - 'Renewable gasoline', - 'Renewable naphtha' - ) - """ - ) - op.execute( - f""" - UPDATE fuel_type - SET other_uses_fossil_derived = false, - update_date = '{current_time}', - update_user = 'no_user' - WHERE fuel_type IN ( - 'CNG', - 'Electricity', - 'Hydrogen', - 'LNG', - 'Propane' - ) - """ - ) - - # - # (8) 5d729face5ab (2024-12-12): Update default_carbon_intensity for 'Other diesel' (fuel_type_id=20) to 100.21 - # - op.execute( - """ - UPDATE fuel_type - SET default_carbon_intensity = 100.21 - WHERE fuel_type_id = 20 - """ - ) - - # - # (9) 851e09cf8661 (2024-12-17): Add "default_carbon_intensity" column to "fuel_category" and populate - # - op.execute( - """ - UPDATE "fuel_category" - SET "default_carbon_intensity" = 88.83 - WHERE "description" = 'Jet fuel'; - """ - ) - op.execute( - """ - UPDATE "fuel_category" - SET "default_carbon_intensity" = 100.21 - WHERE "description" = 'Diesel'; - """ - ) - op.execute( - """ - UPDATE "fuel_category" - SET "default_carbon_intensity" = 93.67 - WHERE "description" = 'Gasoline'; - """ - ) - op.alter_column( - "fuel_category", - "default_carbon_intensity", - existing_type=sa.Numeric(precision=10, scale=2), - nullable=False, - ) - - # - # (10) 59873cafbcd8 (2024-12-20): Update 'Other diesel' to set fossil_derived = false, other_uses_fossil_derived = false - # - op.execute( - """ - UPDATE fuel_type - SET fossil_derived = false, other_uses_fossil_derived = false - WHERE fuel_type = 'Other diesel' - """ - ) - - # - # (11) 5fbcb508c1be (2024-12-21): Rename an end_use_type for LNG - # - op.execute( - """ - UPDATE end_use_type - SET type = 'Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 26 to 75% of load range' - WHERE end_use_type_id = 19 - """ - ) - - # - # (13) d9cdd9fca0ce (2024-12-24): Move common seeders to migrations + # d9cdd9fca0ce (2024-12-24): Move common seeders to migrations # # (Full block of reference data insertion) # @@ -543,12 +184,13 @@ def upgrade(): (16, 'Compression-ignition engine- Marine, operated within 51 to 75% of load range', TRUE), (17, 'Compression-ignition engine- Marine, operated within 76 to 100% of load range', TRUE), (18, 'Compression-ignition engine- Marine, with methane slip reduction kit- General', TRUE), - (19, 'Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 51 to 75% of load range', TRUE), + (19, 'Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 26 to 75% of load range', TRUE), (20, 'Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 76 to 100% of load range', TRUE), (21, 'Compression-ignition engine- Marine, unknown whether kit is installed or average operating load range', TRUE), (22, 'Unknown engine type', TRUE), (23, 'Other (i.e. road transportation)', TRUE), - (24, 'Any', TRUE) + (24, 'Any', TRUE), + (25, 'Marine', TRUE) ON CONFLICT (end_use_type_id) DO NOTHING; """ ) @@ -580,22 +222,22 @@ def upgrade(): INSERT INTO fuel_type (fuel_type_id, fuel_type, fossil_derived, other_uses_fossil_derived, provision_1_id, provision_2_id, default_carbon_intensity, units, unrecognized) VALUES - (1, 'Biodiesel', FALSE, FALSE, 2, 3, 100.21, 'Litres', FALSE), - (2, 'CNG', FALSE, TRUE, 2, 3, 63.91, 'Cubic_metres', FALSE), - (3, 'Electricity', FALSE, TRUE, 2, 3, 12.14, 'Kilowatt_hour', FALSE), - (4, 'Ethanol', FALSE, FALSE, 2, 3, 93.67, 'Litres', FALSE), - (5, 'HDRD', FALSE, FALSE, 2, 3, 100.21, 'Litres', FALSE), - (6, 'Hydrogen', FALSE, TRUE, 2, 3, 123.96, 'Kilograms', FALSE), - (7, 'LNG', FALSE, TRUE, 2, 3, 90.11, 'Kilograms', FALSE), - (11, 'Alternative jet fuel', FALSE, FALSE, 2, 3, 88.83, 'Litres', FALSE), - (13, 'Propane', FALSE, TRUE, 2, 3, 79.87, 'Litres', FALSE), - (14, 'Renewable gasoline', FALSE, FALSE, 2, 3, 93.67, 'Litres', FALSE), - (15, 'Renewable naphtha', FALSE, FALSE, 2, 3, 93.67, 'Litres', FALSE), + (1, 'Biodiesel', FALSE, TRUE, 2, 3, 100.21, 'Litres', FALSE), + (2, 'CNG', FALSE, FALSE, 2, 3, 63.91, 'Cubic_metres', FALSE), + (3, 'Electricity', FALSE, FALSE, 2, 3, 12.14, 'Kilowatt_hour', FALSE), + (4, 'Ethanol', FALSE, TRUE, 2, 3, 93.67, 'Litres', FALSE), + (5, 'HDRD', FALSE, TRUE, 2, 3, 100.21, 'Litres', FALSE), + (6, 'Hydrogen', FALSE, FALSE, 2, 3, 123.96, 'Kilograms', FALSE), + (7, 'LNG', FALSE, FALSE, 2, 3, 90.11, 'Kilograms', FALSE), + (11, 'Alternative jet fuel', FALSE, TRUE, 2, 3, 88.83, 'Litres', FALSE), + (13, 'Propane', FALSE, FALSE, 2, 3, 79.87, 'Litres', FALSE), + (14, 'Renewable gasoline', FALSE, TRUE, 2, 3, 93.67, 'Litres', FALSE), + (15, 'Renewable naphtha', FALSE, TRUE, 2, 3, 93.67, 'Litres', FALSE), (16, 'Fossil-derived diesel', TRUE, TRUE, 1, NULL, 94.38, 'Litres', FALSE), (17, 'Fossil-derived gasoline', TRUE, TRUE, 1, NULL, 93.67, 'Litres', FALSE), (18, 'Fossil-derived jet fuel', TRUE, TRUE, 1, NULL, 88.83, 'Litres', FALSE), (19, 'Other', FALSE, FALSE, 2, 3, 0, 'Litres', TRUE), - (20, 'Other diesel', FALSE, FALSE, 1, NULL, 100.21, 'Litres', FALSE) + (20, 'Other diesel fuel', FALSE, TRUE, 3, NULL, 100.21, 'Litres', FALSE) ON CONFLICT (fuel_type_id) DO NOTHING; """ ) @@ -624,9 +266,9 @@ def upgrade(): (16, 2, 3, 2, 1.0, TRUE), (17, 2, 6, 3, 1.8, TRUE), (18, 2, 6, 2, 0.9, TRUE), - (19, 2, 13, 24, 0.9, TRUE), - (20, 3, 3, 24, 2.5, TRUE), - (21, 3, 11, 24, 1.0, TRUE), + (19, 2, 13, NULL, 0.9, TRUE), + (20, 3, 3, NULL, 2.5, TRUE), + (21, 3, 11, NULL, 1.0, TRUE), (22, 2, 7, 15, 1.0, TRUE), (23, 2, 7, 16, 1.0, TRUE), (24, 2, 7, 17, 1.0, TRUE), @@ -648,7 +290,8 @@ def upgrade(): (40, 2, 19, 24, 1.0, TRUE), (41, 3, 7, 24, 1.0, TRUE), (42, 2, 20, 24, 1.0, TRUE), - (43, 1, 4, 24, 1.0, TRUE) + (43, 1, 4, 24, 1.0, TRUE), + (44, 2, 3, 25, 2.5, TRUE) ON CONFLICT (eer_id) DO NOTHING; """ ) @@ -771,7 +414,9 @@ def upgrade(): (2, 'Rail'), (3, 'Marine-domestic'), (4, 'Adjacent'), - (5, 'Pipeline') + (5, 'Pipeline'), + (6, 'Marine-international'), + (7, 'Marine') ON CONFLICT (transport_mode_id) DO NOTHING; """ ) @@ -928,18 +573,6 @@ def upgrade(): """ ) - # - # (14) bfa7bbb1eea3 (2025-01-03): Update 'Other diesel' -> 'Other diesel fuel' - # - op.execute( - """ - UPDATE fuel_type - SET fuel_type = 'Other diesel fuel', - provision_1_id = 3 - WHERE fuel_type = 'Other diesel'; - """ - ) - # # (16) 94306eca5261 (2025-01-06): Insert is_legacy to provision_of_the_act # @@ -1017,7 +650,7 @@ def upgrade(): ( 'Natural gas-based gasoline', FALSE, - TRUE, + FALSE, 90.07, 'Litres', FALSE, @@ -1028,7 +661,7 @@ def upgrade(): ( 'Petroleum-based diesel', FALSE, - TRUE, + FALSE, 94.76, 'Litres', FALSE, @@ -1039,7 +672,7 @@ def upgrade(): ( 'Petroleum-based gasoline', FALSE, - TRUE, + FALSE, 88.14, 'Litres', FALSE, @@ -1113,555 +746,6 @@ def upgrade(): """ ) - # - # (18) 998929392c8b (2025-01-07): Add marine end use (end_use_type_id=25) plus EER record (eer_id=44) - # - op.execute( - """ - INSERT INTO end_use_type (end_use_type_id, type, intended_use) - VALUES (25, 'Marine', TRUE) - ON CONFLICT (end_use_type_id) DO NOTHING; - """ - ) - op.execute( - """ - INSERT INTO energy_effectiveness_ratio ( - eer_id, fuel_category_id, fuel_type_id, end_use_type_id, ratio, effective_status - ) - VALUES (44, 2, 3, 25, 2.5, TRUE) - ON CONFLICT (eer_id) DO NOTHING; - """ - ) - - # - # (19) fe03799b4018 (2025-01-14): Update other_uses_fossil_derived for certain fuel types - # - op.execute( - """ - UPDATE fuel_type - SET other_uses_fossil_derived = false - WHERE fuel_type IN ( - 'CNG', 'Electricity', 'Hydrogen', 'LNG', 'Propane', - 'Natural gas-based gasoline', 'Petroleum-based diesel', - 'Petroleum-based gasoline' - ) - """ - ) - op.execute( - """ - UPDATE fuel_type - SET other_uses_fossil_derived = true - WHERE fuel_type IN ( - 'Alternative jet fuel', 'Biodiesel', 'Ethanol', 'HDRD', - 'Other diesel fuel', 'Renewable gasoline', 'Renewable naphtha' - ) - """ - ) - - # - # (20) 5bc0ef48739a (2025-01-15): Add Truck and Marine transport mode - # (Though it references 'Marine', we already changed it above. We'll do as-is.) - # - current_time = datetime.now() - op.execute( - f""" - INSERT INTO transport_mode (transport_mode, create_date, update_date, create_user, update_user) - VALUES - ('Truck', '{current_time}', '{current_time}', 'no_user', 'no_user'), - ('Marine', '{current_time}', '{current_time}', 'no_user', 'no_user') - """ - ) - def downgrade(): - """ - Consolidated DOWNGRADE logic in reverse chronological order: - - (20) 5bc0ef48739a (2025-01-15) - (19) fe03799b4018 (2025-01-14) - (18) 998929392c8b (2025-01-07) - (17) fa98709e7952 (2025-01-06) - (16) 94306eca5261 (2025-01-06) - (15) e883ad1f0f60 (2025-01-03) - (14) bfa7bbb1eea3 (2025-01-03) - (13) d9cdd9fca0ce (2024-12-24) - (12) ab04810d4d7c (2024-12-22) - (11) 5fbcb508c1be (2024-12-21) - (10) 59873cafbcd8 (2024-12-20) - (9) 851e09cf8661 (2024-12-17) - (8) 5d729face5ab (2024-12-12) - (7) 7ae38a8413ab (2024-12-09) - (6) 26ab15f8ab18 (2024-12-06) - (5) d4104af84f2b (2024-12-05) - (4) 8491890dd688 (2024-12-04) - (3) 9206124a098b (2024-12-04) - (2) aeaa26f5cdd5 (2024-12-02) - (1) 043c52082a3b (2024-11-27) - """ - - # - # (20) 5bc0ef48739a (Downgrade): Remove Truck, Marine transport modes - # - op.execute( - """ - DELETE FROM transport_mode - WHERE transport_mode IN ('Truck', 'Marine') - """ - ) - - # - # (19) fe03799b4018 (Downgrade): Revert the other_uses_fossil_derived changes - # - op.execute( - """ - UPDATE fuel_type - SET other_uses_fossil_derived = true - WHERE fuel_type IN ( - 'CNG', 'Electricity', 'Hydrogen', 'LNG', 'Propane', - 'Natural gas-based gasoline', 'Petroleum-based diesel', - 'Petroleum-based gasoline' - ) - """ - ) - op.execute( - """ - UPDATE fuel_type - SET other_uses_fossil_derived = false - WHERE fuel_type IN ( - 'Alternative jet fuel', 'Biodiesel', 'Ethanol', 'HDRD', - 'Other diesel fuel', 'Renewable gasoline', 'Renewable naphtha' - ) - """ - ) - - # - # (18) 998929392c8b (Downgrade): Remove EER (id=44) + remove end_use_type (id=25) - # - op.execute( - """ - DELETE FROM energy_effectiveness_ratio - WHERE eer_id = 44; - """ - ) - op.execute( - """ - DELETE FROM end_use_type - WHERE end_use_type_id = 25; - """ - ) - - # - # (17) fa98709e7952 (Downgrade): Remove the newly inserted "Natural gas-based gasoline," etc. - # - op.execute( - """ - DELETE FROM energy_effectiveness_ratio - WHERE fuel_type_id IN ( - SELECT fuel_type_id - FROM fuel_type - WHERE fuel_type IN ( - 'Natural gas-based gasoline', - 'Petroleum-based diesel', - 'Petroleum-based gasoline' - ) - ); - """ - ) - op.execute( - """ - DELETE FROM energy_density - WHERE fuel_type_id IN ( - SELECT fuel_type_id - FROM fuel_type - WHERE fuel_type IN ( - 'Natural gas-based gasoline', - 'Petroleum-based diesel', - 'Petroleum-based gasoline' - ) - ); - """ - ) - op.execute( - """ - DELETE FROM fuel_type - WHERE fuel_type IN ( - 'Natural gas-based gasoline', - 'Petroleum-based diesel', - 'Petroleum-based gasoline' - ); - """ - ) - - # - # (16) 94306eca5261 (Downgrade): Drop is_legacy from provision_of_the_act - # - op.drop_column("provision_of_the_act", "is_legacy") - - # - # (14) bfa7bbb1eea3 (Downgrade): Revert fuel_type = 'Other diesel fuel' -> 'Other diesel' - # - op.execute( - """ - UPDATE fuel_type - SET fuel_type = 'Other diesel', - provision_1_id = 1 - WHERE fuel_type = 'Other diesel fuel'; - """ - ) - - # - # (13) d9cdd9fca0ce (Downgrade): Clear all seeded data, in groups - # - table_groups = [ - [ - "fuel_instance", - "target_carbon_intensity", - "additional_carbon_intensity", - "energy_density", - "energy_effectiveness_ratio", - "fuel_code", # if it exists - ], - [ - "notification_channel", - "notification_type", - "end_user_type", - "allocation_transaction_type", - "compliance_report_status", - "level_of_equipment", - ], - [ - "fuel_type", - "fuel_category", - "transport_mode", - "fuel_code_prefix", - "fuel_code_status", - "end_use_type", - ], - [ - "expected_use_type", - "unit_of_measure", - "provision_of_the_act", - "initiative_agreement_status", - "admin_adjustment_status", - "transfer_category", - "transfer_status", - "role", - "organization_status", - "organization_type", - "compliance_period", - ], - ] - for group in table_groups: - tables_list = ", ".join(group) - op.execute(f"TRUNCATE TABLE {tables_list} CASCADE;") - - # - # (12) ab04810d4d7c (Downgrade): Drop unique constraint from target_carbon_intensity - # - op.drop_constraint( - "uq_target_carbon_intensity_compliance_fuel", - "target_carbon_intensity", - type_="unique", - ) - - # - # (11) 5fbcb508c1be (Downgrade): Rename end_use_type_id=19 back - # - op.execute( - """ - UPDATE end_use_type - SET type = 'Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 51 to 75% of load range' - WHERE end_use_type_id = 19 - """ - ) - - # - # (10) 59873cafbcd8 (Downgrade): Restore 'Other diesel' fossil_derived = true, other_uses_fossil_derived = true - # - op.execute( - """ - UPDATE fuel_type - SET fossil_derived = true, other_uses_fossil_derived = true - WHERE fuel_type = 'Other diesel' - """ - ) - - # - # (9) 851e09cf8661 (Downgrade): Drop the default_carbon_intensity column from fuel_category - # - op.drop_column("fuel_category", "default_carbon_intensity") - - # - # (8) 5d729face5ab (Downgrade): Revert default_carbon_intensity of 'Other diesel' (id=20) to 94.38 - # - op.execute( - """ - UPDATE fuel_type - SET default_carbon_intensity = 94.38 - WHERE fuel_type_id = 20 - """ - ) - - # - # (7) 7ae38a8413ab (Downgrade): Revert volume-based fuels to false, gas-based to true - # - current_time = datetime.now() - op.execute( - f""" - UPDATE fuel_type - SET other_uses_fossil_derived = false, - update_date = '{current_time}', - update_user = 'no_user' - WHERE fuel_type IN ( - 'Alternative jet fuel', - 'Biodiesel', - 'Ethanol', - 'HDRD', - 'Renewable gasoline', - 'Renewable naphtha' - ) - """ - ) - op.execute( - f""" - UPDATE fuel_type - SET other_uses_fossil_derived = true, - update_date = '{current_time}', - update_user = 'no_user' - WHERE fuel_type IN ( - 'CNG', - 'Electricity', - 'Hydrogen', - 'LNG', - 'Propane' - ) - """ - ) - - # - # (6) 26ab15f8ab18 (Downgrade): Restore original end_use_type + additional_carbon_intensity + EER - # - current_time = datetime.now() - original_values = [ - (14, "Marine", "General"), - (15, "Marine", "Operated within 51 to 75% of load range"), - (16, "Marine", "Operated within 76 to 100% of load range"), - (17, "Marine, w/ methane slip reduction kit", "General"), - ( - 18, - "Marine, w/ methane slip reduction kit", - "Operated within 51 to 75% of load range", - ), - ( - 19, - "Marine, w/ methane slip reduction kit", - "Operated within 76 to 100% of load range", - ), - (20, "Unknown", None), - (21, "Aircraft", None), - ] - for end_use_id, type_name, sub_type in original_values: - if sub_type: - op.execute( - f""" - UPDATE end_use_type - SET type = '{type_name}', - sub_type = '{sub_type}', - update_date = '{current_time}', - update_user = 'no_user' - WHERE end_use_type_id = {end_use_id} - """ - ) - else: - op.execute( - f""" - UPDATE end_use_type - SET type = '{type_name}', - sub_type = NULL, - update_date = '{current_time}', - update_user = 'no_user' - WHERE end_use_type_id = {end_use_id} - """ - ) - - uci_originals = [ - (1, 7, 5, 14, 27.3), - (2, 7, 5, 15, 17.8), - (3, 7, 5, 16, 12.2), - (4, 7, 5, 17, 10.6), - (5, 7, 5, 18, 8.4), - (6, 7, 5, 19, 8.0), - (7, 7, 5, 20, 27.3), - (8, 7, 5, None, 0), - (9, None, 5, None, 0), - ] - for uci_id, fuel_type_id, uom_id, eut_id, intensity in uci_originals: - if fuel_type_id and eut_id: - op.execute( - f""" - UPDATE additional_carbon_intensity - SET fuel_type_id = {fuel_type_id}, - uom_id = {uom_id}, - end_use_type_id = {eut_id}, - intensity = {intensity}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE additional_uci_id = {uci_id} - """ - ) - elif fuel_type_id: - op.execute( - f""" - UPDATE additional_carbon_intensity - SET fuel_type_id = {fuel_type_id}, - uom_id = {uom_id}, - end_use_type_id = NULL, - intensity = {intensity}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE additional_uci_id = {uci_id} - """ - ) - else: - op.execute( - f""" - UPDATE additional_carbon_intensity - SET fuel_type_id = NULL, - uom_id = {uom_id}, - end_use_type_id = NULL, - intensity = {intensity}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE additional_uci_id = {uci_id} - """ - ) - - eer_originals = [ - (14, 2, 3, 14, 2.5), - (15, 2, 3, 10, 2.8), - (16, 2, 3, 11, 2.4), - (17, 2, 3, 2, 1.0), - (18, 2, 6, 3, 1.8), - (19, 2, 6, 2, 0.9), - (20, 2, 7, 12, 1.0), - (21, 2, 7, 2, 0.9), - (22, 2, 13, None, 0.9), - (23, 3, 3, None, 2.5), - (24, 3, 11, None, 1.0), - ] - for eer_id, fc_id, ft_id, eut_id, ratio in eer_originals: - if eut_id: - op.execute( - f""" - UPDATE energy_effectiveness_ratio - SET fuel_category_id = {fc_id}, - fuel_type_id = {ft_id}, - end_use_type_id = {eut_id}, - ratio = {ratio}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE eer_id = {eer_id} - """ - ) - else: - op.execute( - f""" - UPDATE energy_effectiveness_ratio - SET fuel_category_id = {fc_id}, - fuel_type_id = {ft_id}, - end_use_type_id = NULL, - ratio = {ratio}, - update_date = '{current_time}', - update_user = 'no_user' - WHERE eer_id = {eer_id} - """ - ) - - # - # (5) d4104af84f2b (Downgrade): Restore notification_type table to old enum-based approach - # - op.execute("DELETE FROM notification_type;") - - # Recreate old enum - notification_type_enum_v2 = ENUM( - "TRANSFER_PARTNER_UPDATE", - "TRANSFER_DIRECTOR_REVIEW", - "INITIATIVE_APPROVED", - "INITIATIVE_DA_REQUEST", - "SUPPLEMENTAL_REQUESTED", - "DIRECTOR_ASSESSMENT", - name="notification_type_enum_v2", - ) - notification_type_enum_v2.create(op.get_bind(), checkfirst=False) - - # Convert the name column back to enum - with op.batch_alter_table("notification_type") as batch_op: - batch_op.alter_column( - "name", - type_=notification_type_enum_v2, - postgresql_using="name::notification_type_enum_v2", - existing_type=sa.String(length=255), - existing_nullable=False, - ) - - # Re-insert old data - op.execute( - """ - INSERT INTO notification_type (notification_type_id, name, description, email_content, create_user, update_user) - VALUES - (1, 'TRANSFER_PARTNER_UPDATE', 'Transfer partner update notification', 'Email content for transfer partner update', 'system', 'system'), - (2, 'TRANSFER_DIRECTOR_REVIEW', 'Director review notification', 'Email content for director review', 'system', 'system'), - (3, 'INITIATIVE_APPROVED', 'Initiative approved notification', 'Email content for initiative approval', 'system', 'system'), - (4, 'INITIATIVE_DA_REQUEST', 'DA request notification', 'Email content for DA request', 'system', 'system'), - (5, 'SUPPLEMENTAL_REQUESTED', 'Supplemental requested notification', 'Email content for supplemental request', 'system', 'system'), - (6, 'DIRECTOR_ASSESSMENT', 'Director assessment notification', 'Email content for director assessment', 'system', 'system'); - """ - ) - - op.execute( - """ - SELECT setval('notification_type_notification_type_id_seq', - (SELECT MAX(notification_type_id) FROM notification_type)); - """ - ) - - # - # (4) 8491890dd688 (Downgrade): No direct reversion steps were provided, so pass. - # - # The original only did data updates; no direct reverts specified. pass - - # - # (3) 9206124a098b (Downgrade): Remove column organization_name from final_supply_equipment - # - op.drop_column("final_supply_equipment", "organization_name") - - # - # (2) aeaa26f5cdd5 (Downgrade): Revert "Other - Additional information..." to "Other" in level_of_equipment - # - op.execute( - """ - UPDATE level_of_equipment - SET name = 'Other' - WHERE name = 'Other - Additional information provided in notes field' - """ - ) - - # - # (1) 043c52082a3b (Downgrade): Revert Marine-domestic -> Marine and remove Marine-international - # - op.execute( - f""" - UPDATE transport_mode - SET transport_mode = 'Marine', - update_date = '{datetime.utcnow()}', - update_user = 'no_user' - WHERE transport_mode = 'Marine-domestic' - """ - ) - op.execute( - """ - DELETE FROM transport_mode - WHERE transport_mode = 'Marine-international' - """ - )