xref: /webtrees/app/Schema/Migration44.php (revision e5a5851c1ee2fef05c5230fce3ffdc366591b06f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Schema;
21
22use Fisharebest\Webtrees\DB;
23use Illuminate\Database\Query\Expression;
24use Illuminate\Database\Schema\Blueprint;
25use PDOException;
26
27/**
28 * Upgrade the database schema from version 44 to version 45.
29 */
30class Migration44 implements MigrationInterface
31{
32    public function upgrade(): void
33    {
34        // It is simpler to create a new table than to update the existing one.
35
36        if (!DB::schema()->hasTable('place_location')) {
37            DB::schema()->create('place_location', static function (Blueprint $table): void {
38                $table->integer('id', true);
39                $table->integer('parent_id')->nullable();
40                $table->string('place', 120);
41                $table->double('latitude')->nullable();
42                $table->double('longitude')->nullable();
43
44                $table->unique(['parent_id', 'place']);
45                $table->unique(['place', 'parent_id']);
46
47                $table->index(['latitude']);
48                $table->index(['longitude']);
49            });
50
51            // SQL-server cannot cascade-delete/update on self-relations.
52            // Users will need to delete all child locations before deleting the parent.
53            if (DB::driverName() === DB::SQL_SERVER) {
54                // SQL-Server doesn't support 'RESTRICT'
55                $action = 'NO ACTION';
56            } else {
57                $action = 'CASCADE';
58            }
59
60            DB::schema()->table('place_location', static function (Blueprint $table) use ($action): void {
61                $table->foreign(['parent_id'])
62                    ->references(['id'])
63                    ->on('place_location')
64                    ->onDelete($action)
65                    ->onUpdate($action);
66            });
67        }
68
69        // This table should only exist if we are upgrading an old installation, which would have been
70        // created with MySQL.  Therefore we can safely use MySQL-specific SQL.
71        if (DB::schema()->hasTable('placelocation')) {
72            if (DB::driverName() === DB::MYSQL) {
73                DB::table('placelocation')
74                    ->where('pl_lati', '=', '')
75                    ->orWhere('pl_long', '=', '')
76                    ->update([
77                        'pl_lati' => null,
78                        'pl_long' => null,
79                    ]);
80
81                // Missing/invalid parents?  Move them to the top level
82                DB::table('placelocation AS pl1')
83                    ->leftJoin('placelocation AS pl2', 'pl1.pl_parent_id', '=', 'pl2.pl_id')
84                    ->whereNull('pl2.pl_id')
85                    ->update([
86                        'pl1.pl_parent_id' => 0,
87                    ]);
88
89                // Remove invalid values.
90                DB::table('placelocation')
91                    ->where('pl_lati', 'NOT REGEXP', '^[NS][0-9]+[.]?[0-9]*$')
92                    ->orWhere('pl_long', 'NOT REGEXP', '^[EW][0-9]+[.]?[0-9]*$')
93                    ->update([
94                        'pl_lati' => null,
95                        'pl_long' => null,
96                    ]);
97
98                // The existing data may have placenames that only differ after the first 120 chars.
99                // Need to remove the constraint before we truncate/merge them.
100                try {
101                    DB::schema()->table('placelocation', static function (Blueprint $table): void {
102                        $table->dropUnique(['pl_parent_id', 'pl_place']);
103                    });
104                } catch (PDOException) {
105                    // Already deleted, or does not exist;
106                }
107
108                DB::table('placelocation')
109                    ->update([
110                        'pl_place' => new Expression('SUBSTRING(pl_place, 1, 120)'),
111                    ]);
112
113                // The lack of unique key constraints means that there may be duplicates...
114                while (true) {
115                    // Two places with the same name and parent...
116                    $row = DB::table('placelocation')
117                        ->select([
118                            new Expression('MIN(pl_id) AS min'),
119                            new Expression('MAX(pl_id) AS max'),
120                        ])
121                        ->groupBy(['pl_parent_id', 'pl_place'])
122                        ->having(new Expression('COUNT(*)'), '>', '1')
123                        ->first();
124
125                    if ($row === null) {
126                        break;
127                    }
128
129                    // ...move children to the first
130                    DB::table('placelocation')
131                        ->where('pl_parent_id', '=', $row->max)
132                        ->update(['pl_parent_id' => $row->min]);
133
134                    // ...delete the second
135                    DB::table('placelocation')
136                        ->where('pl_id', '=', $row->max)
137                        ->delete();
138                }
139
140                // This is the SQL standard.  It works with MySQL 8.0 and higher
141                $select1 = DB::table('placelocation')
142                    ->leftJoin('place_location', 'id', '=', 'pl_id')
143                    ->whereNull('id')
144                    ->orderBy('pl_level')
145                    ->orderBy('pl_id')
146                    ->select([
147                        'pl_id',
148                        new Expression('CASE pl_parent_id WHEN 0 THEN NULL ELSE pl_parent_id END'),
149                        'pl_place',
150                        new Expression("CAST(REPLACE(REPLACE(pl_lati, 'S', '-'), 'N', '') AS FLOAT)"),
151                        new Expression("CAST(REPLACE(REPLACE(pl_long, 'W', '-'), 'E', '') AS FLOAT)"),
152                    ]);
153
154                // This works for MySQL 5.7 and lower, which cannot cast to FLOAT
155                $select2 = DB::table('placelocation')
156                    ->leftJoin('place_location', 'id', '=', 'pl_id')
157                    ->whereNull('id')
158                    ->orderBy('pl_level')
159                    ->orderBy('pl_id')
160                    ->select([
161                        'pl_id',
162                        new Expression('CASE pl_parent_id WHEN 0 THEN NULL ELSE pl_parent_id END'),
163                        'pl_place',
164                        new Expression("REPLACE(REPLACE(pl_lati, 'S', '-'), 'N', '')"),
165                        new Expression("REPLACE(REPLACE(pl_long, 'W', '-'), 'E', '')"),
166                    ]);
167
168                try {
169                    DB::table('place_location')
170                        ->insertUsing(['id', 'parent_id', 'place', 'latitude', 'longitude'], $select1);
171                } catch (PDOException) {
172                    DB::table('place_location')
173                        ->insertUsing(['id', 'parent_id', 'place', 'latitude', 'longitude'], $select2);
174                }
175            }
176
177            DB::schema()->drop('placelocation');
178        }
179
180        // Earlier versions of webtrees used 0 and NULL interchangeably.
181        // Assume 0 at the country-level and NULL at lower levels.
182        DB::table('place_location')
183            ->whereNotNull('parent_id')
184            ->where('latitude', '=', 0)
185            ->where('longitude', '=', 0)
186            ->update([
187                'latitude'  => null,
188                'longitude' => null,
189            ]);
190    }
191}
192