diff --git a/database/migrations/2026_04_03_034848_create_continents_table.php b/database/migrations/2026_04_03_034848_create_continents_table.php new file mode 100644 index 0000000..eb3514c --- /dev/null +++ b/database/migrations/2026_04_03_034848_create_continents_table.php @@ -0,0 +1,40 @@ +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',], + ]; + } +}; diff --git a/database/migrations/2026_04_03_035028_add_continent_to_countries.php b/database/migrations/2026_04_03_035028_add_continent_to_countries.php new file mode 100644 index 0000000..c939ec0 --- /dev/null +++ b/database/migrations/2026_04_03_035028_add_continent_to_countries.php @@ -0,0 +1,49 @@ +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'); + }); + } +}; diff --git a/database/migrations/2026_04_03_035607_add_continent_to_regions.php b/database/migrations/2026_04_03_035607_add_continent_to_regions.php new file mode 100644 index 0000000..c752bb5 --- /dev/null +++ b/database/migrations/2026_04_03_035607_add_continent_to_regions.php @@ -0,0 +1,51 @@ +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'); + }); + } +};