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