xref: /webtrees/app/Schema/Migration42.php (revision 09331e47ab40cb2729fd80102a96672e81d8e7b5)
167992b6aSRichard Cissee<?php
267992b6aSRichard Cissee/**
367992b6aSRichard Cissee * webtrees: online genealogy
467992b6aSRichard Cissee * Copyright (C) 2019 webtrees development team
567992b6aSRichard Cissee * This program is free software: you can redistribute it and/or modify
667992b6aSRichard Cissee * it under the terms of the GNU General Public License as published by
767992b6aSRichard Cissee * the Free Software Foundation, either version 3 of the License, or
867992b6aSRichard Cissee * (at your option) any later version.
967992b6aSRichard Cissee * This program is distributed in the hope that it will be useful,
1067992b6aSRichard Cissee * but WITHOUT ANY WARRANTY; without even the implied warranty of
1167992b6aSRichard Cissee * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1267992b6aSRichard Cissee * GNU General Public License for more details.
1367992b6aSRichard Cissee * You should have received a copy of the GNU General Public License
1467992b6aSRichard Cissee * along with this program. If not, see <http://www.gnu.org/licenses/>.
1567992b6aSRichard Cissee */
1667992b6aSRichard Cisseedeclare(strict_types=1);
1767992b6aSRichard Cissee
1867992b6aSRichard Cisseenamespace Fisharebest\Webtrees\Schema;
1967992b6aSRichard Cissee
2087cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
2187cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
2287cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface;
2387cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleMenuInterface;
2487cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
2587cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface;
2687cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
2787cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
2867992b6aSRichard Cisseeuse Illuminate\Database\Capsule\Manager as DB;
2967992b6aSRichard Cisseeuse Illuminate\Database\Schema\Blueprint;
3067992b6aSRichard Cissee
3167992b6aSRichard Cissee/**
3267992b6aSRichard Cissee * Upgrade the database schema from version 42 to version 43.
3367992b6aSRichard Cissee */
3467992b6aSRichard Cisseeclass Migration42 implements MigrationInterface
3567992b6aSRichard Cissee{
3687cca37cSGreg Roach    private const COMPONENT_TO_INTERFACE = [
3787cca37cSGreg Roach        'block'   => ModuleBlockInterface::class,
3887cca37cSGreg Roach        'chart'   => ModuleChartInterface::class,
3987cca37cSGreg Roach        'list'    => ModuleListInterface::class,
4087cca37cSGreg Roach        'menu'    => ModuleMenuInterface::class,
4187cca37cSGreg Roach        'report'  => ModuleReportInterface::class,
4287cca37cSGreg Roach        'sidebar' => ModuleSidebarInterface::class,
4387cca37cSGreg Roach        'tab'     => ModuleTabInterface::class,
4487cca37cSGreg Roach        'theme'   => ModuleThemeInterface::class,
4587cca37cSGreg Roach    ];
4687cca37cSGreg Roach
4767992b6aSRichard Cissee    /**
4867992b6aSRichard Cissee     * Upgrade to to the next version
4967992b6aSRichard Cissee     *
5067992b6aSRichard Cissee     * @return void
5167992b6aSRichard Cissee     */
5267992b6aSRichard Cissee    public function upgrade(): void
5367992b6aSRichard Cissee    {
5487cca37cSGreg Roach        // doctrine/dbal cannot modify tables containing ENUM fields
5567992b6aSRichard Cissee        $data = DB::table('module_privacy')->get();
5687cca37cSGreg Roach
5767992b6aSRichard Cissee        DB::schema()->drop('module_privacy');
5867992b6aSRichard Cissee
590b5fd0a6SGreg Roach        DB::schema()->create('module_privacy', static function (Blueprint $table): void {
6087cca37cSGreg Roach            $table->increments('id');
6167992b6aSRichard Cissee            $table->string('module_name', 32);
6267992b6aSRichard Cissee            $table->integer('gedcom_id');
6387cca37cSGreg Roach            $table->string('interface');
6467992b6aSRichard Cissee            $table->tinyInteger('access_level');
6567992b6aSRichard Cissee
66*09331e47SGreg Roach            // Default constraint names are too long for MySQL.
67*09331e47SGreg Roach            $key1 = DB::connection()->getTablePrefix() . $table->getTable() . '_ix1';
68*09331e47SGreg Roach            $key2 = DB::connection()->getTablePrefix() . $table->getTable() . '_ix2';
69*09331e47SGreg Roach
70*09331e47SGreg Roach            $table->unique(['gedcom_id', 'module_name', 'interface'], $key1);
71*09331e47SGreg Roach            $table->unique(['module_name', 'gedcom_id', 'interface'], $key2);
7267992b6aSRichard Cissee
7387cca37cSGreg Roach            $table->foreign('module_name')->references('module_name')->on('module')->onDelete('cascade');
7487cca37cSGreg Roach            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom')->onDelete('cascade');
7567992b6aSRichard Cissee        });
7667992b6aSRichard Cissee
7787cca37cSGreg Roach        foreach ($data as $datum) {
7867992b6aSRichard Cissee            DB::table('module_privacy')->insert([
7987cca37cSGreg Roach                'module_name'  => $datum->module_name,
8087cca37cSGreg Roach                'gedcom_id'    => $datum->gedcom_id,
8187cca37cSGreg Roach                'interface'    => self::COMPONENT_TO_INTERFACE[$datum->component],
8287cca37cSGreg Roach                'access_level' => $datum->access_level,
8367992b6aSRichard Cissee            ]);
8467992b6aSRichard Cissee        }
8567992b6aSRichard Cissee    }
8667992b6aSRichard Cissee}
87