xref: /webtrees/app/Schema/Migration37.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Schema;
19
20use Illuminate\Database\Capsule\Manager as DB;
21use Illuminate\Database\Query\Builder;
22use Illuminate\Database\Schema\Blueprint;
23
24/**
25 * Upgrade the database schema from version 37 to version 38.
26 */
27class Migration37 implements MigrationInterface
28{
29    /**
30     * Upgrade to to the next version
31     *
32     * @return void
33     */
34    public function upgrade(): void
35    {
36        // These tables were created by webtrees 1.x, and may not exist if we first installed webtrees 2.x
37        DB::schema()->dropIfExists('site_access_rule');
38        DB::schema()->dropIfExists('next_id');
39
40        // Split the media table into media/media_file so that we can store multiple media
41        // files in each media object.
42        DB::schema()->create('media_file', static function (Blueprint $table): void {
43            $table->integer('id', true);
44            $table->string('m_id', 20);
45            $table->integer('m_file');
46            $table->string('multimedia_file_refn', 248); // GEDCOM only allows 30 characters
47            $table->string('multimedia_format', 4);
48            $table->string('source_media_type', 15);
49            $table->string('descriptive_title', 248);
50
51            $table->index(['m_id', 'm_file']);
52            $table->index(['m_file', 'm_id']);
53            $table->index(['m_file', 'multimedia_file_refn']);
54            $table->index(['m_file', 'multimedia_format']);
55            $table->index(['m_file', 'source_media_type']);
56            $table->index(['m_file', 'descriptive_title']);
57        });
58
59        if (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            ], static function (Builder $query): void {
68                $query->select([
69                    'm_id',
70                    'm_file',
71                    'm_filename',
72                    'm_ext',
73                    'm_type',
74                    'm_titl',
75                ])->from('media');
76            });
77
78            // SQLite can only drop one column at a time.
79            DB::schema()->table('media', static function (Blueprint $table): void {
80                $table->dropColumn('m_filename');
81            });
82            DB::schema()->table('media', static function (Blueprint $table): void {
83                $table->dropColumn('m_ext');
84            });
85            DB::schema()->table('media', static function (Blueprint $table): void {
86                $table->dropColumn('m_type');
87            });
88            DB::schema()->table('media', static function (Blueprint $table): void {
89                $table->dropColumn('m_titl');
90            });
91        }
92    }
93}
94