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() ` ) }