mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Decrement position after deletion
This commit is contained in:
parent
bac33e887c
commit
0959085c9d
1 changed files with 9 additions and 4 deletions
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
BEGIN;
|
||||
|
||||
ALTER TABLE omnivore.labels ADD COLUMN position INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE omnivore.labels ADD COLUMN position integer NOT NULL DEFAULT 0;
|
||||
|
||||
WITH positions AS (
|
||||
SELECT
|
||||
|
|
@ -29,16 +29,21 @@ CREATE OR REPLACE FUNCTION update_label_position()
|
|||
UPDATE omnivore.labels SET position = position - 1 WHERE user_id = OLD.user_id AND position > OLD.position;
|
||||
RETURN OLD;
|
||||
ELSIF (TG_OP = 'INSERT') THEN
|
||||
SELECT MAX(position) + 1 INTO new_position FROM omnivore.labels WHERE user_id = NEW.user_id;
|
||||
SELECT COALESCE(MAX(position), 0) + 1 INTO new_position FROM omnivore.labels WHERE user_id = NEW.user_id;
|
||||
NEW.position = new_position;
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
CREATE TRIGGER update_label_position
|
||||
BEFORE INSERT OR DELETE ON omnivore.labels
|
||||
CREATE TRIGGER increment_label_position
|
||||
BEFORE INSERT ON omnivore.labels
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_label_position();
|
||||
|
||||
CREATE TRIGGER decrement_label_position
|
||||
AFTER DELETE ON omnivore.labels
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_label_position();
|
||||
|
||||
COMMIT;
|
||||
|
|
|
|||
Loading…
Reference in a new issue