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