Skip to content

Commit

Permalink
Merge pull request #2959 from bcgov/batch_row_and_id_trigger
Browse files Browse the repository at this point in the history
Batch id + row number trigger
  • Loading branch information
micheal-w-wells authored Nov 2, 2023
2 parents 6d11435 + 3975fd0 commit cb82969
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions database/src/migrations/0075_batch_id_trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.raw(
`
set
search_path = invasivesbc,
public;
CREATE
OR REPLACE FUNCTION invasivesbc.batch_and_row_id_autofill() RETURNS trigger LANGUAGE plpgsql AS $function$
begin if(new.batch_id is null) then
new.batch_id := (
select
batch_id
from
invasivesbc.activity_incoming_data
where
batch_id is not null
and activity_id = new.activity_id
limit
1
) ;end if;
if(new.row_number is null) then new.row_number := (
select
row_number
from
invasivesbc.activity_incoming_data
where
row_number is not null
and activity_id = new.activity_id
limit
1
) ;end if;
return new;
END;
$function$ ;
CREATE TRIGGER maintain_batch_and_row_id BEFORE INSERT
OR
UPDATE
ON invasivesbc.activity_incoming_data
FOR EACH ROW EXECUTE procedure batch_and_row_id_autofill();
`
);
}

export async function down(knex: Knex): Promise<void> {
await knex.raw(`
set search_path='invasivesbc';
`);
}

0 comments on commit cb82969

Please sign in to comment.