From e4907b273d7f17f51eb7c5bee721640a6e70e8ae Mon Sep 17 00:00:00 2001 From: brennanwebster Date: Tue, 7 Nov 2023 11:16:02 -0800 Subject: [PATCH] add agencies, employers, and herbicides to code table --- .../0076_add_agencies_employers_herbicides.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 database/src/migrations/0076_add_agencies_employers_herbicides.ts diff --git a/database/src/migrations/0076_add_agencies_employers_herbicides.ts b/database/src/migrations/0076_add_agencies_employers_herbicides.ts new file mode 100644 index 000000000..374483d85 --- /dev/null +++ b/database/src/migrations/0076_add_agencies_employers_herbicides.ts @@ -0,0 +1,53 @@ +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + await knex.raw( + ` + set search_path=invasivesbc,public; + + INSERT INTO code + (code_header_id, code_name, code_description, code_sort_order, valid_from, valid_to, created_at, updated_at, created_by_user_id, updated_by_user_id) + VALUES(44, 'RMT', 'Rocky Mountain Trench Natural Resources Society', 1, now(), null, now(), now(), 1, 1), + (44, 'GREP', 'Grassland and Rangeland Enhancement Program', 1, now(), null, now(), now(), 1, 1), + (44, 'CIRNAC', 'Crown-Indigenous Relations and Northern Affairs Canada', 1, now(), null, now(), now(), 1, 1), + (44, 'CWS', 'Canadian Wildlife Service', 1, now(), null, now(), now(), 1, 1), + (44, 'WSS', 'Wild Sheep Society of BC', 1, now(), null, now(), now(), 1, 1), + (79, 'CCE', 'Chu Cho Environmental', 1, now(), null, now(), now(), 1, 1), + (79, 'SFC', 'Strathinnes Forestry Consultants', 1, now(), null, now(), now(), 1, 1), + (48, '30409', 'Sightline A [aminopyralid/metsulfuron-methyl] 30409', 1, now(), null, now(), now(), 1, 1), + (47, '30795', 'Sightline B [fluroxypyr] 30795', 1, now(), null, now(), now(), 1, 1); + + UPDATE code AS c + SET code_sort_order = subquery.row_number + FROM ( + SELECT code_id, ROW_NUMBER() OVER (PARTITION BY code_header_id ORDER BY code_header_id, code_description) AS row_number + FROM code + ) AS subquery + WHERE c.code_id = subquery.code_id; + +` + ); +} + +export async function down(knex: Knex): Promise { + await knex.raw(` + + set search_path=invasivesbc,public; + + DELETE FROM code + WHERE (code_header_id = 44 + AND code_name IN ('RMT', 'GREP', 'CIRNAC' ,'CWS' ,'WSS')) + OR (code_header_id = 79 + AND code_name IN ('CCE', 'SFC')) + OR (code_header_id = 48 and code_name in ('30409')) + OR (code_header_id = 47 and code_name in ('30795')); + + UPDATE code AS c + SET code_sort_order = subquery.row_number + FROM ( + SELECT code_id, ROW_NUMBER() OVER (PARTITION BY code_header_id ORDER BY code_header_id, code_description) AS row_number + FROM code + ) AS subquery + WHERE c.code_id = subquery.code_id; + `); +}