Create inventory__location table
CREATE TABLE inventory__location (
id uuid PRIMARY KEY,
code varchar(64) NOT NULL,
name varchar(256) NOT NULL,
type varchar(16) NOT NULL,
active boolean NOT NULL DEFAULT true,
ext jsonb NOT NULL DEFAULT '{}'::jsonb,
created_at timestamptz NOT NULL,
created_by varchar(128) NOT NULL,
updated_at timestamptz NOT NULL,
updated_by varchar(128) NOT NULL,
version bigint NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX inventory__location_code_uk ON inventory__location (code);
CREATE INDEX inventory__location_type_idx ON inventory__location (type);
CREATE INDEX inventory__location_active_idx ON inventory__location (active);
CREATE INDEX inventory__location_ext_gin ON inventory__location USING GIN (ext jsonb_path_ops);
DROP TABLE inventory__location;
Create inventory__stock_balance table (FK to inventory__location, no FK to catalog__item)
CREATE TABLE inventory__stock_balance (
id uuid PRIMARY KEY,
item_code varchar(64) NOT NULL,
location_id uuid NOT NULL REFERENCES inventory__location(id),
quantity numeric(18,4) NOT NULL,
created_at timestamptz NOT NULL,
created_by varchar(128) NOT NULL,
updated_at timestamptz NOT NULL,
updated_by varchar(128) NOT NULL,
version bigint NOT NULL DEFAULT 0,
CONSTRAINT inventory__stock_balance_nonneg CHECK (quantity >= 0)
);
CREATE UNIQUE INDEX inventory__stock_balance_item_loc_uk
ON inventory__stock_balance (item_code, location_id);
CREATE INDEX inventory__stock_balance_item_idx ON inventory__stock_balance (item_code);
CREATE INDEX inventory__stock_balance_loc_idx ON inventory__stock_balance (location_id);
DROP TABLE inventory__stock_balance;