Skip to content

Commit

Permalink
Trigger function in scheduling_specification correctly updates spec r…
Browse files Browse the repository at this point in the history
…evision
  • Loading branch information
adrienmaillard committed Oct 12, 2023
1 parent afca30f commit 6027ebc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -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');
1 change: 1 addition & 0 deletions scheduler-server/sql/scheduler/applied_migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6027ebc

Please sign in to comment.