19a9dfcf7SGreg Roach<?php 29a9dfcf7SGreg Roach 39a9dfcf7SGreg Roach/** 49a9dfcf7SGreg Roach * webtrees: online genealogy 590949315SGreg Roach * Copyright (C) 2021 webtrees development team 69a9dfcf7SGreg Roach * This program is free software: you can redistribute it and/or modify 79a9dfcf7SGreg Roach * it under the terms of the GNU General Public License as published by 89a9dfcf7SGreg Roach * the Free Software Foundation, either version 3 of the License, or 99a9dfcf7SGreg Roach * (at your option) any later version. 109a9dfcf7SGreg Roach * This program is distributed in the hope that it will be useful, 119a9dfcf7SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 129a9dfcf7SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 139a9dfcf7SGreg Roach * GNU General Public License for more details. 149a9dfcf7SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 169a9dfcf7SGreg Roach */ 179a9dfcf7SGreg Roach 189a9dfcf7SGreg Roachdeclare(strict_types=1); 199a9dfcf7SGreg Roach 209a9dfcf7SGreg Roachnamespace Fisharebest\Webtrees\Schema; 219a9dfcf7SGreg Roach 229a9dfcf7SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2390949315SGreg Roachuse Illuminate\Database\Query\Builder; 249a9dfcf7SGreg Roachuse Illuminate\Database\Query\Expression; 259a9dfcf7SGreg Roachuse Illuminate\Database\Schema\Blueprint; 269a9dfcf7SGreg Roach 279a9dfcf7SGreg Roach/** 289a9dfcf7SGreg Roach * Upgrade the database schema from version 44 to version 45. 299a9dfcf7SGreg Roach */ 309a9dfcf7SGreg Roachclass Migration44 implements MigrationInterface 319a9dfcf7SGreg Roach{ 329a9dfcf7SGreg Roach /** 339a9dfcf7SGreg Roach * Upgrade to to the next version 349a9dfcf7SGreg Roach * 359a9dfcf7SGreg Roach * @return void 369a9dfcf7SGreg Roach */ 379a9dfcf7SGreg Roach public function upgrade(): void 389a9dfcf7SGreg Roach { 3990949315SGreg Roach // It is simpler to create a new table than to update the existing one. 4090949315SGreg Roach 4190949315SGreg Roach if (!DB::schema()->hasTable('place_location')) { 4290949315SGreg Roach DB::schema()->create('place_location', static function (Blueprint $table): void { 4390949315SGreg Roach $table->integer('id', true); 4490949315SGreg Roach $table->integer('parent_id')->nullable(); 4590949315SGreg Roach $table->string('place', 120); 4690949315SGreg Roach $table->double('latitude')->nullable(); 4790949315SGreg Roach $table->double('longitude')->nullable(); 4890949315SGreg Roach 4990949315SGreg Roach $table->unique(['parent_id', 'place']); 5090949315SGreg Roach $table->unique(['place', 'parent_id']); 5190949315SGreg Roach 5290949315SGreg Roach $table->index(['latitude']); 5390949315SGreg Roach $table->index(['longitude']); 5490949315SGreg Roach }); 5590949315SGreg Roach 5690949315SGreg Roach DB::schema()->table('place_location', static function (Blueprint $table): void { 5790949315SGreg Roach $table->foreign(['parent_id']) 5890949315SGreg Roach ->references(['id']) 5990949315SGreg Roach ->on('place_location') 6090949315SGreg Roach ->onDelete('CASCADE') 6190949315SGreg Roach ->onUpdate('CASCADE'); 629a9dfcf7SGreg Roach }); 639a9dfcf7SGreg Roach } 649a9dfcf7SGreg Roach 6590949315SGreg Roach if (DB::schema()->hasTable('placelocation')) { 669a9dfcf7SGreg Roach DB::table('placelocation') 6790949315SGreg Roach ->where('pl_lati', '=', '') 6890949315SGreg Roach ->orWhere('pl_long', '=', '') 6990949315SGreg Roach ->update([ 7090949315SGreg Roach 'pl_lati' => null, 7190949315SGreg Roach 'pl_long' => null, 7290949315SGreg Roach ]); 739a9dfcf7SGreg Roach 7429395507SGreg Roach // Missing/invalid parents? Move them to the top level 7529395507SGreg Roach DB::table('placelocation AS pl1') 7629395507SGreg Roach ->leftJoin('placelocation AS pl2', 'pl1.pl_parent_id', '=', 'pl2.pl_id') 7729395507SGreg Roach ->whereNull('pl2.pl_id') 7829395507SGreg Roach ->update([ 7929395507SGreg Roach 'pl1.pl_parent_id' => 0, 8029395507SGreg Roach ]); 8129395507SGreg Roach 8290949315SGreg Roach $select = DB::table('placelocation') 8390949315SGreg Roach ->leftJoin('place_location', 'id', '=', 'pl_id') 8490949315SGreg Roach ->whereNull('id') 8590949315SGreg Roach ->orderBy('pl_id') 8690949315SGreg Roach ->select([ 8790949315SGreg Roach 'pl_id', 8890949315SGreg Roach new Expression('CASE pl_parent_id WHEN 0 THEN NULL ELSE pl_parent_id END'), 8990949315SGreg Roach 'pl_place', 90*cec4277eSGreg Roach new Expression("CAST(REPLACE(REPLACE(pl_lati, 'S', '-'), 'N', '') AS FLOAT)"), 91*cec4277eSGreg Roach new Expression("CAST(REPLACE(REPLACE(pl_long, 'W', '-'), 'E', '') AS FLOAT)"), 9290949315SGreg Roach ]); 939a9dfcf7SGreg Roach 9490949315SGreg Roach DB::table('place_location') 9590949315SGreg Roach ->insertUsing(['id', 'parent_id', 'place', 'latitude', 'longitude'], $select); 969a9dfcf7SGreg Roach 9790949315SGreg Roach DB::schema()->drop('placelocation'); 9890949315SGreg Roach } 9990949315SGreg Roach 10090949315SGreg Roach // Earlier versions of webtrees used 0 and NULL interchangeably. 10190949315SGreg Roach // Assume 0 at the country-level and NULL at lower levels. 10290949315SGreg Roach DB::table('place_location') 10390949315SGreg Roach ->whereNotNull('parent_id') 10490949315SGreg Roach ->where('latitude', '=', 0) 10590949315SGreg Roach ->where('longitude', '=', 0) 10690949315SGreg Roach ->update([ 10790949315SGreg Roach 'latitude' => null, 10890949315SGreg Roach 'longitude' => null, 10990949315SGreg Roach ]); 1109a9dfcf7SGreg Roach } 1119a9dfcf7SGreg Roach} 112