Skip to content

Commit

Permalink
[MDS-6120] Add permit conditions version history (#3228)
Browse files Browse the repository at this point in the history
* Add permit conditions version history migration

Add mounts for migrations to docker-compose files, create history and backfill SQL migration scripts for permit conditions, update version table generation logic, and fix documentation typo.

* updated permit condition put test to also test versioning
  • Loading branch information
matbusby-fw authored Aug 27, 2024
1 parent be1d7c0 commit 8d333de
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions docker-compose.M1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ services:
ports:
- 5000:5000
volumes:
- ./migrations:/migrations
- ./services/core-api:/app
- core_api_logs:/var/log/core-api
depends_on:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ services:
ports:
- 5000:5000
volumes:
- ./migrations:/migrations
- ./services/core-api:/app
- core_api_logs:/var/log/core-api
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion docs/audit_log.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ It's currently integrated into the MDS system for tracking changes for Tailings
2. Create a version table for the model by running the following command:

```bash
make generate_version_table_migration TABLE=mine
make generate_history_table_migration TABLE=mine
```

This will generate a flyway migration file that creates a version table for the specified model and a migration, and
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- This file was generated by the generate_history_table_ddl command
-- The file contains the corresponding history table definition for the {table} table
CREATE TABLE permit_conditions_version (
create_user VARCHAR(60),
create_timestamp TIMESTAMP WITHOUT TIME ZONE,
update_user VARCHAR(60),
update_timestamp TIMESTAMP WITHOUT TIME ZONE,
deleted_ind BOOLEAN,
permit_condition_id INTEGER NOT NULL,
permit_amendment_id INTEGER,
permit_condition_guid UUID,
condition VARCHAR,
condition_category_code VARCHAR,
condition_type_code VARCHAR,
parent_permit_condition_id INTEGER,
display_order INTEGER,
transaction_id BIGINT NOT NULL,
end_transaction_id BIGINT,
operation_type SMALLINT NOT NULL,
PRIMARY KEY (permit_condition_id, transaction_id)
);
CREATE INDEX ix_permit_conditions_version_end_transaction_id ON permit_conditions_version (end_transaction_id);
CREATE INDEX ix_permit_conditions_version_operation_type ON permit_conditions_version (operation_type);
CREATE INDEX ix_permit_conditions_version_transaction_id ON permit_conditions_version (transaction_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was generated by the generate_history_table_ddl command
-- The file contains the data migration to backfill history records for the {table} table
with transaction AS (insert into transaction(id) values(DEFAULT) RETURNING id)
insert into permit_conditions_version (transaction_id, operation_type, end_transaction_id, "create_user", "create_timestamp", "update_user", "update_timestamp", "deleted_ind", "permit_condition_id", "permit_amendment_id", "permit_condition_guid", "condition", "condition_category_code", "condition_type_code", "parent_permit_condition_id", "display_order")
select t.id, '0', null, "create_user", "create_timestamp", "update_user", "update_timestamp", "deleted_ind", "permit_condition_id", "permit_amendment_id", "permit_condition_guid", "condition", "condition_category_code", "condition_type_code", "parent_permit_condition_id", "display_order"
from permit_conditions,transaction t;
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class _ModelSchema(Base._ModelSchema):
db.ForeignKey('permit_conditions.permit_condition_id'))
display_order = db.Column(db.Integer, nullable=False)

__versioned__ = {}

all_sub_conditions = db.relationship(
'PermitConditions',
lazy='joined',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def generate_history_table_migration(table):

def generate_backfill_history_migration(table, version_table_name, table_class):
data_migdt = (datetime.datetime.today() + datetime.timedelta(minutes=1)).strftime('%Y.%m.%d.%H.%M')
migration_name = f'/migrations/V{data_migdt}__add_{version_table_name}_history_table_backfill.sql'
migration_name = f'/migrations/sql/V{data_migdt}__add_{version_table_name}_history_table_backfill.sql'

with open(migration_name, 'w') as f:
table_columns = []
Expand All @@ -69,7 +69,7 @@ def generate_backfill_history_migration(table, version_table_name, table_class):
def generate_version_table_migration(version_table_name, version_table_definition):
dt = datetime.datetime.today().strftime('%Y.%m.%d.%H.%M')

migration_name = f'/migrations/V{dt}__add_{version_table_name}_history_table.sql'
migration_name = f'/migrations/sql/V{dt}__add_{version_table_name}_history_table.sql'

version_table_create_statement = str(CreateTable(version_table_definition).compile(dialect=postgresql.dialect())).strip()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime, timedelta
from dateutil import parser
from app.api.mines.permits.permit_conditions.models.permit_conditions import PermitConditions

from app.api.mines.response_models import PermitCondition

from tests.factories import create_mine_and_permit

Expand Down Expand Up @@ -45,7 +45,6 @@ def test_delete_permit_condition(test_client, db_session, auth_headers):
# deleted items should be filtered out
assert permit_amendment.conditions[0].deleted_ind != True


# PUT
def test_put_permit_condition(test_client, db_session, auth_headers):
mine, permit = create_mine_and_permit()
Expand All @@ -68,3 +67,50 @@ def test_put_permit_condition(test_client, db_session, auth_headers):

# the API returned success
assert put_resp.status_code == 200

response_json = put_resp.json
assert "permit_condition_guid" in response_json
permit_condition_guid = response_json["permit_condition_guid"]

# Fetch the updated condition using the class method
updated_condition = PermitConditions.find_by_permit_condition_guid(permit_condition_guid)

# Access the versioning table
version_records = list(updated_condition.versions)

# Ensure there are version records
assert len(version_records) == 1

# Get the latest version record
latest_version = version_records[len(version_records) - 1]

# Assert the latest version has the updated values
assert latest_version.condition == data['condition']

assert latest_version.permit_amendment_id == data['permit_amendment_id']
assert latest_version.condition_category_code == data['condition_category_code']
assert latest_version.condition_type_code == data['condition_type_code']

data_b = {
"permit_condition_guid": condition.permit_condition_guid,
"permit_amendment_id": condition.permit_amendment_id,
"condition_category_code": condition.condition_category_code,
"condition_type_code": condition.condition_type_code,
"condition": "version 2",
"display_order": "3",
}

put_resp_b = test_client.put(
f'/mines/{permit_amendment.mine_guid}/permits/{permit_amendment.permit_guid}/amendments/{permit_amendment.permit_amendment_guid}/conditions/{condition.permit_condition_guid}',
headers=auth_headers['full_auth_header'],
json=data_b)

assert put_resp_b.status_code == 200

version_records = list(updated_condition.versions)

# Ensure there are now 2 version records
assert len(version_records) == 2

latest_version = version_records[len(version_records) - 1]
assert latest_version.condition == data_b['condition']

0 comments on commit 8d333de

Please sign in to comment.