Skip to content

Commit

Permalink
🐛 Migration fails on large datasets (#1979)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrLevin authored Oct 13, 2023
1 parent c9d4d1c commit da9f42d
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions database/migrations/2023_09_23_162754_add-parent-to-polylines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
}
};

0 comments on commit da9f42d

Please sign in to comment.