Created
September 12, 2022 12:58
-
-
Save jamesmills/07182fd20fc03c90e79e815754984197 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Storage; | |
class GenerateDropForeignKeyMigration extends Command | |
{ | |
protected $signature = 'migration'; | |
protected $description = 'Command description'; | |
public function handle() | |
{ | |
$data = DB::select( | |
" | |
SELECT | |
TABLE_NAME, | |
COLUMN_NAME, | |
CONSTRAINT_NAME, | |
REFERENCED_TABLE_NAME, | |
REFERENCED_COLUMN_NAME | |
FROM | |
INFORMATION_SCHEMA.KEY_COLUMN_USAGE | |
WHERE | |
REFERENCED_TABLE_SCHEMA = ?", | |
[config('database.connections.mysql.database')] | |
); | |
$results = collect($data)->groupBy('TABLE_NAME'); | |
$text = ''; | |
foreach ($results as $table => $data) { | |
$text .= sprintf( | |
"Schema::table('%s', function (Blueprint \$table) { | |
", | |
$table | |
); | |
foreach ($data as $item) { | |
$text .= sprintf(" \$table->dropForeign('%s');\n", $item->CONSTRAINT_NAME); | |
} | |
$text .= "});\n"; | |
} | |
Storage::put('migration_stuff.txt', $text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This dumps something like the below in
storage/app/migration_stuff.txt