-- target-database: cmvr-project-dev CREATE TABLE IF NOT EXISTS inventory_cost_link ( source_event_id bigint NOT NULL, target_event_id bigint NOT NULL PRIMARY KEY, CONSTRAINT fk_cost_link_source FOREIGN KEY(source_event_id) REFERENCES inventory_cost_event(id), CONSTRAINT fk_cost_link_target FOREIGN KEY(target_event_id) REFERENCES inventory_cost_event(id), CONSTRAINT ck_cost_link_order CHECK(source_event_idborrower_project_id), CONSTRAINT ck_loan_qty CHECK(quantity>0) ) ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS project_loan_return ( id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, loan_id bigint NOT NULL, quantity decimal(20,4) NOT NULL, business_key varchar(160) NOT NULL UNIQUE, return_type varchar(20) NOT NULL, out_event_id bigint NULL, in_event_id bigint NOT NULL UNIQUE, active tinyint NOT NULL DEFAULT 1, create_by varchar(64) NOT NULL, create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT fk_loan_return_loan FOREIGN KEY(loan_id) REFERENCES project_loan(id), CONSTRAINT fk_loan_return_out FOREIGN KEY(out_event_id) REFERENCES inventory_cost_event(id), CONSTRAINT fk_loan_return_in FOREIGN KEY(in_event_id) REFERENCES inventory_cost_event(id), CONSTRAINT ck_loan_return_qty CHECK(quantity>0) ) ENGINE=InnoDB; ALTER TABLE purchase_request ADD COLUMN purchase_purpose varchar(10) NOT NULL DEFAULT 'SELF'; ALTER TABLE purchase_request ADD COLUMN loan_id bigint NULL;