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";