-- Add missing sequence for categories_v2_lft_rgt.id to support auto-increment on insert.
-- This is required by the method we use to insert new category nodes in pw_category_tree.php->insertNode()

CREATE SEQUENCE IF NOT EXISTS categories_v2_lft_rgt_id_seq
    AS bigint
    START WITH 1
    INCREMENT BY 1
    CACHE 1;

SELECT setval('categories_v2_lft_rgt_id_seq', COALESCE((SELECT MAX(id) FROM categories_v2_lft_rgt), 0) + 1, false);

ALTER TABLE categories_v2_lft_rgt
    ALTER COLUMN id SET DEFAULT nextval('categories_v2_lft_rgt_id_seq'::regclass);

ALTER SEQUENCE categories_v2_lft_rgt_id_seq OWNED BY categories_v2_lft_rgt.id;
