xref: /webtrees/app/Schema/Migration42.php (revision 24f2a3af38709f9bf0a739b30264240d20ba34e8)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Module\ModuleBlockInterface;
23use Fisharebest\Webtrees\Module\ModuleChartInterface;
24use Fisharebest\Webtrees\Module\ModuleListInterface;
25use Fisharebest\Webtrees\Module\ModuleMenuInterface;
26use Fisharebest\Webtrees\Module\ModuleReportInterface;
27use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
28use Fisharebest\Webtrees\Module\ModuleTabInterface;
29use Fisharebest\Webtrees\Module\ModuleThemeInterface;
30use Illuminate\Database\Capsule\Manager as DB;
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    /**
50     * Upgrade to to the next version
51     *
52     * @return void
53     */
54    public function upgrade(): void
55    {
56        // doctrine/dbal cannot modify tables containing ENUM fields
57        $data = DB::table('module_privacy')->get();
58
59        DB::schema()->drop('module_privacy');
60
61        DB::schema()->create('module_privacy', static function (Blueprint $table): void {
62            $table->increments('id');
63            $table->string('module_name', 32);
64            $table->integer('gedcom_id');
65            $table->string('interface');
66            $table->tinyInteger('access_level');
67
68            // Default constraint names are too long for MySQL.
69            $key1 = DB::connection()->getTablePrefix() . $table->getTable() . '_ix1';
70            $key2 = DB::connection()->getTablePrefix() . $table->getTable() . '_ix2';
71
72            $table->unique(['gedcom_id', 'module_name', 'interface'], $key1);
73            $table->unique(['module_name', 'gedcom_id', 'interface'], $key2);
74
75            $table->foreign('module_name')->references('module_name')->on('module')->onDelete('cascade');
76            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom')->onDelete('cascade');
77        });
78
79        foreach ($data as $datum) {
80            DB::table('module_privacy')->insert([
81                'module_name'  => $datum->module_name,
82                'gedcom_id'    => $datum->gedcom_id,
83                'interface'    => self::COMPONENT_TO_INTERFACE[$datum->component],
84                'access_level' => $datum->access_level,
85            ]);
86        }
87    }
88}
89