Passed
Push — master ( a376f8...815563 )
by Greg
05:45
created

Migration44::upgrade()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 91
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 4
eloc 63
c 4
b 0
f 0
nc 6
nop 0
dl 0
loc 91
rs 8.8072

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 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
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\Builder;
24
use Illuminate\Database\Query\Expression;
25
use Illuminate\Database\Schema\Blueprint;
26
use PDOException;
27
28
/**
29
 * Upgrade the database schema from version 44 to version 45.
30
 */
31
class Migration44 implements MigrationInterface
32
{
33
    /**
34
     * Upgrade to to the next version
35
     *
36
     * @return void
37
     */
38
    public function upgrade(): void
39
    {
40
        // It is simpler to create a new table than to update the existing one.
41
42
        if (!DB::schema()->hasTable('place_location')) {
43
            DB::schema()->create('place_location', static function (Blueprint $table): void {
44
                $table->integer('id', true);
45
                $table->integer('parent_id')->nullable();
46
                $table->string('place', 120);
47
                $table->double('latitude')->nullable();
48
                $table->double('longitude')->nullable();
49
50
                $table->unique(['parent_id', 'place']);
51
                $table->unique(['place', 'parent_id']);
52
53
                $table->index(['latitude']);
54
                $table->index(['longitude']);
55
            });
56
57
            DB::schema()->table('place_location', static function (Blueprint $table): void {
58
                $table->foreign(['parent_id'])
59
                    ->references(['id'])
60
                    ->on('place_location')
61
                    ->onDelete('CASCADE')
62
                    ->onUpdate('CASCADE');
63
            });
64
        }
65
66
        if (DB::schema()->hasTable('placelocation')) {
67
            DB::table('placelocation')
68
                ->where('pl_lati', '=', '')
69
                ->orWhere('pl_long', '=', '')
70
                ->update([
71
                    'pl_lati' => null,
72
                    'pl_long' => null,
73
                ]);
74
75
            // Missing/invalid parents?  Move them to the top level
76
            DB::table('placelocation AS pl1')
77
                ->leftJoin('placelocation AS pl2', 'pl1.pl_parent_id', '=', 'pl2.pl_id')
78
                ->whereNull('pl2.pl_id')
79
                ->update([
80
                    'pl1.pl_parent_id' => 0,
81
                ]);
82
83
            // This is the SQL standard.  It works with Postgres, Sqlite and MySQL 8
84
            $select1 = DB::table('placelocation')
85
                ->leftJoin('place_location', 'id', '=', 'pl_id')
86
                ->whereNull('id')
87
                ->orderBy('pl_id')
88
                ->select([
89
                    'pl_id',
90
                    new Expression('CASE pl_parent_id WHEN 0 THEN NULL ELSE pl_parent_id END'),
91
                    'pl_place',
92
                    new Expression("CAST(REPLACE(REPLACE(pl_lati, 'S', '-'), 'N', '') AS FLOAT)"),
93
                    new Expression("CAST(REPLACE(REPLACE(pl_long, 'W', '-'), 'E', '') AS FLOAT)"),
94
                ]);
95
96
            // This works for MySQL 5.7 and lower, which cannot cast to FLOAT
97
            $select2 = DB::table('placelocation')
98
                ->leftJoin('place_location', 'id', '=', 'pl_id')
99
                ->whereNull('id')
100
                ->orderBy('pl_id')
101
                ->select([
102
                    'pl_id',
103
                    new Expression('CASE pl_parent_id WHEN 0 THEN NULL ELSE pl_parent_id END'),
104
                    'pl_place',
105
                    new Expression("REPLACE(REPLACE(pl_lati, 'S', '-'), 'N', '')"),
106
                    new Expression("REPLACE(REPLACE(pl_long, 'W', '-'), 'E', '')"),
107
                ]);
108
109
            try {
110
                DB::table('place_location')
111
                    ->insertUsing(['id', 'parent_id', 'place', 'latitude', 'longitude'], $select1);
112
            } catch (PDOException $ex) {
113
                DB::table('place_location')
114
                    ->insertUsing(['id', 'parent_id', 'place', 'latitude', 'longitude'], $select2);
115
            }
116
117
            DB::schema()->drop('placelocation');
118
        }
119
120
        // Earlier versions of webtrees used 0 and NULL interchangeably.
121
        // Assume 0 at the country-level and NULL at lower levels.
122
        DB::table('place_location')
123
            ->whereNotNull('parent_id')
124
            ->where('latitude', '=', 0)
125
            ->where('longitude', '=', 0)
126
            ->update([
127
                'latitude'  => null,
128
                'longitude' => null,
129
            ]);
130
    }
131
}
132