xref: /webtrees/app/Schema/Migration44.php (revision 6ed487d2b01e11ce08b48920f9285a6accd57cda)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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 Illuminate\Database\Capsule\Manager as 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    /**
33     * Upgrade to the next version
34     *
35     * @return void
36     */
37    public function upgrade(): void
38    {
39        // It is simpler to create a new table than to update the existing one.
40
41        if (!DB::schema()->hasTable('place_location')) {
42            DB::schema()->create('place_location', static function (Blueprint $table): void {
43                $table->integer('id', true);
44                $table->integer('parent_id')->nullable();
45                $table->string('place', 120);
46                $table->double('latitude')->nullable();
47                $table->double('longitude')->nullable();
48
49                $table->unique(['parent_id', 'place']);
50                $table->unique(['place', 'parent_id']);
51
52                $table->index(['latitude']);
53                $table->index(['longitude']);
54            });
55
56            // SQL-server cannot cascade-delete/update on self-relations.
57            // Users will need to delete all child locations before deleting the parent.
58            if (DB::connection()->getDriverName() === 'sqlsrv') {
59                $action = 'RESTRICT';
60            } else {
61                $action = 'CASCADE';
62            }
63
64            DB::schema()->table('place_location', static function (Blueprint $table) use ($action): void {
65                $table->foreign(['parent_id'])
66                    ->references(['id'])
67                    ->on('place_location')
68                    ->onDelete($action)
69                    ->onUpdate($action);
70            });
71        }
72
73        if (DB::schema()->hasTable('placelocation')) {
74            DB::table('placelocation')
75                ->where('pl_lati', '=', '')
76                ->orWhere('pl_long', '=', '')
77                ->update([
78                    'pl_lati' => null,
79                    'pl_long' => null,
80                ]);
81
82            // Missing/invalid parents?  Move them to the top level
83            DB::table('placelocation AS pl1')
84                ->leftJoin('placelocation AS pl2', 'pl1.pl_parent_id', '=', 'pl2.pl_id')
85                ->whereNull('pl2.pl_id')
86                ->update([
87                    'pl1.pl_parent_id' => 0,
88                ]);
89
90            // Remove invalid values.
91            if (DB::connection()->getDriverName() === 'mysql') {
92                DB::table('placelocation')
93                    ->where('pl_lati', 'NOT REGEXP', '^[NS][0-9]+[.]?[0-9]*$')
94                    ->orWhere('pl_long', 'NOT REGEXP', '^[EW][0-9]+[.]?[0-9]*$')
95                    ->update([
96                        'pl_lati' => null,
97                        'pl_long' => null,
98                    ]);
99            }
100
101            // The existing data may have placenames that only differ after the first 120 chars.
102            // Need to remove the constraint before we truncate/merge them.
103            try {
104                DB::schema()->table('placelocation', static function (Blueprint $table): void {
105                    $table->dropUnique(['pl_parent_id', 'pl_place']);
106                });
107            } catch (PDOException $ex) {
108                // Already deleted, or does not exist;
109            }
110
111            $substring_function = DB::connection()->getDriverName() === 'sqlite' ? 'SUBSTR' : 'SUBSTRING';
112
113            DB::table('placelocation')
114                ->update([
115                    'pl_place' => new Expression($substring_function . '(pl_place, 1, 120)'),
116                ]);
117
118            // The lack of unique key constraints means that there may be duplicates...
119            while (true) {
120                // Two places with the same name and parent...
121                $row = DB::table('placelocation')
122                    ->select([
123                        new Expression('MIN(pl_id) AS min'),
124                        new Expression('MAX(pl_id) AS max'),
125                    ])
126                    ->groupBy(['pl_parent_id', 'pl_place'])
127                    ->having(new Expression('COUNT(*)'), '>', '1')
128                    ->first();
129
130                if ($row === null) {
131                    break;
132                }
133
134                // ...move children to the first
135                DB::table('placelocation')
136                    ->where('pl_parent_id', '=', $row->max)
137                    ->update(['pl_parent_id' => $row->min]);
138
139                // ...delete the second
140                DB::table('placelocation')
141                    ->where('pl_id', '=', $row->max)
142                    ->delete();
143            }
144
145            // This is the SQL standard.  It works with Postgres, Sqlite and MySQL 8
146            $select1 = DB::table('placelocation')
147                ->leftJoin('place_location', 'id', '=', 'pl_id')
148                ->whereNull('id')
149                ->orderBy('pl_level')
150                ->orderBy('pl_id')
151                ->select([
152                    'pl_id',
153                    new Expression('CASE pl_parent_id WHEN 0 THEN NULL ELSE pl_parent_id END'),
154                    'pl_place',
155                    new Expression("CAST(REPLACE(REPLACE(pl_lati, 'S', '-'), 'N', '') AS FLOAT)"),
156                    new Expression("CAST(REPLACE(REPLACE(pl_long, 'W', '-'), 'E', '') AS FLOAT)"),
157                ]);
158
159            // This works for MySQL 5.7 and lower, which cannot cast to FLOAT
160            $select2 = DB::table('placelocation')
161                ->leftJoin('place_location', 'id', '=', 'pl_id')
162                ->whereNull('id')
163                ->orderBy('pl_level')
164                ->orderBy('pl_id')
165                ->select([
166                    'pl_id',
167                    new Expression('CASE pl_parent_id WHEN 0 THEN NULL ELSE pl_parent_id END'),
168                    'pl_place',
169                    new Expression("REPLACE(REPLACE(pl_lati, 'S', '-'), 'N', '')"),
170                    new Expression("REPLACE(REPLACE(pl_long, 'W', '-'), 'E', '')"),
171                ]);
172
173            // SQL-server needs to be told to insert values into auto-generated columns.
174            if (DB::connection()->getDriverName() === 'sqlsrv') {
175                $prefix    = DB::connection()->getTablePrefix();
176                $statement = 'SET IDENTITY_INSERT [' . $prefix . 'place_location] ON';
177                DB::connection()->statement($statement);
178            }
179
180            try {
181                DB::table('place_location')
182                    ->insertUsing(['id', 'parent_id', 'place', 'latitude', 'longitude'], $select1);
183            } catch (PDOException $ex) {
184                DB::table('place_location')
185                    ->insertUsing(['id', 'parent_id', 'place', 'latitude', 'longitude'], $select2);
186            }
187
188            DB::schema()->drop('placelocation');
189        }
190
191        // Earlier versions of webtrees used 0 and NULL interchangeably.
192        // Assume 0 at the country-level and NULL at lower levels.
193        DB::table('place_location')
194            ->whereNotNull('parent_id')
195            ->where('latitude', '=', 0)
196            ->where('longitude', '=', 0)
197            ->update([
198                'latitude'  => null,
199                'longitude' => null,
200            ]);
201    }
202}
203