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