-
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 pull request #1240 from bcgov/feat/daniel-add-versioning-to-oth…
…er-uses-1191 feat: Add versioning to other uses
- Loading branch information
Showing
14 changed files
with
699 additions
and
354 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
100 changes: 100 additions & 0 deletions
100
backend/lcfs/db/migrations/versions/2024-11-13-00-39_654858661ae7.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,100 @@ | ||
"""Add Versioning to Other Uses | ||
Revision ID: 654858661ae7 | ||
Revises: b659816d0a86 | ||
Create Date: 2024-11-13 00:39:22.594912 | ||
""" | ||
|
||
import uuid | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "654858661ae7" | ||
down_revision = "b659816d0a86" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column( | ||
"other_uses", | ||
sa.Column( | ||
"group_uuid", | ||
sa.String(length=36), | ||
nullable=True, | ||
comment="UUID that groups all versions of a record series", | ||
), | ||
) | ||
op.add_column( | ||
"other_uses", | ||
sa.Column( | ||
"version", | ||
sa.Integer(), | ||
nullable=False, | ||
server_default="0", | ||
comment="Version number of the record", | ||
), | ||
) | ||
op.add_column( | ||
"other_uses", | ||
sa.Column( | ||
"user_type", | ||
postgresql.ENUM( | ||
"SUPPLIER", "GOVERNMENT", name="usertypeenum", create_type=False | ||
), | ||
nullable=False, | ||
server_default=sa.text("'SUPPLIER'"), | ||
comment="Indicates whether the record was created/modified by a supplier or government user", | ||
), | ||
) | ||
op.add_column( | ||
"other_uses", | ||
sa.Column( | ||
"action_type", | ||
postgresql.ENUM( | ||
"CREATE", "UPDATE", "DELETE", name="actiontypeenum", create_type=False | ||
), | ||
server_default=sa.text("'CREATE'"), | ||
nullable=True, | ||
comment="Action type for this record", | ||
), | ||
) | ||
|
||
#Update existing records with generated UUIDs | ||
connection = op.get_bind() | ||
|
||
# Update other_uses table | ||
fuel_exports = connection.execute( | ||
sa.text("SELECT other_uses_id FROM other_uses WHERE group_uuid IS NULL") | ||
).fetchall() | ||
for export in fuel_exports: | ||
export_id = export[0] | ||
connection.execute( | ||
sa.text( | ||
"UPDATE other_uses SET group_uuid = :uuid WHERE other_uses_id = :export_id" | ||
), | ||
{"uuid": str(uuid.uuid4()), "export_id": export_id}, | ||
) | ||
|
||
#Alter the column to be non-nullable | ||
op.alter_column( | ||
"other_uses", | ||
"group_uuid", | ||
existing_type=sa.String(length=36), | ||
nullable=False, | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column("other_uses", "action_type") | ||
op.drop_column("other_uses", "user_type") | ||
op.drop_column("other_uses", "version") | ||
op.drop_column("other_uses", "group_uuid") | ||
# ### end Alembic commands ### |
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
Empty file.
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,49 @@ | ||
from unittest.mock import MagicMock | ||
|
||
from lcfs.db.base import UserTypeEnum, ActionTypeEnum | ||
from lcfs.db.models import OtherUses | ||
from lcfs.web.api.other_uses.schema import OtherUsesCreateSchema | ||
|
||
|
||
def create_mock_entity(overrides: dict): | ||
mock_entity = MagicMock(spec=OtherUses) | ||
mock_entity.other_uses_id = 1 | ||
mock_entity.compliance_report_id = 1 | ||
mock_entity.fuel_type.fuel_type = "Gasoline" | ||
mock_entity.fuel_category.category = "Petroleum-based" | ||
mock_entity.expected_use.name = "Transportation" | ||
mock_entity.units = "L" | ||
mock_entity.rationale = "Test rationale" | ||
mock_entity.group_uuid = "test-group-uuid" | ||
mock_entity.version = 1 | ||
mock_entity.quantity_supplied = 1000 | ||
mock_entity.action_type = ActionTypeEnum.CREATE | ||
mock_entity.user_type = UserTypeEnum.SUPPLIER | ||
|
||
# Apply overrides | ||
if overrides: | ||
for key, value in overrides.items(): | ||
setattr(mock_entity, key, value) | ||
|
||
return mock_entity | ||
|
||
|
||
def create_mock_schema(overrides: dict): | ||
mock_schema = OtherUsesCreateSchema( | ||
compliance_report_id=1, | ||
quantity_supplied=1000, | ||
fuel_type="Gasoline", | ||
fuel_category="Petroleum-based", | ||
expected_use="Transportation", | ||
units="L", | ||
rationale="Test rationale", | ||
group_uuid="test-group-uuid", | ||
version=1, | ||
) | ||
|
||
# Apply overrides | ||
if overrides: | ||
for key, value in overrides.items(): | ||
setattr(mock_schema, key, value) | ||
|
||
return mock_schema |
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.