Passed
Push — master ( 563796...176d8c )
by Greg
05:23
created

Migration37::upgrade()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 56
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 41
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 56
rs 9.264

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) 2019 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\Builder;
24
use Illuminate\Database\Query\Expression;
25
use Illuminate\Database\Schema\Blueprint;
26
27
/**
28
 * Upgrade the database schema from version 37 to version 38.
29
 */
30
class Migration37 implements MigrationInterface
31
{
32
    /**
33
     * Upgrade to to the next version
34
     *
35
     * @return void
36
     */
37
    public function upgrade(): void
38
    {
39
        // These tables were created by webtrees 1.x, and may not exist if we first installed webtrees 2.x
40
        DB::schema()->dropIfExists('site_access_rule');
41
        DB::schema()->dropIfExists('next_id');
42
43
        // Split the media table into media/media_file so that we can store multiple media
44
        // files in each media object.
45
        DB::schema()->create('media_file', static function (Blueprint $table): void {
46
            $table->integer('id', true);
47
            $table->string('m_id', 20);
48
            $table->integer('m_file');
49
            $table->string('multimedia_file_refn', 248); // GEDCOM only allows 30 characters
50
            $table->string('multimedia_format', 4);
51
            $table->string('source_media_type', 15);
52
            $table->string('descriptive_title', 248);
53
54
            $table->index(['m_id', 'm_file']);
55
            $table->index(['m_file', 'm_id']);
56
            $table->index(['m_file', 'multimedia_file_refn']);
57
            $table->index(['m_file', 'multimedia_format']);
58
            $table->index(['m_file', 'source_media_type']);
59
            $table->index(['m_file', 'descriptive_title']);
60
        });
61
62
        if (DB::schema()->hasColumn('media', 'm_filename')) {
63
            (new Builder(DB::connection()))->from('media_file')->insertUsing([
64
                'm_id',
65
                'm_file',
66
                'multimedia_file_refn',
67
                'multimedia_format',
68
                'source_media_type',
69
                'descriptive_title',
70
            ], function (Builder $query): void {
71
                $query->select([
72
                    'm_id',
73
                    'm_file',
74
                    $this->substring('m_filename', 1, 248),
75
                    $this->substring('m_ext', 1, 4),
76
                    $this->substring('m_type', 1, 15),
77
                    $this->substring('m_titl', 1, 248),
78
                ])->from('media');
79
            });
80
81
            // SQLite can only drop one column at a time.
82
            DB::schema()->table('media', static function (Blueprint $table): void {
83
                $table->dropColumn('m_filename');
84
            });
85
            DB::schema()->table('media', static function (Blueprint $table): void {
86
                $table->dropColumn('m_ext');
87
            });
88
            DB::schema()->table('media', static function (Blueprint $table): void {
89
                $table->dropColumn('m_type');
90
            });
91
            DB::schema()->table('media', static function (Blueprint $table): void {
92
                $table->dropColumn('m_titl');
93
            });
94
        }
95
    }
96
97
    /**
98
     * @param string $expression
99
     * @param int    $start
100
     * @param int    $length
101
     *
102
     * @return Expression
103
     */
104
    private function substring(string $expression, int $start, int $length): Expression
105
    {
106
        // Non-standard
107
        if (DB::connection()->getDriverName() === 'sqlite') {
108
            return new Expression('SUBSTR(' . $expression . ',' . $start . ',' . $length . ')');
109
        }
110
111
        // SQL-92 standard
112
        return new Expression('SUBSTRING(' . $expression . ' FROM ' . $start . ' FOR ' . $length . ')');
113
    }
114
}
115