xref: /webtrees/app/Schema/Migration37.php (revision df27497a949ee94233b78915e33f253447a14e62)
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
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\Schema\Blueprint;
25
26/**
27 * Upgrade the database schema from version 37 to version 38.
28 */
29class Migration37 implements MigrationInterface
30{
31    /**
32     * Upgrade to to the next version
33     *
34     * @return void
35     */
36    public function upgrade(): void
37    {
38        // These tables were created by webtrees 1.x, and may not exist if we first installed webtrees 2.x
39        DB::schema()->dropIfExists('site_access_rule');
40        DB::schema()->dropIfExists('next_id');
41
42        // Split the media table into media/media_file so that we can store multiple media
43        // files in each media object.
44        DB::schema()->create('media_file', static function (Blueprint $table): void {
45            $table->integer('id', true);
46            $table->string('m_id', 20);
47            $table->integer('m_file');
48            $table->string('multimedia_file_refn', 248); // GEDCOM only allows 30 characters
49            $table->string('multimedia_format', 4);
50            $table->string('source_media_type', 15);
51            $table->string('descriptive_title', 248);
52
53            $table->index(['m_id', 'm_file']);
54            $table->index(['m_file', 'm_id']);
55            $table->index(['m_file', 'multimedia_file_refn']);
56            $table->index(['m_file', 'multimedia_format']);
57            $table->index(['m_file', 'source_media_type']);
58            $table->index(['m_file', 'descriptive_title']);
59        });
60
61        if (DB::schema()->hasColumn('media', 'm_filename')) {
62            (new Builder(DB::connection()))->from('media_file')->insertUsing([
63                'm_id',
64                'm_file',
65                'multimedia_file_refn',
66                'multimedia_format',
67                'source_media_type',
68                'descriptive_title',
69            ], static function (Builder $query): void {
70                $query->select([
71                    'm_id',
72                    'm_file',
73                    'm_filename',
74                    'm_ext',
75                    'm_type',
76                    'm_titl',
77                ])->from('media');
78            });
79
80            // SQLite can only drop one column at a time.
81            DB::schema()->table('media', static function (Blueprint $table): void {
82                $table->dropColumn('m_filename');
83            });
84            DB::schema()->table('media', static function (Blueprint $table): void {
85                $table->dropColumn('m_ext');
86            });
87            DB::schema()->table('media', static function (Blueprint $table): void {
88                $table->dropColumn('m_type');
89            });
90            DB::schema()->table('media', static function (Blueprint $table): void {
91                $table->dropColumn('m_titl');
92            });
93        }
94    }
95}
96