From 24b3a798ef4ed781c18f8733ca26aa84e91a1f6a Mon Sep 17 00:00:00 2001 From: Jonathan Goulding <58443816+jonathangoulding@users.noreply.github.com> Date: Fri, 2 Aug 2024 09:02:28 +0100 Subject: [PATCH] Add insert on conflict to seeders (#1219) * Add insert on conflict to seeders As we move to using seeders for tests we can look to add functionality to allow us to seed the actually database. This change adds ON CONLICT resoultion based on unique constraints that are not the primary id's. This will allow the seeders to seed data and not affect existing data. This change is part of ongoing work to with the import service and test improvements to allow us to move to parallel test runs. --- ...07028_water-licence-version-purpose-condition-types.js | 2 ++ .../licence-version-purpose-condition-types.seeder.js | 8 +++++++- test/support/seeders/primary-purpose.seeder.js | 6 +++++- test/support/seeders/purposes.seeder.js | 8 +++++++- test/support/seeders/secondary-purpose.seeder.js | 6 +++++- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/db/migrations/legacy/20221108007028_water-licence-version-purpose-condition-types.js b/db/migrations/legacy/20221108007028_water-licence-version-purpose-condition-types.js index b7b5cd3f70..6297da9d4b 100644 --- a/db/migrations/legacy/20221108007028_water-licence-version-purpose-condition-types.js +++ b/db/migrations/legacy/20221108007028_water-licence-version-purpose-condition-types.js @@ -20,6 +20,8 @@ exports.up = function (knex) { // Legacy timestamps table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + + table.unique(['code', 'subcode'], 'uidx_code_subcode') }) } diff --git a/test/support/seeders/licence-version-purpose-condition-types.seeder.js b/test/support/seeders/licence-version-purpose-condition-types.seeder.js index 7244f71f87..eaee052341 100644 --- a/test/support/seeders/licence-version-purpose-condition-types.seeder.js +++ b/test/support/seeders/licence-version-purpose-condition-types.seeder.js @@ -17,7 +17,13 @@ const keys = ['id', 'code', 'subcode', 'description', 'subcodeDescription', 'dis async function seed () { await db.raw(` INSERT INTO public.licence_version_purpose_condition_types (id, code, subcode, description, subcode_description, display_title, created_at, updated_at) - VALUES ${buildSeedValueString(keys, data)}; + VALUES ${buildSeedValueString(keys, data)} + ON CONFLICT (code, subcode) + DO UPDATE SET + description = excluded.description, + subcode_description = excluded.subcode_description, + display_title = excluded.display_title, + updated_at = now() ` ) } diff --git a/test/support/seeders/primary-purpose.seeder.js b/test/support/seeders/primary-purpose.seeder.js index bb0456b23b..24dfd0ffce 100644 --- a/test/support/seeders/primary-purpose.seeder.js +++ b/test/support/seeders/primary-purpose.seeder.js @@ -17,7 +17,11 @@ const keys = ['id', 'legacyId', 'description', 'createdAt', 'updatedAt'] async function seed () { await db.raw(` INSERT INTO public.primary_purposes (id, legacy_id, description, created_at, updated_at) - VALUES ${buildSeedValueString(keys, data)}; + VALUES ${buildSeedValueString(keys, data)} + ON CONFLICT (legacy_id) + DO UPDATE SET + description = excluded.description, + updated_at = now() ` ) } diff --git a/test/support/seeders/purposes.seeder.js b/test/support/seeders/purposes.seeder.js index 50eb3e006b..756d1b874a 100644 --- a/test/support/seeders/purposes.seeder.js +++ b/test/support/seeders/purposes.seeder.js @@ -17,7 +17,13 @@ const keys = ['id', 'legacyId', 'description', 'lossFactor', 'twoPartTariff', 'c async function seed () { await db.raw(` INSERT INTO public.purposes (id, legacy_id, description, loss_factor, two_part_tariff, created_at, updated_at) - VALUES ${buildSeedValueString(keys, data)}; + VALUES ${buildSeedValueString(keys, data)} + ON CONFLICT (legacy_id) + DO UPDATE SET + description = excluded.description, + loss_factor = excluded.loss_factor, + two_part_tariff = excluded.two_part_tariff, + updated_at = now() ` ) } diff --git a/test/support/seeders/secondary-purpose.seeder.js b/test/support/seeders/secondary-purpose.seeder.js index e271cc1342..c96a959823 100644 --- a/test/support/seeders/secondary-purpose.seeder.js +++ b/test/support/seeders/secondary-purpose.seeder.js @@ -17,7 +17,11 @@ const keys = ['id', 'legacyId', 'description', 'createdAt', 'updatedAt'] async function seed () { await db.raw(` INSERT INTO public.secondary_purposes (id, legacy_id, description, created_at, updated_at) - VALUES ${buildSeedValueString(keys, data)}; + VALUES ${buildSeedValueString(keys, data)} + ON CONFLICT (legacy_id) + DO UPDATE SET + description = excluded.description, + updated_at = now() ` ) }