-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2959 from bcgov/batch_row_and_id_trigger
Batch id + row number trigger
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
`); | ||
} |