-- Add migration script here
CREATE TABLE unit
(
    id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 0 MINVALUE 0 ),
    name text NOT NULL,
    display text NOT NULL,
    CONSTRAINT unit_id PRIMARY KEY (id)
        INCLUDE(id)
);

-- Insert units
INSERT INTO unit (name, display) VALUES ('KG', 'kg');
INSERT INTO unit (name, display) VALUES ('G', 'g');
INSERT INTO unit (name, display) VALUES ('L', 'l');
INSERT INTO unit (name, display) VALUES ('ML', 'ml');
INSERT INTO unit (name, display) VALUES ('Stk', 'Stk');

-- Alter article and variant table to add unit
ALTER TABLE public.variant
    ADD unit_id bigint NOT NULL DEFAULT 1,
    ADD CONSTRAINT variant_unit_id_fkey
       foreign key (unit_id)
           references unit(id)
           ON DELETE NO ACTION;

ALTER TABLE public.article
    ADD default_unit_id bigint NOT NULL DEFAULT 1,
    ADD CONSTRAINT article_unit_id_fkey
        foreign key (default_unit_id)
            references unit(id)
            ON DELETE NO ACTION;

-- Add display text to origin table
ALTER TABLE public.origin
    ADD display text NOT NULL DEFAULT 'NOT SET';
