-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 20240222084232_update_delete_pokerstory_proc.sql
This resolves #492
- Loading branch information
1 parent
508e2a3
commit 7aeaf0e
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
internal/db/migrations/20240222084232_update_delete_pokerstory_proc.sql
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,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 |