Skip to content

Commit

Permalink
Skip nonuploaded plans
Browse files Browse the repository at this point in the history
  • Loading branch information
calebissharp committed Mar 4, 2020
1 parent 718823c commit da48153
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/libs/db2/migrations/20200303122440_remove-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}),
);
Expand Down

0 comments on commit da48153

Please sign in to comment.