xref: /webtrees/app/Schema/Migration42.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\Module\ModuleBlockInterface;
24use Fisharebest\Webtrees\Module\ModuleChartInterface;
25use Fisharebest\Webtrees\Module\ModuleListInterface;
26use Fisharebest\Webtrees\Module\ModuleMenuInterface;
27use Fisharebest\Webtrees\Module\ModuleReportInterface;
28use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
29use Fisharebest\Webtrees\Module\ModuleTabInterface;
30use Fisharebest\Webtrees\Module\ModuleThemeInterface;
31use Illuminate\Database\Schema\Blueprint;
32
33/**
34 * Upgrade the database schema from version 42 to version 43.
35 */
36class Migration42 implements MigrationInterface
37{
38    private const COMPONENT_TO_INTERFACE = [
39        'block'   => ModuleBlockInterface::class,
40        'chart'   => ModuleChartInterface::class,
41        'list'    => ModuleListInterface::class,
42        'menu'    => ModuleMenuInterface::class,
43        'report'  => ModuleReportInterface::class,
44        'sidebar' => ModuleSidebarInterface::class,
45        'tab'     => ModuleTabInterface::class,
46        'theme'   => ModuleThemeInterface::class,
47    ];
48
49    public function upgrade(): void
50    {
51        // doctrine/dbal cannot modify tables containing ENUM fields
52        $data = DB::table('module_privacy')->get();
53
54        DB::schema()->drop('module_privacy');
55
56        DB::schema()->create('module_privacy', static function (Blueprint $table): void {
57            $table->increments('id');
58            $table->string('module_name', 32);
59            $table->integer('gedcom_id');
60            $table->string('interface');
61            $table->tinyInteger('access_level');
62
63            // Default constraint names are too long for MySQL.
64            $key1 = DB::prefix($table->getTable() . '_ix1');
65            $key2 = DB::prefix($table->getTable() . '_ix2');
66
67            $table->unique(['gedcom_id', 'module_name', 'interface'], $key1);
68            $table->unique(['module_name', 'gedcom_id', 'interface'], $key2);
69
70            $table->foreign('module_name')->references('module_name')->on('module')->onDelete('cascade');
71            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom')->onDelete('cascade');
72        });
73
74        foreach ($data as $datum) {
75            DB::table('module_privacy')->insert([
76                'module_name'  => $datum->module_name,
77                'gedcom_id'    => $datum->gedcom_id,
78                'interface'    => self::COMPONENT_TO_INTERFACE[$datum->component],
79                'access_level' => $datum->access_level,
80            ]);
81        }
82    }
83}
84