From 7aeaf0ee375b0a8df08c782aae59a5175ab87830 Mon Sep 17 00:00:00 2001 From: Steven Weathers Date: Thu, 22 Feb 2024 08:46:26 -0500 Subject: [PATCH] Create 20240222084232_update_delete_pokerstory_proc.sql This resolves #492 --- ...22084232_update_delete_pokerstory_proc.sql | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 internal/db/migrations/20240222084232_update_delete_pokerstory_proc.sql diff --git a/internal/db/migrations/20240222084232_update_delete_pokerstory_proc.sql b/internal/db/migrations/20240222084232_update_delete_pokerstory_proc.sql new file mode 100644 index 00000000..b735a6aa --- /dev/null +++ b/internal/db/migrations/20240222084232_update_delete_pokerstory_proc.sql @@ -0,0 +1,59 @@ +-- +goose Up +-- +goose StatementBegin +CREATE OR REPLACE PROCEDURE thunderdome.poker_story_delete(IN pokerid uuid, IN storyid uuid) + LANGUAGE plpgsql + AS $$ +DECLARE + active_storyid UUID; + story RECORD; + pos DOUBLE PRECISION = -1; +BEGIN + SET CONSTRAINTS thunderdome.poker_story_poker_id_position DEFERRED; + + active_storyid := (SELECT b.active_story_id FROM thunderdome.poker b WHERE b.id = pokerid); + DELETE FROM thunderdome.poker_story WHERE id = storyid; + + FOR story IN SELECT id, position FROM thunderdome.poker_story WHERE poker_id = pokerid ORDER BY position + LOOP + pos = pos + 1; + + UPDATE thunderdome.poker_story SET position = pos WHERE id = story.id; + END LOOP; + + IF active_storyid = storyid THEN + UPDATE thunderdome.poker SET last_active = NOW(), voting_locked = true, active_story_id = null + WHERE id = pokerid; + END IF; + + COMMIT; +END$$; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +CREATE OR REPLACE PROCEDURE thunderdome.poker_story_delete(IN pokerid uuid, IN storyid uuid) + LANGUAGE plpgsql + AS $$ +DECLARE + active_storyid UUID; + story RECORD; + pos DOUBLE PRECISION = -1; +BEGIN + active_storyid := (SELECT b.active_story_id FROM thunderdome.poker b WHERE b.id = pokerid); + DELETE FROM thunderdome.poker_story WHERE id = storyid; + + FOR story IN SELECT id, position FROM thunderdome.poker_story WHERE poker_id = pokerid ORDER BY position + LOOP + pos = pos + 1; + + UPDATE thunderdome.poker_story SET position = pos WHERE id = story.id; + END LOOP; + + IF active_storyid = storyid THEN + UPDATE thunderdome.poker SET last_active = NOW(), voting_locked = true, active_story_id = null + WHERE id = pokerid; + END IF; + + COMMIT; +END$$; +-- +goose StatementEnd