Skip to content

Commit f0af55f

Browse files
committed
Restore original migrations to v1 schema and consolidate v2 changes in upgrade migration
Changelog: fixed
1 parent 3d2fd54 commit f0af55f

5 files changed

Lines changed: 37 additions & 80 deletions

database/migrations/0000_03_07_190506_create_countries_table.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public function up(): void
4040

4141
$table->string('tld', 8);
4242
$table->string('native', 80)->nullable();
43-
$table->string('region_name', 80);
43+
$table->string('region', 80);
4444

4545
if (config()->boolean('atlas.entities.regions')) {
46-
$table->foreignId('region_id')->nullable()->constrained(config()->string('atlas.regions_tablename'))->nullOnDelete();
46+
$table->foreignId('region_id')->constrained(config()->string('atlas.regions_tablename'))->nullOnDelete();
4747
}
48-
$table->string('subregion_name', 80)->nullable();
48+
$table->string('subregion', 80)->nullable();
4949

5050
if (config()->boolean('atlas.entities.subregions')) {
5151
$table->foreignId('subregion_id')->nullable()->constrained(config()->string('atlas.subregions_tablename'))->nullOnDelete();
@@ -56,9 +56,6 @@ public function up(): void
5656
$table->string('longitude', 15);
5757
$table->string('emoji', 40);
5858
$table->string('emojiU', 40);
59-
60-
$table->index('iso2');
61-
$table->index('iso3');
6259
});
6360
}
6461

database/migrations/0000_03_07_190507_create_states_table.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public function up(): void
3737
$table->string('type')->nullable();
3838
$table->string('latitude')->nullable();
3939
$table->string('longitude')->nullable();
40-
41-
$table->index('state_code');
4240
});
4341
}
4442

database/migrations/0000_03_07_190508_create_cities_table.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public function up(): void
4444
$table->string('latitude');
4545
$table->string('longitude');
4646
$table->string('wiki_data_id')->nullable();
47-
48-
$table->index('name');
4947
});
5048
}
5149

database/migrations/0000_03_07_190510_create_countries_timezones_table.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public function up(): void
2929

3030
$table->string('timezone_name');
3131
$table->foreign('timezone_name')->references('zone_name')->on(config()->string('atlas.timezones_tablename'));
32-
33-
$table->unique(['country_id', 'timezone_name']);
3432
});
3533
}
3634

database/migrations/0000_03_07_190511_upgrade_atlas_schema.php

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
{
1212
/**
1313
* Run the migrations.
14+
*
15+
* Applies all v2 schema changes. Every operation is guarded for
16+
* idempotency so this migration is safe on both fresh installs
17+
* (where the tables were just created by the original migrations)
18+
* and upgrades from v1.x.
1419
*/
1520
public function up(): void
1621
{
17-
$this->renamePivotColumn();
1822
$this->renameCountryStringColumns();
1923
$this->fixRegionIdNullability();
2024
$this->addMissingIndexes();
@@ -28,36 +32,8 @@ public function down(): void
2832
// Not reversible: the original CREATE TABLE migrations handle table drops.
2933
}
3034

31-
/**
32-
* Rename time_zone_name → timezone_name in the country-timezone pivot table.
33-
*
34-
* Only applies to databases created before the column was renamed in the
35-
* original migration file (commit 0258d50).
36-
*/
37-
private function renamePivotColumn(): void
38-
{
39-
$pivotTable = config()->string('atlas.country_timezone_pivot_tablename');
40-
41-
if (! Schema::hasTable($pivotTable) || ! Schema::hasColumn($pivotTable, 'time_zone_name')) {
42-
return;
43-
}
44-
45-
$timezonesTable = config()->string('atlas.timezones_tablename');
46-
47-
Schema::table($pivotTable, function (Blueprint $table): void {
48-
$table->dropForeign(['time_zone_name']);
49-
$table->renameColumn('time_zone_name', 'timezone_name');
50-
});
51-
52-
Schema::table($pivotTable, function (Blueprint $table) use ($timezonesTable): void {
53-
$table->foreign('timezone_name')->references('zone_name')->on($timezonesTable);
54-
});
55-
}
56-
5735
/**
5836
* Rename region → region_name and subregion → subregion_name on the countries table.
59-
*
60-
* Only applies to databases created before the columns were renamed.
6137
*/
6238
private function renameCountryStringColumns(): void
6339
{
@@ -82,8 +58,6 @@ private function renameCountryStringColumns(): void
8258

8359
/**
8460
* Make region_id nullable so that nullOnDelete() can work correctly.
85-
*
86-
* Only applies to databases where region_id was created as NOT NULL.
8761
*/
8862
private function fixRegionIdNullability(): void
8963
{
@@ -99,43 +73,7 @@ private function fixRegionIdNullability(): void
9973
}
10074

10175
/**
102-
* Remove duplicate rows from the pivot table so the unique index can be added safely.
103-
*
104-
* Databases upgraded from v1.x may contain duplicates because the old
105-
* TimezonesSeeder had no DB-level uniqueness guarantee. Uses a
106-
* database-agnostic approach (temp table + reinsert) since the pivot
107-
* has no primary key column.
108-
*/
109-
private function deduplicatePivotRows(string $pivotTable): void
110-
{
111-
$duplicates = DB::table($pivotTable)
112-
->select('country_id', 'timezone_name')
113-
->groupBy('country_id', 'timezone_name')
114-
->havingRaw('COUNT(*) > 1')
115-
->get();
116-
117-
if ($duplicates->isEmpty()) {
118-
return;
119-
}
120-
121-
// For each duplicate group, delete all rows then re-insert one.
122-
foreach ($duplicates as $row) {
123-
DB::table($pivotTable)
124-
->where('country_id', $row->country_id)
125-
->where('timezone_name', $row->timezone_name)
126-
->delete();
127-
128-
DB::table($pivotTable)->insert([
129-
'country_id' => $row->country_id,
130-
'timezone_name' => $row->timezone_name,
131-
]);
132-
}
133-
}
134-
135-
/**
136-
* Add indexes that were added to CREATE TABLE migrations after initial release.
137-
*
138-
* Only applies to databases created before the indexes were added (commit 7716529).
76+
* Add indexes introduced in v2.
13977
*/
14078
private function addMissingIndexes(): void
14179
{
@@ -181,4 +119,32 @@ private function addMissingIndexes(): void
181119
});
182120
}
183121
}
122+
123+
/**
124+
* Remove duplicate rows from the pivot table so the unique index can be added safely.
125+
*/
126+
private function deduplicatePivotRows(string $pivotTable): void
127+
{
128+
$duplicates = DB::table($pivotTable)
129+
->select('country_id', 'timezone_name')
130+
->groupBy('country_id', 'timezone_name')
131+
->havingRaw('COUNT(*) > 1')
132+
->get();
133+
134+
if ($duplicates->isEmpty()) {
135+
return;
136+
}
137+
138+
foreach ($duplicates as $row) {
139+
DB::table($pivotTable)
140+
->where('country_id', $row->country_id)
141+
->where('timezone_name', $row->timezone_name)
142+
->delete();
143+
144+
DB::table($pivotTable)->insert([
145+
'country_id' => $row->country_id,
146+
'timezone_name' => $row->timezone_name,
147+
]);
148+
}
149+
}
184150
};

0 commit comments

Comments
 (0)