-- target-database: cmvr-project-dev CREATE TABLE IF NOT EXISTS inventory_cost_lock (id int NOT NULL PRIMARY KEY) ENGINE=InnoDB; INSERT INTO inventory_cost_lock(id) SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM inventory_cost_lock WHERE id=1); CREATE TABLE IF NOT EXISTS project_stock_balance ( id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, project_id bigint NOT NULL, item_id bigint NOT NULL, area_id bigint NOT NULL, quantity decimal(20,4) NOT NULL DEFAULT 0, version int NOT NULL DEFAULT 0, UNIQUE KEY uk_project_item_area (project_id,item_id,area_id), CONSTRAINT fk_psb_project FOREIGN KEY(project_id) REFERENCES project_info(id), CONSTRAINT fk_psb_item FOREIGN KEY(item_id) REFERENCES product_info(id), CONSTRAINT fk_psb_area FOREIGN KEY(area_id) REFERENCES warehouse_area(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS project_item_cost ( project_id bigint NOT NULL, item_id bigint NOT NULL, quantity decimal(20,4) NOT NULL DEFAULT 0, amount decimal(24,6) DEFAULT 0, version int NOT NULL DEFAULT 0, PRIMARY KEY(project_id,item_id), CONSTRAINT fk_pic_project FOREIGN KEY(project_id) REFERENCES project_info(id), CONSTRAINT fk_pic_item FOREIGN KEY(item_id) REFERENCES product_info(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS inventory_cost_event ( id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, project_id bigint NOT NULL, item_id bigint NOT NULL, area_id bigint NOT NULL, event_key varchar(160) NOT NULL, event_type varchar(16) NOT NULL, quantity decimal(20,4) NOT NULL, receipt_amount decimal(24,6) DEFAULT NULL, source_id bigint DEFAULT NULL, original_cost decimal(24,6) DEFAULT NULL, effective_cost decimal(24,6) DEFAULT NULL, active tinyint NOT NULL DEFAULT 1, create_by varchar(64) DEFAULT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, void_reason varchar(300) DEFAULT NULL, void_by varchar(64) DEFAULT NULL, void_time datetime DEFAULT NULL, UNIQUE KEY uk_inventory_event_key(event_key), KEY idx_inventory_cost_stream(project_id,item_id,id), CONSTRAINT fk_ice_project FOREIGN KEY(project_id) REFERENCES project_info(id), CONSTRAINT fk_ice_item FOREIGN KEY(item_id) REFERENCES product_info(id), CONSTRAINT fk_ice_area FOREIGN KEY(area_id) REFERENCES warehouse_area(id), CONSTRAINT fk_ice_source FOREIGN KEY(source_id) REFERENCES inventory_cost_event(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS inventory_cost_revision ( id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, revision_key varchar(160) NOT NULL, target_event_id bigint NOT NULL, new_amount decimal(24,6) DEFAULT NULL, expected_version int NOT NULL, create_by varchar(64) DEFAULT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY uk_inventory_revision_key(revision_key), KEY idx_inventory_revision_target(target_event_id,id), CONSTRAINT fk_icr_target FOREIGN KEY(target_event_id) REFERENCES inventory_cost_event(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS inventory_cost_change ( id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, revision_id bigint NOT NULL, event_id bigint NOT NULL, old_amount decimal(24,6) DEFAULT NULL, new_amount decimal(24,6) DEFAULT NULL, delta_amount decimal(24,6) DEFAULT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY uk_inventory_revision_event(revision_id,event_id), CONSTRAINT fk_icc_revision FOREIGN KEY(revision_id) REFERENCES inventory_cost_revision(id), CONSTRAINT fk_icc_event FOREIGN KEY(event_id) REFERENCES inventory_cost_event(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;