1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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 <https://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\Query\Expression; 25use Illuminate\Database\Schema\Blueprint; 26 27/** 28 * Upgrade the database schema from version 37 to version 38. 29 */ 30class Migration37 implements MigrationInterface 31{ 32 /** 33 * Upgrade to the next version 34 * 35 * @return void 36 */ 37 public function upgrade(): void 38 { 39 // These tables were created by webtrees 1.x, and may not exist if we first installed webtrees 2.x 40 DB::schema()->dropIfExists('site_access_rule'); 41 DB::schema()->dropIfExists('next_id'); 42 43 // Split the media table into media/media_file so that we can store multiple media 44 // files in each media object. 45 if (!DB::schema()->hasTable('media_file')) { 46 DB::schema()->create('media_file', static function (Blueprint $table): void { 47 $table->integer('id', true); 48 $table->string('m_id', 20); 49 $table->integer('m_file'); 50 $table->string('multimedia_file_refn', 248); // GEDCOM only allows 30 characters 51 $table->string('multimedia_format', 4); 52 $table->string('source_media_type', 15); 53 $table->string('descriptive_title', 248); 54 55 $table->index(['m_id', 'm_file']); 56 $table->index(['m_file', 'm_id']); 57 $table->index(['m_file', 'multimedia_file_refn']); 58 $table->index(['m_file', 'multimedia_format']); 59 $table->index(['m_file', 'source_media_type']); 60 $table->index(['m_file', 'descriptive_title']); 61 }); 62 } 63 64 if (DB::table('media_file')->count() === 0 && DB::schema()->hasColumn('media', 'm_filename')) { 65 (new Builder(DB::connection()))->from('media_file')->insertUsing([ 66 'm_id', 67 'm_file', 68 'multimedia_file_refn', 69 'multimedia_format', 70 'source_media_type', 71 'descriptive_title', 72 ], function (Builder $query): void { 73 // SQLite also supports SUBSTRING() from 3.34.0 (2020-12-01) 74 $substring_function = DB::connection()->getDriverName() === 'sqlite' ? 'SUBSTR' : 'SUBSTRING'; 75 76 $query->select([ 77 'm_id', 78 'm_file', 79 new Expression($substring_function . '(m_filename, 1, 248)'), 80 new Expression($substring_function . '(m_ext, 1, 4)'), 81 new Expression($substring_function . '(m_type, 1, 15)'), 82 new Expression($substring_function . '(m_titl, 1, 248)'), 83 ])->from('media'); 84 }); 85 86 // The Laravel database library for SQLite can only drop one column at a time. 87 // But SQL-Server needs to delete them together, due to the compound index.ZZ 88 if (DB::connection()->getDriverName() === 'sqlite') { 89 DB::schema()->table('media', static function (Blueprint $table): void { 90 $table->dropColumn('m_filename'); 91 }); 92 DB::schema()->table('media', static function (Blueprint $table): void { 93 $table->dropColumn('m_ext'); 94 }); 95 DB::schema()->table('media', static function (Blueprint $table): void { 96 $table->dropColumn('m_type'); 97 }); 98 DB::schema()->table('media', static function (Blueprint $table): void { 99 $table->dropColumn('m_titl'); 100 }); 101 } else { 102 DB::schema()->table('media', static function (Blueprint $table): void { 103 $table->dropColumn('m_filename'); 104 $table->dropColumn('m_ext'); 105 $table->dropColumn('m_type'); 106 $table->dropColumn('m_titl'); 107 }); 108 } 109 } 110 } 111} 112