diff --git a/database/migrations/2023_09_23_162754_add-parent-to-polylines.php b/database/migrations/2023_09_23_162754_add-parent-to-polylines.php index c2a2ba1e9..43b74dcd7 100644 --- a/database/migrations/2023_09_23_162754_add-parent-to-polylines.php +++ b/database/migrations/2023_09_23_162754_add-parent-to-polylines.php @@ -3,20 +3,35 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; +use Symfony\Component\Console\Output\ConsoleOutput; return new class extends Migration { public function up(): void { - Schema::table('poly_lines', function(Blueprint $table) { - $table->unsignedBigInteger('parent_id')->nullable()->after('id'); - $table->foreign('parent_id')->references('id')->on('poly_lines'); - }); + if (!Schema::hasColumn('poly_lines', 'parent_id')) { + Schema::table('poly_lines', function (Blueprint $table) { + $table->unsignedBigInteger('parent_id')->nullable()->after('id'); + $table->foreign('parent_id')->references('id')->on('poly_lines'); + }); + } else { + $output = new ConsoleOutput(); + $output->writeln("\nColumn already exists. Skipping."); + } } public function down(): void { - Schema::table('poly_lines', function(Blueprint $table) { - $table->dropForeign(['parent_id']); - $table->dropColumn('parent_id'); - }); + try { + Schema::table('poly_lines', function (Blueprint $table) { + $table->dropForeign(['parent_id']); + $table->dropColumn('parent_id'); + }); + } catch (\Throwable) { + $output = new ConsoleOutput(); + $output->writeln("\nForeign key didn't exist. Dropping only column."); + + Schema::table('poly_lines', function (Blueprint $table) { + $table->dropColumn('parent_id'); + }); + } } };