xref: /webtrees/app/Schema/Migration37.php (revision 9db3b8943e3966b447e01b7f009c5d279f21439d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 Fisharebest\Webtrees\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    public function upgrade(): void
33    {
34        // These tables were created by webtrees 1.x, and may not exist if we first installed webtrees 2.x
35        DB::schema()->dropIfExists('site_access_rule');
36        DB::schema()->dropIfExists('next_id');
37
38        // Split the media table into media/media_file so that we can store multiple media
39        // files in each media object.
40        if (!DB::schema()->hasTable('media_file')) {
41            DB::schema()->create('media_file', static function (Blueprint $table): void {
42                $table->integer('id', true);
43                $table->string('m_id', 20);
44                $table->integer('m_file');
45                $table->string('multimedia_file_refn', 248); // GEDCOM only allows 30 characters
46                $table->string('multimedia_format', 4);
47                $table->string('source_media_type', 15);
48                $table->string('descriptive_title', 248);
49
50                $table->index(['m_id', 'm_file']);
51                $table->index(['m_file', 'm_id']);
52                $table->index(['m_file', 'multimedia_file_refn']);
53                $table->index(['m_file', 'multimedia_format']);
54                $table->index(['m_file', 'source_media_type']);
55                $table->index(['m_file', 'descriptive_title']);
56            });
57        }
58
59        if (DB::table('media_file')->count() === 0 && DB::schema()->hasColumn('media', 'm_filename')) {
60            (new Builder(DB::connection()))->from('media_file')->insertUsing([
61                'm_id',
62                'm_file',
63                'multimedia_file_refn',
64                'multimedia_format',
65                'source_media_type',
66                'descriptive_title',
67            ], function (Builder $query): void {
68                // SQLite also supports SUBSTRING() from 3.34.0 (2020-12-01)
69                $substring_function = DB::driverName() === DB::SQLITE ? 'SUBSTR' : 'SUBSTRING';
70
71                $query->select([
72                    'm_id',
73                    'm_file',
74                    new Expression($substring_function . '(m_filename, 1, 248)'),
75                    new Expression($substring_function . '(m_ext, 1, 4)'),
76                    new Expression($substring_function . '(m_type, 1, 15)'),
77                    new Expression($substring_function . '(m_titl, 1, 248)'),
78                ])->from('media');
79            });
80
81            // The Laravel database library for 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