Completed
Pull Request — master (#3652)
by
unknown
06:05
created

Migration45::upgrade()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 2
eloc 22
c 1
b 1
f 1
nc 2
nop 0
dl 0
loc 31
rs 9.568
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Schema;
21
22
use Illuminate\Database\Capsule\Manager as DB;
23
use Illuminate\Database\Query\Expression;
24
use Illuminate\Database\Schema\Blueprint;
25
26
/**
27
 * Upgrade the database schema from version 45 to version 46.
28
 */
29
class Migration45 implements MigrationInterface
30
{
31
    /**
32
     * Upgrade 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