Modificación a la migración tags
This commit is contained in:
parent
b5779e5bdb
commit
df252f55f2
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class extends Migration
|
return new class extends Migration
|
||||||
@ -11,12 +12,44 @@
|
|||||||
*/
|
*/
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
|
// Obtener el nombre real de la FK si existe
|
||||||
|
$foreignKeys = DB::select("
|
||||||
|
SELECT CONSTRAINT_NAME
|
||||||
|
FROM information_schema.KEY_COLUMN_USAGE
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
AND TABLE_NAME = 'tags'
|
||||||
|
AND COLUMN_NAME = 'package_id'
|
||||||
|
AND REFERENCED_TABLE_NAME IS NOT NULL
|
||||||
|
");
|
||||||
|
|
||||||
|
// Eliminar FK solo si existe
|
||||||
|
if (!empty($foreignKeys)) {
|
||||||
|
$constraintName = $foreignKeys[0]->CONSTRAINT_NAME;
|
||||||
|
DB::statement("ALTER TABLE tags DROP FOREIGN KEY `{$constraintName}`");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Renombrar columna solo si existe package_id
|
||||||
|
if (Schema::hasColumn('tags', 'package_id')) {
|
||||||
Schema::table('tags', function (Blueprint $table) {
|
Schema::table('tags', function (Blueprint $table) {
|
||||||
$table->dropForeign(['package_id']);
|
|
||||||
$table->renameColumn('package_id', 'box_id');
|
$table->renameColumn('package_id', 'box_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$boxForeignKeys = DB::select("
|
||||||
|
SELECT CONSTRAINT_NAME
|
||||||
|
FROM information_schema.KEY_COLUMN_USAGE
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
AND TABLE_NAME = 'tags'
|
||||||
|
AND COLUMN_NAME = 'box_id'
|
||||||
|
AND REFERENCED_TABLE_NAME = 'boxes'
|
||||||
|
");
|
||||||
|
|
||||||
|
if (empty($boxForeignKeys)) {
|
||||||
|
Schema::table('tags', function (Blueprint $table) {
|
||||||
$table->foreign('box_id')->references('id')->on('boxes')->onDelete('cascade');
|
$table->foreign('box_id')->references('id')->on('boxes')->onDelete('cascade');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverse the migrations.
|
* Reverse the migrations.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user