From f444dc12f3a493458bebc18d235dd1937925b48d Mon Sep 17 00:00:00 2001 From: Theresa Kamerman Date: Wed, 13 Sep 2023 17:40:59 -0700 Subject: [PATCH] Add cancel trigger to scheduling request --- .../10_cancel_scheduling_trigger/down.sql | 4 ++++ .../10_cancel_scheduling_trigger/up.sql | 19 ++++++++++++++++ .../sql/scheduler/applied_migrations.sql | 1 + .../scheduler/tables/scheduling_request.sql | 22 ++++++++++++++++--- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 deployment/hasura/migrations/AerieScheduler/10_cancel_scheduling_trigger/down.sql create mode 100644 deployment/hasura/migrations/AerieScheduler/10_cancel_scheduling_trigger/up.sql diff --git a/deployment/hasura/migrations/AerieScheduler/10_cancel_scheduling_trigger/down.sql b/deployment/hasura/migrations/AerieScheduler/10_cancel_scheduling_trigger/down.sql new file mode 100644 index 0000000000..e3153fba34 --- /dev/null +++ b/deployment/hasura/migrations/AerieScheduler/10_cancel_scheduling_trigger/down.sql @@ -0,0 +1,4 @@ +drop trigger cancel_pending_scheduling_rqs on scheduling_request; +drop function cancel_pending_scheduling_rqs(); + +call migrations.mark_migration_rolled_back('10'); diff --git a/deployment/hasura/migrations/AerieScheduler/10_cancel_scheduling_trigger/up.sql b/deployment/hasura/migrations/AerieScheduler/10_cancel_scheduling_trigger/up.sql new file mode 100644 index 0000000000..509364022d --- /dev/null +++ b/deployment/hasura/migrations/AerieScheduler/10_cancel_scheduling_trigger/up.sql @@ -0,0 +1,19 @@ +create function cancel_pending_scheduling_rqs() +returns trigger +security definer +language plpgsql as $$ +begin + update scheduling_request + set canceled = true + where status = 'pending' + and specification_id = new.specification_id; + return new; +end +$$; + +create trigger cancel_pending_scheduling_rqs + before insert on scheduling_request + for each row + execute function cancel_pending_scheduling_rqs(); + +call migrations.mark_migration_applied('10'); diff --git a/scheduler-server/sql/scheduler/applied_migrations.sql b/scheduler-server/sql/scheduler/applied_migrations.sql index 4f0f55d200..245dc51deb 100644 --- a/scheduler-server/sql/scheduler/applied_migrations.sql +++ b/scheduler-server/sql/scheduler/applied_migrations.sql @@ -12,3 +12,4 @@ call migrations.mark_migration_applied('6'); call migrations.mark_migration_applied('7'); call migrations.mark_migration_applied('8'); call migrations.mark_migration_applied('9'); +call migrations.mark_migration_applied('10'); diff --git a/scheduler-server/sql/scheduler/tables/scheduling_request.sql b/scheduler-server/sql/scheduler/tables/scheduling_request.sql index 9f434c8791..8577101231 100644 --- a/scheduler-server/sql/scheduler/tables/scheduling_request.sql +++ b/scheduler-server/sql/scheduler/tables/scheduling_request.sql @@ -44,7 +44,7 @@ comment on column scheduling_request.requested_at is e'' -- Scheduling request NOTIFY triggers -- These triggers NOTIFY LISTEN(ing) scheduler worker clients of pending scheduling requests -create or replace function notify_scheduler_workers () +create function notify_scheduler_workers () returns trigger security definer language plpgsql as $$ @@ -64,9 +64,25 @@ begin return null; end$$; -do $$ begin create trigger notify_scheduler_workers after insert on scheduling_request for each row execute function notify_scheduler_workers(); -end $$; + +create function cancel_pending_scheduling_rqs() +returns trigger +security definer +language plpgsql as $$ +begin + update scheduling_request + set canceled = true + where status = 'pending' + and specification_id = new.specification_id; + return new; +end +$$; + +create trigger cancel_pending_scheduling_rqs + before insert on scheduling_request + for each row + execute function cancel_pending_scheduling_rqs();