xref: /webtrees/app/Schema/Migration37.php (revision 52550490b7095dd69811f3ec21ed5a3ca1a8968d)
1d83c32e4SGreg Roach<?php
23976b470SGreg Roach
3d83c32e4SGreg Roach/**
4d83c32e4SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6d83c32e4SGreg Roach * This program is free software: you can redistribute it and/or modify
7d83c32e4SGreg Roach * it under the terms of the GNU General Public License as published by
8d83c32e4SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d83c32e4SGreg Roach * (at your option) any later version.
10d83c32e4SGreg Roach * This program is distributed in the hope that it will be useful,
11d83c32e4SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d83c32e4SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d83c32e4SGreg Roach * GNU General Public License for more details.
14d83c32e4SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16d83c32e4SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20d83c32e4SGreg Roachnamespace Fisharebest\Webtrees\Schema;
21d83c32e4SGreg Roach
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
2316d6367aSGreg Roachuse Illuminate\Database\Query\Builder;
241581e36eSGreg Roachuse Illuminate\Database\Query\Expression;
25c9beb871SGreg Roachuse Illuminate\Database\Schema\Blueprint;
26d83c32e4SGreg Roach
27d83c32e4SGreg Roach/**
28d83c32e4SGreg Roach * Upgrade the database schema from version 37 to version 38.
29d83c32e4SGreg Roach */
30c1010edaSGreg Roachclass Migration37 implements MigrationInterface
31c1010edaSGreg Roach{
32362be83aSGreg Roach    public function upgrade(): void
33c1010edaSGreg Roach    {
34c9beb871SGreg Roach        // These tables were created by webtrees 1.x, and may not exist if we first installed webtrees 2.x
35c9beb871SGreg Roach        DB::schema()->dropIfExists('site_access_rule');
36c9beb871SGreg Roach        DB::schema()->dropIfExists('next_id');
37d83c32e4SGreg Roach
38c9beb871SGreg Roach        // Split the media table into media/media_file so that we can store multiple media
39c9beb871SGreg Roach        // files in each media object.
403bd06429SGreg Roach        if (!DB::schema()->hasTable('media_file')) {
410b5fd0a6SGreg Roach            DB::schema()->create('media_file', static function (Blueprint $table): void {
42c9beb871SGreg Roach                $table->integer('id', true);
43c9beb871SGreg Roach                $table->string('m_id', 20);
44c9beb871SGreg Roach                $table->integer('m_file');
45bb308685SGreg Roach                $table->string('multimedia_file_refn', 248); // GEDCOM only allows 30 characters
46c9beb871SGreg Roach                $table->string('multimedia_format', 4);
47c9beb871SGreg Roach                $table->string('source_media_type', 15);
48c9beb871SGreg Roach                $table->string('descriptive_title', 248);
49d83c32e4SGreg Roach
50c9beb871SGreg Roach                $table->index(['m_id', 'm_file']);
51c9beb871SGreg Roach                $table->index(['m_file', 'm_id']);
52c9beb871SGreg Roach                $table->index(['m_file', 'multimedia_file_refn']);
53c9beb871SGreg Roach                $table->index(['m_file', 'multimedia_format']);
54c9beb871SGreg Roach                $table->index(['m_file', 'source_media_type']);
55c9beb871SGreg Roach                $table->index(['m_file', 'descriptive_title']);
56c9beb871SGreg Roach            });
573bd06429SGreg Roach        }
588f5f5da8SGreg Roach
593bd06429SGreg Roach        if (DB::table('media_file')->count() === 0 && DB::schema()->hasColumn('media', 'm_filename')) {
608e618764SGreg Roach            (new Builder(DB::connection()))->from('media_file')->insertUsing([
61c9beb871SGreg Roach                'm_id',
62c9beb871SGreg Roach                'm_file',
63c9beb871SGreg Roach                'multimedia_file_refn',
64c9beb871SGreg Roach                'multimedia_format',
65c9beb871SGreg Roach                'source_media_type',
66c9beb871SGreg Roach                'descriptive_title',
671581e36eSGreg Roach            ], function (Builder $query): void {
6810fc856aSGreg Roach                // SQLite also supports SUBSTRING() from 3.34.0 (2020-12-01)
69*52550490SGreg Roach                $substring_function = DB::driverName() === DB::SQLITE ? 'SUBSTR' : 'SUBSTRING';
7010fc856aSGreg Roach
71c9beb871SGreg Roach                $query->select([
72c9beb871SGreg Roach                    'm_id',
73c9beb871SGreg Roach                    'm_file',
7410fc856aSGreg Roach                    new Expression($substring_function . '(m_filename, 1, 248)'),
7510fc856aSGreg Roach                    new Expression($substring_function . '(m_ext, 1, 4)'),
7610fc856aSGreg Roach                    new Expression($substring_function . '(m_type, 1, 15)'),
7710fc856aSGreg Roach                    new Expression($substring_function . '(m_titl, 1, 248)'),
78c9beb871SGreg Roach                ])->from('media');
79c9beb871SGreg Roach            });
808f5f5da8SGreg Roach
816ed487d2SGreg Roach            // The Laravel database library for SQLite can only drop one column at a time.
820b5fd0a6SGreg Roach            DB::schema()->table('media', static function (Blueprint $table): void {
83c9beb871SGreg Roach                $table->dropColumn('m_filename');
84b48bb5e9SGreg Roach            });
850b5fd0a6SGreg Roach            DB::schema()->table('media', static function (Blueprint $table): void {
86c9beb871SGreg Roach                $table->dropColumn('m_ext');
87b48bb5e9SGreg Roach            });
880b5fd0a6SGreg Roach            DB::schema()->table('media', static function (Blueprint $table): void {
89c9beb871SGreg Roach                $table->dropColumn('m_type');
90b48bb5e9SGreg Roach            });
910b5fd0a6SGreg Roach            DB::schema()->table('media', static function (Blueprint $table): void {
92c9beb871SGreg Roach                $table->dropColumn('m_titl');
93c9beb871SGreg Roach            });
948f5f5da8SGreg Roach        }
95d83c32e4SGreg Roach    }
96ac6b57f1SGreg Roach}
97