Split port_groups migration to prevent issues (#12732)

* Split port_groups migration to prevent issues

* split out to individual tables

* fix class names

* Fix the hasTable check
don't need separate foreign key migrations since they are both on the same table.
This commit is contained in:
Tony Murray
2021-04-12 02:39:46 +02:00
committed by GitHub
parent ba23ee3999
commit dec1ed1e45
3 changed files with 81 additions and 8 deletions
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePortGroupPortTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (! Schema::hasTable('port_group_port')) {
Schema::create('port_group_port', function (Blueprint $table) {
$table->unsignedInteger('port_group_id')->unsigned()->index();
$table->unsignedInteger('port_id')->unsigned()->index();
$table->primary(['port_group_id', 'port_id']);
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('port_group_port');
}
}
@@ -17,13 +17,6 @@ class CreatePortGroupsTable extends Migration
$table->string('name')->unique();
$table->string('desc')->nullable();
});
Schema::create('port_group_port', function (Blueprint $table) {
$table->unsignedInteger('port_group_id')->unsigned()->index();
$table->unsignedInteger('port_id')->unsigned()->index();
$table->primary(['port_group_id', 'port_id']);
$table->foreign('port_group_id')->references('id')->on('port_groups')->onDelete('CASCADE');
$table->foreign('port_id')->references('port_id')->on('ports')->onDelete('CASCADE');
});
}
/**
@@ -33,7 +26,6 @@ class CreatePortGroupsTable extends Migration
*/
public function down()
{
Schema::drop('port_group_port');
Schema::drop('port_groups');
}
}
@@ -0,0 +1,48 @@
<?php
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddForeignKeysToPortGroupPortTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('port_group_port', function (Blueprint $table) {
// check existing foreign key constraints because initially was in one migration
$constraint_names = array_map(function (ForeignKeyConstraint $constraint) {
return $constraint->getName();
}, Schema::getConnection()->getDoctrineSchemaManager()
->listTableForeignKeys('port_group_port'));
if (! in_array('port_group_port_port_group_id_foreign', $constraint_names)) {
$table->foreign('port_group_id')->references('id')->on('port_groups')->onDelete('CASCADE');
}
if (! in_array('port_group_port_port_id_foreign', $constraint_names)) {
$table->foreign('port_id')->references('port_id')->on('ports')->onDelete('CASCADE');
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (\LibreNMS\DB\Eloquent::getDriver() !== 'sqlite') {
Schema::table('port_group_port', function (Blueprint $table) {
$table->dropForeign('port_group_port_port_group_id_foreign');
$table->dropForeign('port_group_port_port_id_foreign');
});
}
}
}