From 5cd94d138d587fbbea675ba434b04b074f56e6fb Mon Sep 17 00:00:00 2001 From: wushumin Date: Thu, 4 Jun 2026 14:14:20 +0800 Subject: [PATCH] chore: add report review sql migration --- .../schema_upgrade_report_review_flow.sql | 259 ++++++++++++++++++ .../schema_upgrade_report_review_flow.php | 67 ++++- 2 files changed, 320 insertions(+), 6 deletions(-) create mode 100644 server-api/database/schema_upgrade_report_review_flow.sql mode change 100644 => 100755 server-api/tools/schema_upgrade_report_review_flow.php diff --git a/server-api/database/schema_upgrade_report_review_flow.sql b/server-api/database/schema_upgrade_report_review_flow.sql new file mode 100644 index 0000000..199b386 --- /dev/null +++ b/server-api/database/schema_upgrade_report_review_flow.sql @@ -0,0 +1,259 @@ +-- Report review / publish flow schema migration. +-- Navicat friendly: no DELIMITER, no stored procedure. +-- Usage: +-- Select the target database first, then run this whole file. + +SELECT 'START schema_upgrade_report_review_flow' AS migration_step; + +SET @reports_exists := ( + SELECT COUNT(*) + FROM information_schema.TABLES + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'reports' +); +SELECT IF(@reports_exists = 0, 'ERROR reports table does not exist', 'OK reports table exists') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'reports' + AND COLUMN_NAME = 'reject_reason' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE reports ADD COLUMN reject_reason VARCHAR(500) NOT NULL DEFAULT '''' AFTER invalid_reason', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN reports.reject_reason', 'SKIP_COLUMN reports.reject_reason') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'reports' + AND COLUMN_NAME = 'rejected_by' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE reports ADD COLUMN rejected_by BIGINT UNSIGNED NULL DEFAULT NULL AFTER reject_reason', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN reports.rejected_by', 'SKIP_COLUMN reports.rejected_by') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'reports' + AND COLUMN_NAME = 'rejected_by_name' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE reports ADD COLUMN rejected_by_name VARCHAR(64) NOT NULL DEFAULT '''' AFTER rejected_by', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN reports.rejected_by_name', 'SKIP_COLUMN reports.rejected_by_name') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'reports' + AND COLUMN_NAME = 'rejected_at' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE reports ADD COLUMN rejected_at DATETIME NULL DEFAULT NULL AFTER rejected_by_name', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN reports.rejected_at', 'SKIP_COLUMN reports.rejected_at') AS migration_step; + +UPDATE reports + SET reject_reason = invalid_reason + WHERE report_status = 'rejected' + AND reject_reason = '' + AND invalid_reason <> ''; +SELECT CONCAT('SYNC_REJECT_REASON affected_rows=', ROW_COUNT()) AS migration_step; + +CREATE TABLE IF NOT EXISTS report_logs ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + report_id BIGINT UNSIGNED NOT NULL, + action VARCHAR(64) NOT NULL, + operator_id BIGINT UNSIGNED NULL DEFAULT NULL, + operator_name VARCHAR(64) NOT NULL DEFAULT '', + before_data LONGTEXT NULL, + after_data LONGTEXT NULL, + remark VARCHAR(255) NOT NULL DEFAULT '', + created_at DATETIME NULL DEFAULT NULL, + PRIMARY KEY (id), + KEY idx_report_logs_report_id (report_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报告操作日志'; +SELECT 'ENSURE_TABLE report_logs' AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND COLUMN_NAME = 'report_id' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE report_logs ADD COLUMN report_id BIGINT UNSIGNED NOT NULL AFTER id', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.report_id', 'SKIP_COLUMN report_logs.report_id') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND COLUMN_NAME = 'action' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE report_logs ADD COLUMN action VARCHAR(64) NOT NULL DEFAULT '''' AFTER report_id', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.action', 'SKIP_COLUMN report_logs.action') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND COLUMN_NAME = 'operator_id' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE report_logs ADD COLUMN operator_id BIGINT UNSIGNED NULL DEFAULT NULL AFTER action', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.operator_id', 'SKIP_COLUMN report_logs.operator_id') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND COLUMN_NAME = 'operator_name' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE report_logs ADD COLUMN operator_name VARCHAR(64) NOT NULL DEFAULT '''' AFTER operator_id', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.operator_name', 'SKIP_COLUMN report_logs.operator_name') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND COLUMN_NAME = 'before_data' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE report_logs ADD COLUMN before_data LONGTEXT NULL AFTER operator_name', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.before_data', 'SKIP_COLUMN report_logs.before_data') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND COLUMN_NAME = 'after_data' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE report_logs ADD COLUMN after_data LONGTEXT NULL AFTER before_data', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.after_data', 'SKIP_COLUMN report_logs.after_data') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND COLUMN_NAME = 'remark' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE report_logs ADD COLUMN remark VARCHAR(255) NOT NULL DEFAULT '''' AFTER after_data', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.remark', 'SKIP_COLUMN report_logs.remark') AS migration_step; + +SET @column_exists := ( + SELECT COUNT(*) + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND COLUMN_NAME = 'created_at' +); +SET @sql := IF( + @column_exists = 0, + 'ALTER TABLE report_logs ADD COLUMN created_at DATETIME NULL DEFAULT NULL AFTER remark', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.created_at', 'SKIP_COLUMN report_logs.created_at') AS migration_step; + +SET @index_exists := ( + SELECT COUNT(*) + FROM information_schema.STATISTICS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'report_logs' + AND INDEX_NAME = 'idx_report_logs_report_id' +); +SET @sql := IF( + @index_exists = 0, + 'ALTER TABLE report_logs ADD KEY idx_report_logs_report_id (report_id)', + 'DO 0' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; +SELECT IF(@index_exists = 0, 'ADD_INDEX report_logs.idx_report_logs_report_id', 'SKIP_INDEX report_logs.idx_report_logs_report_id') AS migration_step; + +SELECT 'SCHEMA_UPGRADE_REPORT_REVIEW_FLOW_OK' AS migration_step; diff --git a/server-api/tools/schema_upgrade_report_review_flow.php b/server-api/tools/schema_upgrade_report_review_flow.php old mode 100644 new mode 100755 index 60916fb..e88f578 --- a/server-api/tools/schema_upgrade_report_review_flow.php +++ b/server-api/tools/schema_upgrade_report_review_flow.php @@ -1,3 +1,4 @@ +#!/usr/bin/env php safeLoad(); +$database = trim((string)($_ENV['DB_DATABASE'] ?? '')); +if ($database === '') { + fwrite(STDERR, "DB_DATABASE is required in server-api/.env or environment variables.\n"); + exit(1); +} + $dsn = sprintf( 'mysql:host=%s;port=%s;dbname=%s;charset=%s', $_ENV['DB_HOST'] ?? '127.0.0.1', $_ENV['DB_PORT'] ?? '3306', - $_ENV['DB_DATABASE'] ?? '', + $database, $_ENV['DB_CHARSET'] ?? 'utf8mb4' ); @@ -25,6 +32,13 @@ $pdo = new PDO( ] ); +function reportReviewHasTable(PDO $pdo, string $table): bool +{ + $stmt = $pdo->prepare('SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?'); + $stmt->execute([$table]); + return (int)$stmt->fetchColumn() > 0; +} + function reportReviewHasColumn(PDO $pdo, string $table, string $column): bool { $stmt = $pdo->prepare('SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?'); @@ -32,6 +46,13 @@ function reportReviewHasColumn(PDO $pdo, string $table, string $column): bool return (int)$stmt->fetchColumn() > 0; } +function reportReviewHasIndex(PDO $pdo, string $table, string $index): bool +{ + $stmt = $pdo->prepare('SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND INDEX_NAME = ?'); + $stmt->execute([$table, $index]); + return (int)$stmt->fetchColumn() > 0; +} + $reportColumns = [ 'reject_reason' => "ALTER TABLE reports ADD COLUMN reject_reason VARCHAR(500) NOT NULL DEFAULT '' AFTER invalid_reason", 'rejected_by' => 'ALTER TABLE reports ADD COLUMN rejected_by BIGINT UNSIGNED NULL DEFAULT NULL AFTER reject_reason', @@ -40,7 +61,9 @@ $reportColumns = [ ]; foreach ($reportColumns as $column => $sql) { - if (!reportReviewHasColumn($pdo, 'reports', $column)) { + if (reportReviewHasColumn($pdo, 'reports', $column)) { + echo "SKIP_COLUMN reports.{$column}\n"; + } else { $pdo->exec($sql); echo "ADD_COLUMN reports.{$column}\n"; } @@ -48,20 +71,52 @@ foreach ($reportColumns as $column => $sql) { $pdo->exec("UPDATE reports SET reject_reason = invalid_reason WHERE report_status = 'rejected' AND reject_reason = '' AND invalid_reason <> ''"); -$pdo->exec(<<<'SQL' +if (!reportReviewHasTable($pdo, 'report_logs')) { + $pdo->exec(<<<'SQL' CREATE TABLE IF NOT EXISTS report_logs ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, report_id BIGINT UNSIGNED NOT NULL, action VARCHAR(64) NOT NULL, operator_id BIGINT UNSIGNED NULL DEFAULT NULL, operator_name VARCHAR(64) NOT NULL DEFAULT '', - before_data JSON NULL, - after_data JSON NULL, + before_data LONGTEXT NULL, + after_data LONGTEXT NULL, remark VARCHAR(255) NOT NULL DEFAULT '', - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + created_at DATETIME NULL DEFAULT NULL, PRIMARY KEY (id), KEY idx_report_logs_report_id (report_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报告操作日志' SQL); + echo "CREATE_TABLE report_logs\n"; +} else { + echo "SKIP_TABLE report_logs\n"; +} + +$reportLogColumns = [ + 'report_id' => 'ALTER TABLE report_logs ADD COLUMN report_id BIGINT UNSIGNED NOT NULL AFTER id', + 'action' => "ALTER TABLE report_logs ADD COLUMN action VARCHAR(64) NOT NULL DEFAULT '' AFTER report_id", + 'operator_id' => 'ALTER TABLE report_logs ADD COLUMN operator_id BIGINT UNSIGNED NULL DEFAULT NULL AFTER action', + 'operator_name' => "ALTER TABLE report_logs ADD COLUMN operator_name VARCHAR(64) NOT NULL DEFAULT '' AFTER operator_id", + 'before_data' => 'ALTER TABLE report_logs ADD COLUMN before_data LONGTEXT NULL AFTER operator_name', + 'after_data' => 'ALTER TABLE report_logs ADD COLUMN after_data LONGTEXT NULL AFTER before_data', + 'remark' => "ALTER TABLE report_logs ADD COLUMN remark VARCHAR(255) NOT NULL DEFAULT '' AFTER after_data", + 'created_at' => 'ALTER TABLE report_logs ADD COLUMN created_at DATETIME NULL DEFAULT NULL AFTER remark', +]; + +foreach ($reportLogColumns as $column => $sql) { + if (reportReviewHasColumn($pdo, 'report_logs', $column)) { + echo "SKIP_COLUMN report_logs.{$column}\n"; + } else { + $pdo->exec($sql); + echo "ADD_COLUMN report_logs.{$column}\n"; + } +} + +if (reportReviewHasIndex($pdo, 'report_logs', 'idx_report_logs_report_id')) { + echo "SKIP_INDEX report_logs.idx_report_logs_report_id\n"; +} else { + $pdo->exec('ALTER TABLE report_logs ADD KEY idx_report_logs_report_id (report_id)'); + echo "ADD_INDEX report_logs.idx_report_logs_report_id\n"; +} echo "SCHEMA_UPGRADE_REPORT_REVIEW_FLOW_OK\n";