-- Etichette
CREATE TABLE IF NOT EXISTS public.etichette (
    id_etichetta integer NOT NULL,
    title TEXT NOT NULL,
    color_hex character varying(7) NOT NULL
);
-- Sequence for id_etichetta
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_sequences 
    WHERE schemaname = 'public' AND sequencename = 'etichette_id_etichetta_seq'
    ) THEN
        CREATE SEQUENCE public.etichette_id_etichetta_seq 
        AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
    END IF;
END $$;

ALTER SEQUENCE public.etichette_id_etichetta_seq OWNED BY public.etichette.id_etichetta;
-- Set default for id_etichetta
ALTER TABLE public.etichette
ALTER COLUMN id_etichetta
SET DEFAULT nextval(
    'public.etichette_id_etichetta_seq'::regclass
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.etichette 
ADD CONSTRAINT etichette_pkey PRIMARY KEY (id_etichetta);
    EXCEPTION
        WHEN invalid_table_definition THEN
            RAISE NOTICE 'Primary key already exists. Ignoring...'; 
END $$;