Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into update-node-version-0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexZorkin authored Jan 16, 2025
2 parents 2eab8de + 8aacc2f commit ad2b3fa
Show file tree
Hide file tree
Showing 93 changed files with 2,121 additions and 461 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cron-cleanup-workflow-runs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ jobs:
with:
token: ${{ github.token }}
repository: ${{ github.repository }}
retain_days: 15
retain_days: 5
keep_minimum_runs: 0
34 changes: 27 additions & 7 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,57 @@ jobs:
- 6379:6379
rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
environment:
env:
RABBITMQ_DEFAULT_USER: lcfs
RABBITMQ_DEFAULT_PASS: development_only
RABBITMQ_DEFAULT_VHOST: lcfs
ports:
- "15672:15672"
steps:
- uses: actions/checkout@v2

- name: Build and Run Backend Service
run: |
docker build -t backend-service ./backend/Dockerfile
docker run -d --name backend -e LCFS_DB_HOST=localhost -e LCFS_REDIS_HOST=localhost -p 8000:8000 backend-service
docker build -t backend-service -f ./backend/Dockerfile ./backend
docker run -d --name backend \
--network=host \
-e LCFS_DB_HOST=localhost \
-e LCFS_REDIS_HOST=localhost \
backend-service
- name: Wait for DB to be Ready
run: sleep 20

- name: Run Alembic Migrations
run: docker exec backend poetry run alembic upgrade head

- name: Data Seeding
run: docker exec backend poetry run python /app/lcfs/db/seeders/seed_database.py

- name: Build and Run Frontend Service
run: |
docker build -t frontend-service ./fontend/Dockerfile.dev
docker run -d --name frontend -p 3000:3000 frontend-service
docker build -t frontend-service -f ./frontend/Dockerfile.dev ./frontend
docker run -d --name frontend \
--network=host \
frontend-service
- name: Cypress run
uses: cypress-io/github-action@v2
with:
browser: chrome
wait-on: 'http://localhost:3000'
wait-on-timeout: 60
record: false
config-file: cypress.config.js
working-directory: frontend
env:
IDIR_TEST_USER: ${{ secrets.CYPRESS_IDIR_TEST_USER }}
IDIR_TEST_PASS: ${{ secrets.CYPRESS_IDIR_TEST_PASS }}
BCEID_TEST_USER: ${{ secrets.CYPRESS_BCEID_TEST_USER }}
BCEID_TEST_PASS: ${{ secrets.CYPRESS_BCEID_TEST_PASS }}

- name: Cleanup
if: always()
run: |
docker stop backend frontend
docker rm backend frontend
docker rm backend frontend
1 change: 1 addition & 0 deletions backend/lcfs/db/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"mv_org_compliance_report_count",
"transaction_status_view",
"mv_compliance_report_count",
"mv_fuel_code_count",
]


Expand Down
238 changes: 238 additions & 0 deletions backend/lcfs/db/migrations/versions/2025-01-13-22-13_f78e53370ed2.py
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);
"""
)
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
Loading

0 comments on commit ad2b3fa

Please sign in to comment.