From 718823c013ea05f378fe234aafef700c6e567028 Mon Sep 17 00:00:00 2001 From: Caleb Sharp Date: Wed, 4 Mar 2020 09:51:44 -0800 Subject: [PATCH 1/2] Create migration to remove duplicate plans --- .../20200303122440_remove-duplicates.js | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/libs/db2/migrations/20200303122440_remove-duplicates.js diff --git a/src/libs/db2/migrations/20200303122440_remove-duplicates.js b/src/libs/db2/migrations/20200303122440_remove-duplicates.js new file mode 100644 index 00000000..1540be85 --- /dev/null +++ b/src/libs/db2/migrations/20200303122440_remove-duplicates.js @@ -0,0 +1,112 @@ +const Plan = require('../../../../build/src/libs/db2/model/plan').default; +const PlanSnapshot = require('../../../../build/src/libs/db2/model/plansnapshot').default; +const Agreement = require('../../../../build/src/libs/db2/model/agreement').default; + +exports.up = async (knex) => { + const { rows } = await knex.raw(` + SELECT + plan_version.canonical_id as canonical_id, + ARRAY_AGG(plan_version.plan_id) FILTER (WHERE plan_version.version != - 1) AS past_plan_ids, + ( + SELECT + id + FROM + plan + JOIN plan_version pv ON id = pv.plan_id + AND pv.canonical_id = plan_version.canonical_id + WHERE + pv.version = - 1 + LIMIT 1) AS current_plan_id + FROM + plan_version + GROUP BY + plan_version.canonical_id; + `); + + const promises = await rows + .filter(row => row.past_plan_ids !== null) + .map(async ({ + past_plan_ids: pastPlanIds = [], + }) => { + await Promise.all(pastPlanIds.map(async (pastPlanId) => { + console.log(`Deleting plan ${pastPlanId}`); + + await knex.raw('DELETE FROM plan_version WHERE plan_id=?', [pastPlanId]); + await knex.raw('DELETE FROM plan WHERE id=?', [pastPlanId]); + })); + }); + + await Promise.all(promises); + + const { rows: nonVersionedPlanIds } = await knex.raw(` + SELECT id, agreement_id FROM plan where plan.id not in (select plan_id from plan_snapshot); + `); + + + await Promise.all( + nonVersionedPlanIds.map(async ({ id, agreement_id: agreementId }, i) => { + const { rows: [currentPlan] } = await knex.raw(` + SELECT * FROM plan_snapshot WHERE plan_id IN (SELECT id FROM plan WHERE agreement_id=?) LIMIT 1; + `, [agreementId]); + + if (currentPlan) { + console.log(`Creating snapshot from ${id} on plan ${currentPlan.plan_id}`); + + const [plan] = await Plan.findWithStatusExtension(knex, { + 'plan.id': id }, + ['id', 'desc']); + + if (!plan) { + console.log(`Could not find plan ${id}`); + } + + const [agreement] = await Agreement.findWithTypeZoneDistrictExemption( + knex, { forest_file_id: agreementId }, + ); + await agreement.eagerloadAllOneToManyExceptPlan(); + agreement.transformToV1(); + + await plan.eagerloadAllOneToMany(); + + const { rows: [{ version: lastVersion }] } = await knex.raw( + 'SELECT version FROM plan_snapshot WHERE plan_id=? ORDER BY version DESC LIMIT 1;', + [currentPlan.plan_id], + ); + + const snapshot = await PlanSnapshot.create(knex, { + snapshot: JSON.stringify(plan), + created_at: plan.created_at, + version: lastVersion + (1 * i) + 1, + plan_id: currentPlan.plan_id, + status_id: plan.statusId, + }); + + console.log(`Deleting plan ${id}`); + + await knex.raw('DELETE FROM plan_version WHERE plan_id=?', [id]); + await knex.raw('DELETE FROM plan WHERE id=?', [id]); + + return snapshot; + } + }), + ); + + // Reset version increments + await knex.raw('UPDATE plan_snapshot SET version = version + 10000'); + + const { rows: snapshotSets } = await knex.raw('SELECT plan_id, ARRAY_AGG(id ORDER BY created_at ASC) AS ids FROM plan_snapshot GROUP BY plan_id'); + + await Promise.all( + snapshotSets.map(async ({ ids }) => { + await Promise.all( + ids.map(async (id, i) => { + await knex.raw('UPDATE plan_snapshot SET version=? WHERE id=?', [i + 1, id]); + }), + ); + }), + ); +}; + +exports.down = async (knex) => { + +}; From da481536de24a6075f5b963e35c34bb38905c1cd Mon Sep 17 00:00:00 2001 From: Caleb Sharp Date: Wed, 4 Mar 2020 11:59:19 -0800 Subject: [PATCH 2/2] Skip nonuploaded plans --- .../20200303122440_remove-duplicates.js | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/libs/db2/migrations/20200303122440_remove-duplicates.js b/src/libs/db2/migrations/20200303122440_remove-duplicates.js index 1540be85..af2ce538 100644 --- a/src/libs/db2/migrations/20200303122440_remove-duplicates.js +++ b/src/libs/db2/migrations/20200303122440_remove-duplicates.js @@ -57,36 +57,36 @@ exports.up = async (knex) => { ['id', 'desc']); if (!plan) { - console.log(`Could not find plan ${id}`); + console.log(`Could not find plan ${id}. 'uploaded' is probably false`); + } else { + const [agreement] = await Agreement.findWithTypeZoneDistrictExemption( + knex, { forest_file_id: agreementId }, + ); + await agreement.eagerloadAllOneToManyExceptPlan(); + agreement.transformToV1(); + + await plan.eagerloadAllOneToMany(); + + const { rows: [{ version: lastVersion }] } = await knex.raw( + 'SELECT version FROM plan_snapshot WHERE plan_id=? ORDER BY version DESC LIMIT 1;', + [currentPlan.plan_id], + ); + + const snapshot = await PlanSnapshot.create(knex, { + snapshot: JSON.stringify(plan), + created_at: plan.created_at, + version: lastVersion + (1 * i) + 1, + plan_id: currentPlan.plan_id, + status_id: plan.statusId, + }); + + console.log(`Deleting plan ${id}`); + + await knex.raw('DELETE FROM plan_version WHERE plan_id=?', [id]); + await knex.raw('DELETE FROM plan WHERE id=?', [id]); + + return snapshot; } - - const [agreement] = await Agreement.findWithTypeZoneDistrictExemption( - knex, { forest_file_id: agreementId }, - ); - await agreement.eagerloadAllOneToManyExceptPlan(); - agreement.transformToV1(); - - await plan.eagerloadAllOneToMany(); - - const { rows: [{ version: lastVersion }] } = await knex.raw( - 'SELECT version FROM plan_snapshot WHERE plan_id=? ORDER BY version DESC LIMIT 1;', - [currentPlan.plan_id], - ); - - const snapshot = await PlanSnapshot.create(knex, { - snapshot: JSON.stringify(plan), - created_at: plan.created_at, - version: lastVersion + (1 * i) + 1, - plan_id: currentPlan.plan_id, - status_id: plan.statusId, - }); - - console.log(`Deleting plan ${id}`); - - await knex.raw('DELETE FROM plan_version WHERE plan_id=?', [id]); - await knex.raw('DELETE FROM plan WHERE id=?', [id]); - - return snapshot; } }), );