xref: /webtrees/app/Schema/Migration44.php (revision 7e8403a6ee08bc7f7d341a10554c3b4b429ad499)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://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;
25
26/**
27 * Upgrade the database schema from version 44 to version 45.
28 */
29class Migration44 implements MigrationInterface
30{
31    /**
32     * Upgrade to to the next version
33     *
34     * @return void
35     */
36    public function upgrade(): void
37    {
38        if (!DB::schema()->hasColumn('placelocation', 'pl_latitude')) {
39            DB::schema()->table('placelocation', static function (Blueprint $table): void {
40                $table->float('pl_latitude')->nullable()->after('pl_lati');
41                $table->float('pl_longitude')->nullable()->after('pl_long');
42            });
43        }
44
45        DB::table('placelocation')
46            ->where('pl_lati', 'LIKE', 'N%')
47            ->update(['pl_latitude' => new Expression('CAST(SUBSTR(pl_lati, 2) AS FLOAT)')]);
48
49        DB::table('placelocation')
50            ->where('pl_lati', 'LIKE', 'S%')
51            ->update(['pl_latitude' => new Expression('- CAST(SUBSTR(pl_lati, 2) AS FLOAT)')]);
52
53        DB::table('placelocation')
54            ->where('pl_long', 'LIKE', 'E%')
55            ->update(['pl_longitude' => new Expression('CAST(SUBSTR(pl_long, 2) AS FLOAT)')]);
56
57        DB::table('placelocation')
58            ->where('pl_long', 'LIKE', 'W%')
59            ->update(['pl_longitude' => new Expression('- CAST(SUBSTR(pl_long, 2) AS FLOAT)')]);
60
61        DB::schema()->table('placelocation', static function (Blueprint $table): void {
62            $table->dropColumn('pl_lati');
63            $table->dropColumn('pl_long');
64            $table->dropColumn('pl_zoom');
65            $table->dropColumn('pl_icon');
66            $table->dropColumn('pl_level');
67        });
68    }
69}
70