-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,030 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
backend/lcfs/db/migrations/versions/2025-01-14-18-12_fe03799b4018.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Update other_uses_fossil_derived in fuel_type | ||
Revision ID: fe03799b4018 | ||
Revises: fa98709e7952 | ||
Create Date: 2025-01-14 18:12:43.683691 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "fe03799b4018" | ||
down_revision = "5163af6ba4a4" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# Set other_uses_fossil_derived to false | ||
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' | ||
) | ||
""") | ||
|
||
# Set other_uses_fossil_derived to true | ||
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' | ||
) | ||
""") | ||
|
||
def downgrade() -> None: | ||
# Revert `other_uses_fossil_derived` to original values for false | ||
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' | ||
) | ||
""") | ||
|
||
# Revert `other_uses_fossil_derived` to original values for true | ||
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' | ||
) | ||
""") |
75 changes: 75 additions & 0 deletions
75
backend/lcfs/db/migrations/versions/2025-01-14-23-47_8119d12538df.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""mv for fuel code count | ||
Revision ID: 8119d12538df | ||
Revises: d25e7c47659e | ||
Create Date: 2025-01-14 23:47:28.504150 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "8119d12538df" | ||
down_revision = "fe03799b4018" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.execute( | ||
""" | ||
CREATE MATERIALIZED VIEW mv_fuel_code_count AS | ||
SELECT | ||
CASE fuel_status_id | ||
WHEN 1 THEN 'Draft' | ||
END as status, | ||
COUNT(*) as count | ||
FROM fuel_code | ||
WHERE fuel_status_id = 1 | ||
GROUP BY fuel_status_id; | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" | ||
CREATE UNIQUE INDEX mv_fuel_code_count_idx | ||
ON mv_fuel_code_count (status); | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" | ||
CREATE OR REPLACE FUNCTION refresh_mv_fuel_code_count() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
REFRESH MATERIALIZED VIEW CONCURRENTLY mv_fuel_code_count; | ||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" | ||
CREATE TRIGGER refresh_mv_fuel_code_count_after_change | ||
AFTER INSERT OR UPDATE OR DELETE ON fuel_code | ||
FOR EACH STATEMENT EXECUTE FUNCTION refresh_mv_fuel_code_count(); | ||
""" | ||
) | ||
|
||
# Refresh the materialized view to include existing fuel codes | ||
op.execute( | ||
"REFRESH MATERIALIZED VIEW CONCURRENTLY mv_fuel_code_count;" | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.execute( | ||
"DROP TRIGGER IF EXISTS refresh_mv_fuel_code_count_after_change ON fuel_code;") | ||
op.execute("DROP FUNCTION IF EXISTS refresh_mv_fuel_code_count();") | ||
op.execute("DROP MATERIALIZED VIEW IF EXISTS mv_fuel_code_count;") | ||
# ### end Alembic commands ### |
44 changes: 44 additions & 0 deletions
44
backend/lcfs/db/migrations/versions/2025-01-15-22-48_5bc0ef48739a.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""add truck and marine transport mode | ||
Revision ID: 5bc0ef48739a | ||
Revises: f78e53370ed2 | ||
Create Date: 2025-01-15 22:48:43.582069 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from datetime import datetime | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "5bc0ef48739a" | ||
down_revision = "8119d12538df" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
current_time = datetime.now() | ||
|
||
# Insert Truck and Marine transport modes | ||
op.execute( | ||
""" | ||
INSERT INTO transport_mode (transport_mode, create_date, update_date, create_user, update_user) | ||
VALUES | ||
('Truck', '{}', '{}', 'no_user', 'no_user'), | ||
('Marine', '{}', '{}', 'no_user', 'no_user') | ||
""".format( | ||
current_time, current_time, current_time, current_time | ||
) | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
# Remove Truck and Marine transport modes | ||
op.execute( | ||
""" | ||
DELETE FROM transport_mode | ||
WHERE transport_mode IN ('Truck', 'Marine') | ||
""" | ||
) |
51 changes: 51 additions & 0 deletions
51
backend/lcfs/db/migrations/versions/2025-01-16-19-35_998929392c8b.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""add marine end use | ||
Revision ID: 998929392c8b | ||
Revises: 5bc0ef48739a | ||
Create Date: 2025-01-07 19:35:00.064999 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "998929392c8b" | ||
down_revision = "5bc0ef48739a" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
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; | ||
""" | ||
) | ||
# Energy Effectiveness Ratios | ||
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; | ||
""" | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.execute( | ||
""" | ||
DELETE FROM energy_effectiveness_ratio | ||
WHERE eer_id = 44; | ||
""" | ||
) | ||
op.execute( | ||
""" | ||
DELETE FROM end_use_type | ||
WHERE end_use_type_id = 25; | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from sqlalchemy import Column, Integer, String | ||
from lcfs.db.base import BaseModel | ||
|
||
|
||
class FuelCodeCountView(BaseModel): | ||
__tablename__ = "mv_fuel_code_count" | ||
__table_args__ = { | ||
"extend_existing": True, | ||
"comment": "Materialized view for counting fuel code by status", | ||
} | ||
|
||
status = Column( | ||
String, | ||
primary_key=True, | ||
comment="Status name (e.g. draft, approved, deleted)" | ||
) | ||
count = Column( | ||
Integer, | ||
comment="Count of fuel code for this status" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.