-
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.
Merge branch 'release-0.2.0' into update-node-version-0.2.0
- Loading branch information
Showing
93 changed files
with
2,121 additions
and
461 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
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
238 changes: 238 additions & 0 deletions
238
backend/lcfs/db/migrations/versions/2025-01-13-22-13_f78e53370ed2.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,238 @@ | ||
"""Add CR to Transaction Aggregate | ||
Revision ID: f78e53370ed2 | ||
Revises: d25e7c47659e | ||
Create Date: 2025-01-13 22:13:48.610890 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "f78e53370ed2" | ||
down_revision = "d25e7c47659e" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.execute("DROP MATERIALIZED VIEW mv_transaction_aggregate;") | ||
op.execute( | ||
""" | ||
CREATE MATERIALIZED VIEW mv_transaction_aggregate AS | ||
SELECT | ||
t.transfer_id AS transaction_id, | ||
'Transfer' AS transaction_type, | ||
NULL AS description, | ||
org_from.organization_id AS from_organization_id, | ||
org_from.name AS from_organization, | ||
org_to.organization_id AS to_organization_id, | ||
org_to.name AS to_organization, | ||
t.quantity, | ||
t.price_per_unit, | ||
ts.status::text AS status, | ||
NULL AS compliance_period, | ||
t.from_org_comment AS COMMENT, | ||
tc.category, | ||
( | ||
SELECT | ||
th.create_date | ||
FROM | ||
transfer_history th | ||
WHERE | ||
th.transfer_id = t.transfer_id | ||
AND th.transfer_status_id = 6) AS recorded_date, NULL AS approved_date, t.transaction_effective_date, t.update_date, t.create_date | ||
FROM | ||
transfer t | ||
JOIN organization org_from ON t.from_organization_id = org_from.organization_id | ||
JOIN organization org_to ON t.to_organization_id = org_to.organization_id | ||
JOIN transfer_status ts ON t.current_status_id = ts.transfer_status_id | ||
LEFT JOIN transfer_category tc ON t.transfer_category_id = tc.transfer_category_id | ||
UNION ALL | ||
SELECT | ||
ia.initiative_agreement_id AS transaction_id, | ||
'InitiativeAgreement' AS transaction_type, | ||
NULL AS description, | ||
NULL AS from_organization_id, | ||
NULL AS from_organization, | ||
org.organization_id AS to_organization_id, | ||
org.name AS to_organization, | ||
ia.compliance_units AS quantity, | ||
NULL AS price_per_unit, | ||
ias.status::text AS status, | ||
NULL AS compliance_period, | ||
ia.gov_comment AS COMMENT, | ||
NULL AS category, | ||
NULL AS recorded_date, | ||
( | ||
SELECT | ||
iah.create_date | ||
FROM | ||
initiative_agreement_history iah | ||
WHERE | ||
iah.initiative_agreement_id = ia.initiative_agreement_id | ||
AND iah.initiative_agreement_status_id = 3) AS approved_date, ia.transaction_effective_date, ia.update_date, ia.create_date | ||
FROM | ||
initiative_agreement ia | ||
JOIN organization org ON ia.to_organization_id = org.organization_id | ||
JOIN initiative_agreement_status ias ON ia.current_status_id = ias.initiative_agreement_status_id | ||
UNION ALL | ||
SELECT | ||
aa.admin_adjustment_id AS transaction_id, | ||
'AdminAdjustment' AS transaction_type, | ||
NULL AS description, | ||
NULL AS from_organization_id, | ||
NULL AS from_organization, | ||
org.organization_id AS to_organization_id, | ||
org.name AS to_organization, | ||
aa.compliance_units AS quantity, | ||
NULL AS price_per_unit, | ||
aas.status::text AS status, | ||
NULL AS compliance_period, | ||
aa.gov_comment AS COMMENT, | ||
NULL AS category, | ||
NULL AS recorded_date, | ||
( | ||
SELECT | ||
aah.create_date | ||
FROM | ||
admin_adjustment_history aah | ||
WHERE | ||
aah.admin_adjustment_id = aa.admin_adjustment_id | ||
AND aah.admin_adjustment_status_id = 3) AS approved_date, aa.transaction_effective_date, aa.update_date, aa.create_date | ||
FROM | ||
admin_adjustment aa | ||
JOIN organization org ON aa.to_organization_id = org.organization_id | ||
JOIN admin_adjustment_status aas ON aa.current_status_id = aas.admin_adjustment_status_id | ||
UNION ALL | ||
SELECT | ||
cr.compliance_report_id AS transaction_id, | ||
'ComplianceReport' AS transaction_type, | ||
cr.nickname AS description, | ||
NULL AS from_organization_id, | ||
NULL AS from_organization, | ||
org.organization_id AS to_organization_id, | ||
org.name AS to_organization, | ||
tr.compliance_units AS quantity, | ||
NULL AS price_per_unit, | ||
crs.status::text AS status, | ||
cp.description AS compliance_period, | ||
NULL AS COMMENT, | ||
NULL AS category, | ||
NULL AS recorded_date, | ||
NULL AS approved_date, | ||
NULL AS transaction_effective_date, | ||
cr.update_date, | ||
cr.create_date | ||
FROM | ||
compliance_report cr | ||
JOIN organization org ON cr.organization_id = org.organization_id | ||
JOIN compliance_report_status crs ON cr.current_status_id = crs.compliance_report_status_id | ||
JOIN compliance_period cp ON cr.compliance_period_id = cp.compliance_period_id | ||
JOIN TRANSACTION tr ON cr.transaction_id = tr.transaction_id | ||
AND cr.transaction_id IS NOT NULL; | ||
""" | ||
) | ||
|
||
# Create unique index on mv_transaction_aggregate | ||
op.execute( | ||
""" | ||
CREATE UNIQUE INDEX mv_transaction_aggregate_unique_idx ON mv_transaction_aggregate (transaction_id, description, transaction_type); | ||
""" | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.execute("DROP MATERIALIZED VIEW mv_transaction_aggregate;") | ||
op.execute( | ||
""" | ||
CREATE MATERIALIZED VIEW mv_transaction_aggregate AS | ||
SELECT | ||
t.transfer_id AS transaction_id, | ||
'Transfer' AS transaction_type, | ||
org_from.organization_id AS from_organization_id, | ||
org_from.name AS from_organization, | ||
org_to.organization_id AS to_organization_id, | ||
org_to.name AS to_organization, | ||
t.quantity, | ||
t.price_per_unit, | ||
ts.status::text AS status, | ||
NULL AS compliance_period, | ||
t.from_org_comment AS comment, | ||
tc.category, | ||
( | ||
SELECT th.create_date | ||
FROM transfer_history th | ||
WHERE th.transfer_id = t.transfer_id AND th.transfer_status_id = 6 | ||
) AS recorded_date, | ||
NULL AS approved_date, | ||
t.transaction_effective_date, | ||
t.update_date, | ||
t.create_date | ||
FROM transfer t | ||
JOIN organization org_from ON t.from_organization_id = org_from.organization_id | ||
JOIN organization org_to ON t.to_organization_id = org_to.organization_id | ||
JOIN transfer_status ts ON t.current_status_id = ts.transfer_status_id | ||
LEFT JOIN transfer_category tc ON t.transfer_category_id = tc.transfer_category_id | ||
UNION ALL | ||
SELECT | ||
ia.initiative_agreement_id AS transaction_id, | ||
'InitiativeAgreement' AS transaction_type, | ||
NULL AS from_organization_id, | ||
NULL AS from_organization, | ||
org.organization_id AS to_organization_id, | ||
org.name AS to_organization, | ||
ia.compliance_units AS quantity, | ||
NULL AS price_per_unit, | ||
ias.status::text AS status, | ||
NULL AS compliance_period, | ||
ia.gov_comment AS comment, | ||
NULL AS category, | ||
NULL AS recorded_date, | ||
( | ||
SELECT iah.create_date | ||
FROM initiative_agreement_history iah | ||
WHERE iah.initiative_agreement_id = ia.initiative_agreement_id AND iah.initiative_agreement_status_id = 3 | ||
) AS approved_date, | ||
ia.transaction_effective_date, | ||
ia.update_date, | ||
ia.create_date | ||
FROM initiative_agreement ia | ||
JOIN organization org ON ia.to_organization_id = org.organization_id | ||
JOIN initiative_agreement_status ias ON ia.current_status_id = ias.initiative_agreement_status_id | ||
UNION ALL | ||
SELECT | ||
aa.admin_adjustment_id AS transaction_id, | ||
'AdminAdjustment' AS transaction_type, | ||
NULL AS from_organization_id, | ||
NULL AS from_organization, | ||
org.organization_id AS to_organization_id, | ||
org.name AS to_organization, | ||
aa.compliance_units AS quantity, | ||
NULL AS price_per_unit, | ||
aas.status::text AS status, | ||
NULL AS compliance_period, | ||
aa.gov_comment AS comment, | ||
NULL AS category, | ||
NULL AS recorded_date, | ||
( | ||
SELECT aah.create_date | ||
FROM admin_adjustment_history aah | ||
WHERE aah.admin_adjustment_id = aa.admin_adjustment_id AND aah.admin_adjustment_status_id = 3 | ||
) AS approved_date, | ||
aa.transaction_effective_date, | ||
aa.update_date, | ||
aa.create_date | ||
FROM admin_adjustment aa | ||
JOIN organization org ON aa.to_organization_id = org.organization_id | ||
JOIN admin_adjustment_status aas ON aa.current_status_id = aas.admin_adjustment_status_id; | ||
""" | ||
) | ||
|
||
# Create unique index on mv_transaction_aggregate | ||
op.execute( | ||
""" | ||
CREATE UNIQUE INDEX mv_transaction_aggregate_unique_idx ON mv_transaction_aggregate (transaction_id, transaction_type); | ||
""" | ||
) |
70 changes: 70 additions & 0 deletions
70
backend/lcfs/db/migrations/versions/2025-01-14-14-03_5163af6ba4a4.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,70 @@ | ||
"""revert org display balance | ||
Revision ID: 5163af6ba4a4 | ||
Revises: f78e53370ed2 | ||
Create Date: 2025-01-14 14:03:50.975682 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "5163af6ba4a4" | ||
down_revision = "f78e53370ed2" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# Reapply the original logic for balances: | ||
op.execute( | ||
""" | ||
CREATE OR REPLACE FUNCTION update_organization_balance() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
new_total_balance BIGINT; | ||
new_reserved_balance BIGINT; | ||
org_id INT := COALESCE(NEW.organization_id, OLD.organization_id); | ||
BEGIN | ||
SELECT COALESCE(SUM(compliance_units), 0) | ||
INTO new_total_balance | ||
FROM "transaction" | ||
WHERE organization_id = org_id | ||
AND transaction_action = 'Adjustment'; | ||
SELECT COALESCE(SUM(compliance_units), 0) | ||
INTO new_reserved_balance | ||
FROM "transaction" | ||
WHERE organization_id = org_id | ||
AND transaction_action = 'Reserved' | ||
AND compliance_units < 0; | ||
UPDATE organization | ||
SET total_balance = new_total_balance, | ||
reserved_balance = new_reserved_balance | ||
WHERE organization_id = org_id; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" | ||
DROP TRIGGER IF EXISTS update_organization_balance_trigger ON "transaction"; | ||
""" | ||
) | ||
op.execute( | ||
""" | ||
CREATE TRIGGER update_organization_balance_trigger | ||
AFTER INSERT OR UPDATE OR DELETE ON "transaction" | ||
FOR EACH ROW EXECUTE FUNCTION update_organization_balance(); | ||
""" | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
pass |
Oops, something went wrong.