Files
resource/routes/console.php
T
2026-01-21 19:53:48 +08:00

44 lines
1.5 KiB
PHP
Executable File

<?php
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');
Artisan::command('db:reCreate {sTableName?}', function ($sTableName = null) {
// 1. 没传参数?直接亮红牌!
if (empty($sTableName)) {
$this->error('错误:必须指定要重建的表名!我可不想乱开火。');
$this->info('用法示例:php artisan db:reCreate slave_sites');
return; // 直接撤退,不往下走
}
// 2. 确定目标
$sTable = $sTableName;
// 3. 检查表是否存在(不存在炸个寂寞?)
if (!Schema::hasTable($sTable)) {
$this->warn("目标表 [{$sTable}] 本就不存在,但我还是会清理迁移记录并尝试重建。");
}
// 4. 物理抹除表
Schema::dropIfExists($sTable);
// 5. 撕掉迁移记录
// 针对你的匈牙利命名或标准命名,用 snake 匹配最稳
$sMatch = Str::snake($sTable);
DB::table('migrations')->where('migration', 'like', "%{$sMatch}%")->delete();
$this->warn("已爆破表及记录: {$sTable}");
// 6. 重新集结
$this->comment('正在根据迁移文件重新创建表...');
Artisan::call('migrate');
$this->info("表 [{$sTable}] 重建成功!任务完成!");
})->purpose('定点爆破并重建指定的表,不带参数不动手');