Files
BMS/bms-back/sql/price/rollback_prc_order_schema.sql
T
2026-10-16 20:28:49 +08:00

192 lines
6.6 KiB
SQL

-- Restore schema to pre-prc_order (code rollback).
-- Run on database bms. Safe to re-run.
--
-- DATA: prc_order_ext was TRUNCATE before the FK.
-- Those ext rows are gone. This script only restores table shape.
-- Row data needs a backup / binlog from before that TRUNCATE.
SET NAMES utf8mb4;
SELECT DATABASE() AS current_db;
-- 1) Drop FK from ext -> prc_order (table may still be prc_order_ext)
SET @fk := (
SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_ext'
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
AND CONSTRAINT_NAME = 'fk_order_ext_order'
LIMIT 1
);
SET @sql := IF(@fk IS NOT NULL,
'ALTER TABLE prc_order_ext DROP FOREIGN KEY fk_order_ext_order',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 2) Rename prc_order_ext -> prc_order_bill_ext
SET @new_tbl := (
SELECT TABLE_NAME FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'prc_order_ext' LIMIT 1
);
SET @old_tbl := (
SELECT TABLE_NAME FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'prc_order_bill_ext' LIMIT 1
);
SET @sql := IF(@new_tbl IS NOT NULL AND @old_tbl IS NULL,
'RENAME TABLE prc_order_ext TO prc_order_bill_ext',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 3) PK column order_id -> bill_id
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
AND COLUMN_NAME = 'order_id'
LIMIT 1
);
SET @sql := IF(@col IS NOT NULL,
'ALTER TABLE prc_order_bill_ext CHANGE COLUMN order_id bill_id varchar(36) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT \'计费订单ID,对应 prc_order_bill.bill_id\'',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 4) Put back columns that were moved off the ext table
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
AND COLUMN_NAME = 'product_floor_code'
LIMIT 1
);
SET @sql := IF(@col IS NULL,
'ALTER TABLE prc_order_bill_ext ADD COLUMN product_floor_code text NULL COMMENT \'渠道编码\'',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
AND COLUMN_NAME = 'order_status'
LIMIT 1
);
SET @sql := IF(@col IS NULL,
'ALTER TABLE prc_order_bill_ext ADD COLUMN order_status tinyint(4) DEFAULT NULL COMMENT \'订单状态\'',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
AND COLUMN_NAME = 'pickup_date'
LIMIT 1
);
SET @sql := IF(@col IS NULL,
'ALTER TABLE prc_order_bill_ext ADD COLUMN pickup_date datetime DEFAULT NULL COMMENT \'收件时间\'',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
AND COLUMN_NAME = 'bl_pickup'
LIMIT 1
);
SET @sql := IF(@col IS NULL,
'ALTER TABLE prc_order_bill_ext ADD COLUMN bl_pickup tinyint(4) DEFAULT NULL COMMENT \'是否收件 0未收件 1已收件\'',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
AND COLUMN_NAME = 'dimensional_weight_factor'
LIMIT 1
);
SET @sql := IF(@col IS NULL,
'ALTER TABLE prc_order_bill_ext ADD COLUMN dimensional_weight_factor decimal(10,3) DEFAULT NULL COMMENT \'计费重\'',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 5) Drop orphan ext rows, then restore FK to prc_order_bill
DELETE e FROM prc_order_bill_ext e
LEFT JOIN prc_order_bill b ON b.bill_id = e.bill_id
WHERE b.bill_id IS NULL;
SET @fk := (
SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill_ext'
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
AND CONSTRAINT_NAME = 'fk_order_bill_ext_bill'
LIMIT 1
);
SET @sql := IF(@fk IS NULL,
'ALTER TABLE prc_order_bill_ext ADD CONSTRAINT fk_order_bill_ext_bill FOREIGN KEY (bill_id) REFERENCES prc_order_bill (bill_id) ON DELETE CASCADE',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
ALTER TABLE prc_order_bill_ext COMMENT = '订单计费扩展表';
-- 6) Drop prc_order_bill.order_id (and its index)
SET @idx := (
SELECT INDEX_NAME FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill'
AND INDEX_NAME = 'idx_order_bill_order_id'
LIMIT 1
);
SET @sql := IF(@idx IS NOT NULL,
'ALTER TABLE prc_order_bill DROP INDEX idx_order_bill_order_id',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill'
AND COLUMN_NAME = 'order_id'
LIMIT 1
);
SET @sql := IF(@col IS NOT NULL,
'ALTER TABLE prc_order_bill DROP COLUMN order_id',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 7) Drop prc_order_bill.pickup_date added in the rolled-back change
-- Receive time stays on prc_order_bill_ext.pickup_date
SET @idx := (
SELECT INDEX_NAME FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill'
AND INDEX_NAME = 'idx_order_bill_pickup_date'
LIMIT 1
);
SET @sql := IF(@idx IS NOT NULL,
'ALTER TABLE prc_order_bill DROP INDEX idx_order_bill_pickup_date',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @col := (
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'prc_order_bill'
AND COLUMN_NAME = 'pickup_date'
LIMIT 1
);
SET @sql := IF(@col IS NOT NULL,
'ALTER TABLE prc_order_bill DROP COLUMN pickup_date',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- 8) Drop new master table
SET @tbl := (
SELECT TABLE_NAME FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'prc_order' LIMIT 1
);
SET @sql := IF(@tbl IS NOT NULL, 'DROP TABLE prc_order', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;