fix(database): 修复缺失表结构并完善索引检查逻辑

- 新增 fix-missing-tables.php 脚本用于补建 settlement_batches 和 system_jobs 表
- 为 settlement_batches 表添加 draw_id 和 settle_version 联合索引
- 在迁移文件中添加表存在性检查避免索引操作失败
- 补录相关迁移记录到 migrations 表中确保迁移状态一致
- 完善 schema 检查逻辑防止对不存在的表进行索引操作
This commit is contained in:
2026-06-10 14:42:23 +08:00
parent c887c4e146
commit 1d13d19b65
2 changed files with 95 additions and 4 deletions

View File

@@ -8,13 +8,13 @@ return new class extends Migration
{
public function up(): void
{
if (! $this->hasIndex('ticket_items', 'idx_ticket_items_order_id')) {
if (Schema::hasTable('ticket_items') && ! $this->hasIndex('ticket_items', 'idx_ticket_items_order_id')) {
Schema::table('ticket_items', function (Blueprint $table): void {
$table->index('order_id', 'idx_ticket_items_order_id');
});
}
if (! $this->hasIndex('settlement_batches', 'idx_settlement_batches_result_batch_id')) {
if (Schema::hasTable('settlement_batches') && ! $this->hasIndex('settlement_batches', 'idx_settlement_batches_result_batch_id')) {
Schema::table('settlement_batches', function (Blueprint $table): void {
$table->index('result_batch_id', 'idx_settlement_batches_result_batch_id');
});
@@ -23,13 +23,13 @@ return new class extends Migration
public function down(): void
{
if ($this->hasIndex('settlement_batches', 'idx_settlement_batches_result_batch_id')) {
if (Schema::hasTable('settlement_batches') && $this->hasIndex('settlement_batches', 'idx_settlement_batches_result_batch_id')) {
Schema::table('settlement_batches', function (Blueprint $table): void {
$table->dropIndex('idx_settlement_batches_result_batch_id');
});
}
if ($this->hasIndex('ticket_items', 'idx_ticket_items_order_id')) {
if (Schema::hasTable('ticket_items') && $this->hasIndex('ticket_items', 'idx_ticket_items_order_id')) {
Schema::table('ticket_items', function (Blueprint $table): void {
$table->dropIndex('idx_ticket_items_order_id');
});

91
fix-missing-tables.php Normal file
View File

@@ -0,0 +1,91 @@
<?php
/**
* 一次性修复脚本:补建生产库缺失的 settlement_batches system_jobs 表。
*
* 用法php fix-missing-tables.php
* 执行完毕后删除此文件。
*/
require __DIR__ . '/vendor/autoload.php';
$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
$created = [];
$skipped = [];
// ── 1. settlement_batches ──
if (! Schema::hasTable('settlement_batches')) {
DB::statement('
CREATE TABLE settlement_batches (
id bigserial PRIMARY KEY,
draw_id bigint NOT NULL REFERENCES draws(id) ON DELETE CASCADE,
result_batch_id bigint NOT NULL REFERENCES draw_result_batches(id) ON DELETE CASCADE,
settle_version integer NOT NULL DEFAULT 1,
status varchar(32) NOT NULL,
total_ticket_count integer NOT NULL DEFAULT 0,
total_win_count integer NOT NULL DEFAULT 0,
total_payout_amount bigint NOT NULL DEFAULT 0,
total_jackpot_payout_amount bigint NOT NULL DEFAULT 0,
review_status varchar(32) NOT NULL DEFAULT \'pending\',
reviewed_by bigint REFERENCES admin_users(id) ON DELETE SET NULL,
reviewed_at timestamp,
review_remark varchar(255),
paid_at timestamp,
started_at timestamp,
finished_at timestamp,
created_at timestamp,
updated_at timestamp
)
');
DB::statement('CREATE INDEX idx_settlement_batches_draw_version ON settlement_batches (draw_id, settle_version)');
$created[] = 'settlement_batches';
} else {
$skipped[] = 'settlement_batches (已存在)';
}
// 补录迁移记录
if (! DB::table('migrations')->where('migration', '2026_05_08_130008_create_settlement_and_jackpot_tables')->exists()) {
DB::table('migrations')->insert([
'migration' => '2026_05_08_130008_create_settlement_and_jackpot_tables',
'batch' => 99,
]);
}
// ── 2. system_jobs ──
if (! Schema::hasTable('system_jobs')) {
DB::statement('
CREATE TABLE system_jobs (
id bigserial PRIMARY KEY,
job_key varchar(128) UNIQUE NOT NULL,
name varchar(128) NOT NULL,
schedule_cron varchar(64),
is_enabled boolean NOT NULL DEFAULT true,
last_started_at timestamp,
last_finished_at timestamp,
last_status varchar(32),
created_at timestamp,
updated_at timestamp
)
');
$created[] = 'system_jobs';
} else {
$skipped[] = 'system_jobs (已存在)';
}
// 补录迁移记录
if (! DB::table('migrations')->where('migration', '2026_05_08_130009_create_report_audit_reconcile_tables')->exists()) {
DB::table('migrations')->insert([
'migration' => '2026_05_08_130009_create_report_audit_reconcile_tables',
'batch' => 99,
]);
}
// ── 输出结果 ──
echo "=== 修复完成 ===\n";
echo "新建: " . (empty($created) ? '无' : implode(', ', $created)) . "\n";
echo "跳过: " . (empty($skipped) ? '无' : implode(', ', $skipped)) . "\n";
echo "\n接下来请执行: php artisan migrate --force\n";