Skip to content

Commit

Permalink
Add insert on conflict to seeders (#1219)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jonathangoulding authored Aug 2, 2024
1 parent 3bf5ef7 commit 24b3a79
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
`
)
}
Expand Down
6 changes: 5 additions & 1 deletion test/support/seeders/primary-purpose.seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
`
)
}
Expand Down
8 changes: 7 additions & 1 deletion test/support/seeders/purposes.seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
`
)
}
Expand Down
6 changes: 5 additions & 1 deletion test/support/seeders/secondary-purpose.seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
`
)
}
Expand Down

0 comments on commit 24b3a79

Please sign in to comment.