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