-- Add columns to articulos ALTER TABLE public.articulos ADD COLUMN IF NOT EXISTS tipo_control character varying(50) NOT NULL DEFAULT 'UNITARIO'; ALTER TABLE public.articulos ADD COLUMN IF NOT EXISTS cantidad_global integer NOT NULL DEFAULT 1; -- Add columns to movimientos_inventario ALTER TABLE public.movimientos_inventario ADD COLUMN IF NOT EXISTS cantidad integer NOT NULL DEFAULT 1; -- Create existencias table CREATE TABLE IF NOT EXISTS public.existencias ( id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ), articulo_id integer NOT NULL, ubicacion_id integer NOT NULL, cantidad integer NOT NULL DEFAULT 0, actualizado_en timestamp without time zone NOT NULL DEFAULT (now() AT TIME ZONE 'utc'::text), CONSTRAINT existencias_pkey PRIMARY KEY (id), CONSTRAINT fk_existencias_articulo FOREIGN KEY (articulo_id) REFERENCES public.articulos (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT fk_existencias_ubicacion FOREIGN KEY (ubicacion_id) REFERENCES public.ubicaciones (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT uq_existencias_articulo_ubicacion UNIQUE (articulo_id, ubicacion_id) ) TABLESPACE pg_default; ALTER TABLE IF EXISTS public.existencias OWNER to postgres; -- Index for existencias CREATE INDEX IF NOT EXISTS ix_existencias_articulo_id ON public.existencias USING btree (articulo_id ASC NULLS LAST) TABLESPACE pg_default; CREATE INDEX IF NOT EXISTS ix_existencias_ubicacion_id ON public.existencias USING btree (ubicacion_id ASC NULLS LAST) TABLESPACE pg_default;