feat(M04-B): 完成订单状态机与迁移历史

This commit is contained in:
Codex
2026-06-20 13:53:55 +08:00
parent 725028cb2d
commit 40c891f1b7
19 changed files with 698 additions and 16 deletions
@@ -0,0 +1,5 @@
DELETE FROM qipai_schema_migrations WHERE version = '2026062012';
DROP TABLE IF EXISTS qipai_order_status_history;
ALTER TABLE qipai_orders
DROP COLUMN status_updated_at,
DROP COLUMN status_version;
@@ -0,0 +1,38 @@
ALTER TABLE qipai_orders
ADD COLUMN status_version INT UNSIGNED NOT NULL DEFAULT 1 AFTER status,
ADD COLUMN status_updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
AFTER status_version;
CREATE TABLE IF NOT EXISTS qipai_order_status_history (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
tenant_id BIGINT UNSIGNED NOT NULL,
order_id BIGINT UNSIGNED NOT NULL,
from_status VARCHAR(32) NULL,
to_status VARCHAR(32) NOT NULL,
action VARCHAR(32) NOT NULL,
actor_type VARCHAR(32) NOT NULL,
actor_id BIGINT UNSIGNED NULL,
source VARCHAR(32) NOT NULL,
reason VARCHAR(512) NOT NULL DEFAULT '',
trace_id VARCHAR(128) NOT NULL,
metadata JSON NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
CONSTRAINT fk_qipai_order_history_tenant
FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
CONSTRAINT fk_qipai_order_history_order
FOREIGN KEY (order_id) REFERENCES qipai_orders(id),
UNIQUE KEY uq_qipai_order_history_trace (tenant_id, order_id, trace_id),
KEY idx_qipai_order_history_order (tenant_id, order_id, id),
KEY idx_qipai_order_history_status (tenant_id, to_status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT IGNORE INTO qipai_order_status_history
(tenant_id, order_id, from_status, to_status, action, actor_type,
actor_id, source, reason, trace_id, metadata, created_at)
SELECT tenant_id, id, NULL, status, 'MIGRATED', 'SYSTEM',
NULL, 'MIGRATION', 'M04-B baseline history',
CONCAT('m04b-migration-', id), JSON_OBJECT('statusVersion', 1), created_at
FROM qipai_orders;
INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026062012', 'm04b_order_state_machine');
@@ -0,0 +1,20 @@
SELECT column_name FROM information_schema.columns
WHERE table_schema = DATABASE() AND table_name = 'qipai_orders'
AND column_name IN ('status_version', 'status_updated_at')
ORDER BY column_name;
SELECT table_name FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name = 'qipai_order_status_history';
SELECT index_name FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = 'qipai_order_status_history'
AND index_name IN (
'uq_qipai_order_history_trace',
'idx_qipai_order_history_order',
'idx_qipai_order_history_status'
)
GROUP BY index_name ORDER BY index_name;
SELECT version, name FROM qipai_schema_migrations WHERE version = '2026062012';