material_manage_system/scripts/migrations/005_phase1_inventory_split.sql
2026-09-16 16:29:04 +08:00

37 lines
2.1 KiB
SQL

CREATE TABLE inventory_balances (
id BIGINT NOT NULL AUTO_INCREMENT,
organization_id BIGINT NOT NULL,
warehouse_id BIGINT NOT NULL,
location_id BIGINT NOT NULL,
material_id BIGINT NOT NULL,
on_hand DECIMAL(20,6) NOT NULL DEFAULT 0,
reserved DECIMAL(20,6) NOT NULL DEFAULT 0,
locked DECIMAL(20,6) NOT NULL DEFAULT 0,
in_transit DECIMAL(20,6) NOT NULL DEFAULT 0,
version INT NOT NULL DEFAULT 1,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY uk_inventory_balance_dimension (organization_id, warehouse_id, location_id, material_id),
KEY idx_inventory_balances_material (organization_id, material_id),
CONSTRAINT fk_inventory_balances_org FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE RESTRICT,
CONSTRAINT fk_inventory_balances_warehouse FOREIGN KEY (warehouse_id) REFERENCES warehouses(id) ON DELETE RESTRICT,
CONSTRAINT fk_inventory_balances_location FOREIGN KEY (location_id) REFERENCES warehouse_locations(id) ON DELETE RESTRICT,
CONSTRAINT fk_inventory_balances_material FOREIGN KEY (material_id) REFERENCES materials(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE stock_warning_rules (
id BIGINT NOT NULL AUTO_INCREMENT,
organization_id BIGINT NOT NULL,
warehouse_id BIGINT NOT NULL,
material_id BIGINT NOT NULL,
minimum_available DECIMAL(20,6) NOT NULL DEFAULT 0,
enabled BOOLEAN NOT NULL DEFAULT TRUE,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY uk_stock_warning_rules_dimension (organization_id, warehouse_id, material_id),
CONSTRAINT fk_stock_warning_rules_org FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE RESTRICT,
CONSTRAINT fk_stock_warning_rules_warehouse FOREIGN KEY (warehouse_id) REFERENCES warehouses(id) ON DELETE RESTRICT,
CONSTRAINT fk_stock_warning_rules_material FOREIGN KEY (material_id) REFERENCES materials(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;