xref: /webtrees/app/Schema/Migration37.php (revision 3fa66c660869af2f4c92ef37e06997aa8a0f55e1)
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 Fisharebest\Webtrees\Database;
21use Illuminate\Database\Query\Builder;
22use PDOException;
23use Illuminate\Database\Capsule\Manager as DB;
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', 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', 512); // 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            // Need to set row format so that we can index fields longer than 255 characters.
61            $table->engine = 'InnoDB ROW_FORMAT=COMPRESSED';
62        });
63
64        if (DB::schema()->hasColumn('media', 'm_filename')) {
65            $builder = new Builder(DB::connection());
66            $builder->from('media_file')->insertUsing([
67                'm_id',
68                'm_file',
69                'multimedia_file_refn',
70                'multimedia_format',
71                'source_media_type',
72                'descriptive_title',
73            ], function (Builder $query) {
74                $query->select([
75                    'm_id',
76                    'm_file',
77                    'm_filename',
78                    'm_ext',
79                    'm_type', //DB::raw('LEFT(m_type, 15)'),
80                    'm_titl',
81                ])->from('media');
82            });
83
84            // SQLite can only drop one column at a time.
85            DB::schema()->table('media', function (Blueprint $table): void {
86                $table->dropColumn('m_filename');
87            });
88            DB::schema()->table('media', function (Blueprint $table): void {
89                $table->dropColumn('m_ext');
90            });
91            DB::schema()->table('media', function (Blueprint $table): void {
92                $table->dropColumn('m_type');
93            });
94            DB::schema()->table('media', function (Blueprint $table): void {
95                $table->dropColumn('m_titl');
96            });
97        }
98    }
99}
100