chore: add report review sql migration
This commit is contained in:
67
server-api/tools/schema_upgrade_report_review_flow.php
Normal file → Executable file
67
server-api/tools/schema_upgrade_report_review_flow.php
Normal file → Executable file
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
@@ -7,11 +8,17 @@ require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
|
||||
$dotenv->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";
|
||||
|
||||
Reference in New Issue
Block a user