From 1d13d19b65a00a648491f877c81b4dea4d21f89a Mon Sep 17 00:00:00 2001 From: kang Date: Wed, 10 Jun 2026 14:42:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(database):=20=E4=BF=AE=E5=A4=8D=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1=E8=A1=A8=E7=BB=93=E6=9E=84=E5=B9=B6=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 fix-missing-tables.php 脚本用于补建 settlement_batches 和 system_jobs 表 - 为 settlement_batches 表添加 draw_id 和 settle_version 联合索引 - 在迁移文件中添加表存在性检查避免索引操作失败 - 补录相关迁移记录到 migrations 表中确保迁移状态一致 - 完善 schema 检查逻辑防止对不存在的表进行索引操作 --- ...lign_postgres_indexes_with_live_schema.php | 8 +- fix-missing-tables.php | 91 +++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 fix-missing-tables.php diff --git a/database/migrations/2026_06_10_120000_align_postgres_indexes_with_live_schema.php b/database/migrations/2026_06_10_120000_align_postgres_indexes_with_live_schema.php index 9e602cb..bc22f0a 100644 --- a/database/migrations/2026_06_10_120000_align_postgres_indexes_with_live_schema.php +++ b/database/migrations/2026_06_10_120000_align_postgres_indexes_with_live_schema.php @@ -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'); }); diff --git a/fix-missing-tables.php b/fix-missing-tables.php new file mode 100644 index 0000000..5f9e134 --- /dev/null +++ b/fix-missing-tables.php @@ -0,0 +1,91 @@ +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";