From 6027ebc4e5ba16d67288453913eeaba6c01e995a Mon Sep 17 00:00:00 2001 From: maillard Date: Tue, 10 Oct 2023 17:52:28 -0700 Subject: [PATCH] Trigger function in scheduling_specification correctly updates spec revision --- .../database/SchedulerDatabaseTests.java | 21 +++++++++++++++++++ .../11_fix_scheduling_spec_trigger/down.sql | 14 +++++++++++++ .../11_fix_scheduling_spec_trigger/up.sql | 14 +++++++++++++ .../sql/scheduler/applied_migrations.sql | 1 + .../tables/scheduling_specification.sql | 4 ++-- 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 deployment/hasura/migrations/AerieScheduler/11_fix_scheduling_spec_trigger/down.sql create mode 100644 deployment/hasura/migrations/AerieScheduler/11_fix_scheduling_spec_trigger/up.sql diff --git a/db-tests/src/test/java/gov/nasa/jpl/aerie/database/SchedulerDatabaseTests.java b/db-tests/src/test/java/gov/nasa/jpl/aerie/database/SchedulerDatabaseTests.java index ac8b399240..b2a3a04256 100644 --- a/db-tests/src/test/java/gov/nasa/jpl/aerie/database/SchedulerDatabaseTests.java +++ b/db-tests/src/test/java/gov/nasa/jpl/aerie/database/SchedulerDatabaseTests.java @@ -133,6 +133,27 @@ void shouldErrorWhenInsertingNonConsecutivePriority() throws SQLException { )); } + private int getSpecificationRevision(int specificationId) throws SQLException { + final var res = connection.createStatement().executeQuery(""" + select revision from scheduling_specification + where id = %d; + """.formatted(specificationId)); + res.next(); + return res.getInt("revision"); + } + + @Test + void shouldIncrementSpecRevisionAfterModifyingGoal() throws SQLException { + insertGoalPriorities(0, new int[] {0, 1, 2, 3, 4}, new int[]{0, 1, 2, 3, 4}); + final var revisionBefore = getSpecificationRevision(specificationIds[0]); + connection.createStatement().executeUpdate(""" + update scheduling_goal + set name = 'other name' where id = %d; + """.formatted(goalIds[3])); + final var revisionAfter = getSpecificationRevision(specificationIds[0]); + assertEquals(revisionBefore + 1, revisionAfter); + } + @Test void shouldReorderPrioritiesOnUpdate() throws SQLException { insertGoalPriorities(0, new int[] {0, 1, 2}, new int[]{0, 1, 2}); diff --git a/deployment/hasura/migrations/AerieScheduler/11_fix_scheduling_spec_trigger/down.sql b/deployment/hasura/migrations/AerieScheduler/11_fix_scheduling_spec_trigger/down.sql new file mode 100644 index 0000000000..8cd65ee1b2 --- /dev/null +++ b/deployment/hasura/migrations/AerieScheduler/11_fix_scheduling_spec_trigger/down.sql @@ -0,0 +1,14 @@ +create or replace function increment_revision_on_goal_update() + returns trigger + security definer + language plpgsql as $$begin + with goals as ( + select g.specification_id from scheduling_specification_goals as g + where g.specification_id = new.id + ) + update scheduling_specification set revision = revision + 1 + where exists(select 1 from goals where specification_id = id); +return new; +end$$; + +call migrations.mark_migration_rolled_back('11'); diff --git a/deployment/hasura/migrations/AerieScheduler/11_fix_scheduling_spec_trigger/up.sql b/deployment/hasura/migrations/AerieScheduler/11_fix_scheduling_spec_trigger/up.sql new file mode 100644 index 0000000000..ed9a959972 --- /dev/null +++ b/deployment/hasura/migrations/AerieScheduler/11_fix_scheduling_spec_trigger/up.sql @@ -0,0 +1,14 @@ +create or replace function increment_revision_on_goal_update() + returns trigger + security definer + language plpgsql as $$begin + with goals as ( + select g.specification_id from scheduling_specification_goals as g + where g.goal_id = new.id + ) + update scheduling_specification set revision = revision + 1 + where exists(select 1 from goals where specification_id = id); + return new; +end$$; + +call migrations.mark_migration_applied('11'); diff --git a/scheduler-server/sql/scheduler/applied_migrations.sql b/scheduler-server/sql/scheduler/applied_migrations.sql index 245dc51deb..9f1d007a71 100644 --- a/scheduler-server/sql/scheduler/applied_migrations.sql +++ b/scheduler-server/sql/scheduler/applied_migrations.sql @@ -13,3 +13,4 @@ call migrations.mark_migration_applied('7'); call migrations.mark_migration_applied('8'); call migrations.mark_migration_applied('9'); call migrations.mark_migration_applied('10'); +call migrations.mark_migration_applied('11'); diff --git a/scheduler-server/sql/scheduler/tables/scheduling_specification.sql b/scheduler-server/sql/scheduler/tables/scheduling_specification.sql index 9590b7cb25..e26c133da9 100644 --- a/scheduler-server/sql/scheduler/tables/scheduling_specification.sql +++ b/scheduler-server/sql/scheduler/tables/scheduling_specification.sql @@ -45,11 +45,11 @@ create function increment_revision_on_goal_update() language plpgsql as $$begin with goals as ( select g.specification_id from scheduling_specification_goals as g - where g.specification_id = new.id + where g.goal_id = new.id ) update scheduling_specification set revision = revision + 1 where exists(select 1 from goals where specification_id = id); -return new; + return new; end$$; create trigger increment_revision_on_update_trigger