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