From 6e427bf21b2d84ed77e19e389a0ad2c51169927e Mon Sep 17 00:00:00 2001 From: andrea rota Date: Fri, 6 Oct 2023 18:06:05 +0100 Subject: [PATCH] use deterministic ids to help make the process idempotent --- .../cost-surface/cost-surface-data-migration.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/data/scripts/cost-surface/cost-surface-data-migration.py b/data/scripts/cost-surface/cost-surface-data-migration.py index 0a049b5b15..99e276138c 100644 --- a/data/scripts/cost-surface/cost-surface-data-migration.py +++ b/data/scripts/cost-surface/cost-surface-data-migration.py @@ -40,13 +40,17 @@ scenario_id, scenario_name, project_id = scenario # Insert new row in cost_surfaces + # + # We use scenario_id as the id for the cost_surface so that we can make + # the process idempotent, at least in terms of creating cost surfaces + # for existing scenarios. insert_cost_surface_sql = ''' INSERT INTO cost_surfaces (id, name, min, max, is_default, project_id, is_migrated) - VALUES (gen_random_uuid(), %s, 0, 0, false, %s, true) + VALUES (%s, %s, 0, 0, false, %s, true) RETURNING id; ''' - cur_api_db.execute(insert_cost_surface_sql, (scenario_name, project_id)) + cur_api_db.execute(insert_cost_surface_sql, (scenario_id, scenario_name, project_id)) cost_surface_id = cur_api_db.fetchone()[0] # Retrieve the unique id for this new cost_surface # Update scenarios.cost_surface_id for this specific scenario @@ -66,12 +70,16 @@ project_id = project[0] # Insert new row in cost_surfaces with project-specific information + # + # We use project_id as the id for the cost_surface so that we can make + # the process idempotent, at least in terms of creating default cost + # surfaces for existing projects. insert_cost_surface_for_project_sql = ''' INSERT INTO cost_surfaces (id, project_id, min, max, is_default, name, is_migrated) - VALUES (gen_random_uuid(), %s, 1, 1, true, 'default', true); + VALUES (%s, %s, 1, 1, true, 'default', true); ''' - cur_api_db.execute(insert_cost_surface_for_project_sql, (project_id,)) + cur_api_db.execute(insert_cost_surface_for_project_sql, (project_id, project_id,)) print("Successfully migrated API model data from marxan-api for:") print(len(all_scenarios), "scenarios")