xref: /webtrees/app/Schema/Migration37.php (revision b6017f990d38d8c56e04c0096ce9a7e8745ad4ba)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Schema;
21
22use Illuminate\Database\Capsule\Manager as DB;
23use Illuminate\Database\Query\Builder;
24use Illuminate\Database\Query\Expression;
25use Illuminate\Database\Schema\Blueprint;
26
27/**
28 * Upgrade the database schema from version 37 to version 38.
29 */
30class 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        if (!DB::schema()->hasTable('media_file')) {
46            DB::schema()->create('media_file', static function (Blueprint $table): void {
47                $table->integer('id', true);
48                $table->string('m_id', 20);
49                $table->integer('m_file');
50                $table->string('multimedia_file_refn', 248); // GEDCOM only allows 30 characters
51                $table->string('multimedia_format', 4);
52                $table->string('source_media_type', 15);
53                $table->string('descriptive_title', 248);
54
55                $table->index(['m_id', 'm_file']);
56                $table->index(['m_file', 'm_id']);
57                $table->index(['m_file', 'multimedia_file_refn']);
58                $table->index(['m_file', 'multimedia_format']);
59                $table->index(['m_file', 'source_media_type']);
60                $table->index(['m_file', 'descriptive_title']);
61            });
62        }
63
64        if (DB::table('media_file')->count() === 0 && DB::schema()->hasColumn('media', 'm_filename')) {
65            (new Builder(DB::connection()))->from('media_file')->insertUsing([
66                'm_id',
67                'm_file',
68                'multimedia_file_refn',
69                'multimedia_format',
70                'source_media_type',
71                'descriptive_title',
72            ], function (Builder $query): void {
73                $query->select([
74                    'm_id',
75                    'm_file',
76                    $this->substring('m_filename', 1, 248),
77                    $this->substring('m_ext', 1, 4),
78                    $this->substring('m_type', 1, 15),
79                    $this->substring('m_titl', 1, 248),
80                ])->from('media');
81            });
82
83            // SQLite can only drop one column at a time.
84            DB::schema()->table('media', static function (Blueprint $table): void {
85                $table->dropColumn('m_filename');
86            });
87            DB::schema()->table('media', static function (Blueprint $table): void {
88                $table->dropColumn('m_ext');
89            });
90            DB::schema()->table('media', static function (Blueprint $table): void {
91                $table->dropColumn('m_type');
92            });
93            DB::schema()->table('media', static function (Blueprint $table): void {
94                $table->dropColumn('m_titl');
95            });
96        }
97    }
98
99    /**
100     * @param string $expression
101     * @param int    $start
102     * @param int    $length
103     *
104     * @return Expression
105     */
106    private function substring(string $expression, int $start, int $length): Expression
107    {
108        // Non-standard
109        if (DB::connection()->getDriverName() === 'sqlite') {
110            return new Expression('SUBSTR(' . $expression . ',' . $start . ',' . $length . ')');
111        }
112
113        // SQL-92 standard
114        return new Expression('SUBSTRING(' . $expression . ' FROM ' . $start . ' FOR ' . $length . ')');
115    }
116}
117