Add position column in labels table

This commit is contained in:
Hongbo Wu
2022-07-27 21:38:52 +08:00
parent 5e2e2a2151
commit 5e8508e025
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,25 @@
-- Type: DO
-- Name: add_position_to_labels
-- Description: Add position column to labels table
BEGIN;
ALTER TABLE omnivore.labels ADD COLUMN position INTEGER NOT NULL DEFAULT 0;
WITH positions AS (
SELECT
id, row_number() OVER (PARTITION BY user_id ORDER BY name) as row_num
FROM
omnivore.labels
) UPDATE
omnivore.labels
SET
position = positions.row_num
FROM
positions
WHERE
omnivore.labels.id = positions.id;
ALTER TABLE omnivore.labels ADD constraint labels_position_user_id_unique UNIQUE(user_id, position);
COMMIT;

View File

@ -0,0 +1,9 @@
-- Type: UNDO
-- Name: add_position_to_labels
-- Description: Add position column to labels table
BEGIN;
ALTER TABLE omnivore.labels DROP COLUMN IF EXISTS position;
COMMIT;