-- Schema 15 repairs early, incompatible schema-14 payment configuration tables. -- The legacy table is retained verbatim and is never interpreted as active config. SET @schema = DATABASE(); SET @sql = ( SELECT IF( EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema=@schema AND table_name='payment_channel_configs') AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=@schema AND table_name='payment_channel_configs' AND column_name='active_version'), 'RENAME TABLE payment_channel_configs TO payment_channel_configs_legacy_v14', 'SELECT 1' ) ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; CREATE TABLE IF NOT EXISTS payment_channel_config_versions ( channel VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, version BIGINT UNSIGNED NOT NULL, enabled TINYINT(1) NOT NULL DEFAULT 0, config_encrypted MEDIUMBLOB NOT NULL, created_by BIGINT UNSIGNED NULL, created_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (channel, version), CONSTRAINT fk_payment_config_version_admin FOREIGN KEY (created_by) REFERENCES admins(id) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; CREATE TABLE IF NOT EXISTS payment_channel_configs ( channel VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, active_version BIGINT UNSIGNED NULL, PRIMARY KEY (channel), CONSTRAINT fk_payment_config_active FOREIGN KEY (channel, active_version) REFERENCES payment_channel_config_versions(channel, version) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; INSERT IGNORE INTO payment_channel_configs(channel) VALUES ('alipay'), ('wechat'); SET @sql = ( SELECT IF( NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=@schema AND table_name='payment_orders' AND column_name='provider_config_version'), 'ALTER TABLE payment_orders ADD COLUMN provider_config_version BIGINT UNSIGNED NULL AFTER channel', 'SELECT 1' ) ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @sql = ( SELECT IF( NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema=@schema AND table_name='payment_orders' AND constraint_name='fk_payment_order_config_version'), 'ALTER TABLE payment_orders ADD CONSTRAINT fk_payment_order_config_version FOREIGN KEY (channel, provider_config_version) REFERENCES payment_channel_config_versions(channel, version)', 'SELECT 1' ) ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;