Add countries and regions
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('continents', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('code', 2)->unique();
|
||||
$table->string('name');
|
||||
$table->string('internal_name');
|
||||
});
|
||||
|
||||
DB::table('continents')->insert($this->continents());
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('continents');
|
||||
}
|
||||
|
||||
private function continents(): array
|
||||
{
|
||||
|
||||
return [
|
||||
['code' => 'AF', 'name' => 'Africa', 'internal_name' => 'africa', ],
|
||||
['code' => 'AN', 'name' => 'Antarctica', 'internal_name' => 'antarctica', ],
|
||||
['code' => 'AS', 'name' => 'Asia', 'internal_name' => 'asia', ],
|
||||
['code' => 'EU', 'name' => 'Europe', 'internal_name' => 'europe', ],
|
||||
['code' => 'NA', 'name' => 'North America', 'internal_name' => 'north_america', ],
|
||||
['code' => 'OC', 'name' => 'Oceania', 'internal_name' => 'oceania', ],
|
||||
['code' => 'SA', 'name' => 'South America', 'internal_name' => 'south_america',],
|
||||
];
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('countries', function (Blueprint $table) {
|
||||
$table->foreignId('continent_id')->nullable()->constrained('continents');
|
||||
});
|
||||
|
||||
// Resolve continent codes already in the table
|
||||
$continentMap = DB::table('continents')->pluck('id', 'code')->all();
|
||||
|
||||
foreach ($continentMap as $code => $id) {
|
||||
DB::table('countries')
|
||||
->where('continent', $code)
|
||||
->update(['continent_id' => $id]);
|
||||
}
|
||||
|
||||
Schema::table('countries', function (Blueprint $table) {
|
||||
$table->foreignId('continent_id')->nullable(false)->change();
|
||||
$table->dropColumn('continent');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('countries', function (Blueprint $table) {
|
||||
$table->string('continent', 2);
|
||||
});
|
||||
|
||||
$continentMap = DB::table('continents')->pluck('code', 'id')->all();
|
||||
|
||||
foreach ($continentMap as $id => $code) {
|
||||
DB::table('countries')
|
||||
->where('continent_id', $id)
|
||||
->update(['continent' => $code]);
|
||||
}
|
||||
|
||||
Schema::table('countries', function (Blueprint $table) {
|
||||
$table->dropConstrainedForeignId('continent_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
// Add the FK column first, nullable so we can populate it before constraining
|
||||
Schema::table('regions', function (Blueprint $table) {
|
||||
$table->foreignId('continent_id')->nullable()->constrained('continents');
|
||||
});
|
||||
|
||||
// Resolve continent codes already in the table
|
||||
$continentMap = DB::table('continents')->pluck('id', 'code')->all();
|
||||
|
||||
foreach ($continentMap as $code => $id) {
|
||||
DB::table('regions')
|
||||
->where('continent', $code)
|
||||
->update(['continent_id' => $id]);
|
||||
}
|
||||
|
||||
// Now make it non-nullable and drop the old string column
|
||||
Schema::table('regions', function (Blueprint $table) {
|
||||
$table->foreignId('continent_id')->nullable(false)->change();
|
||||
$table->dropColumn('continent');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('regions', function (Blueprint $table) {
|
||||
$table->string('continent', 2);
|
||||
});
|
||||
|
||||
$continentMap = DB::table('continents')->pluck('code', 'id')->all();
|
||||
|
||||
foreach ($continentMap as $id => $code) {
|
||||
DB::table('regions')
|
||||
->where('continent_id', $id)
|
||||
->update(['continent' => $code]);
|
||||
}
|
||||
|
||||
Schema::table('regions', function (Blueprint $table) {
|
||||
$table->dropConstrainedForeignId('continent_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user