xref: /webtrees/app/Services/MigrationService.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
10c1c7615SGreg Roach<?php
23976b470SGreg Roach
30c1c7615SGreg Roach/**
40c1c7615SGreg Roach * webtrees: online genealogy
50c1c7615SGreg Roach * Copyright (C) 2019 webtrees development team
60c1c7615SGreg Roach * This program is free software: you can redistribute it and/or modify
70c1c7615SGreg Roach * it under the terms of the GNU General Public License as published by
80c1c7615SGreg Roach * the Free Software Foundation, either version 3 of the License, or
90c1c7615SGreg Roach * (at your option) any later version.
100c1c7615SGreg Roach * This program is distributed in the hope that it will be useful,
110c1c7615SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
120c1c7615SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130c1c7615SGreg Roach * GNU General Public License for more details.
140c1c7615SGreg Roach * You should have received a copy of the GNU General Public License
150c1c7615SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
160c1c7615SGreg Roach */
17*fcfa147eSGreg Roach
180c1c7615SGreg Roachdeclare(strict_types=1);
190c1c7615SGreg Roach
200c1c7615SGreg Roachnamespace Fisharebest\Webtrees\Services;
210c1c7615SGreg Roach
220c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\MigrationInterface;
230c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDefaultResnTable;
240c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedGedcomSettingTable;
250c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedGedcomTable;
260c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedSiteSettingTable;
270c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedUserTable;
280c1c7615SGreg Roachuse Fisharebest\Webtrees\Site;
290c1c7615SGreg Roachuse PDOException;
300c1c7615SGreg Roach
310c1c7615SGreg Roach/**
320c1c7615SGreg Roach * Update the database schema.
330c1c7615SGreg Roach */
340c1c7615SGreg Roachclass MigrationService
350c1c7615SGreg Roach{
360c1c7615SGreg Roach    /**
370c1c7615SGreg Roach     * Run a series of scripts to bring the database schema up to date.
380c1c7615SGreg Roach     *
390c1c7615SGreg Roach     * @param string $namespace      Where to find our MigrationXXX classes
400c1c7615SGreg Roach     * @param string $schema_name    Which schema to update.
410c1c7615SGreg Roach     * @param int    $target_version Updade to this version
420c1c7615SGreg Roach     *
430c1c7615SGreg Roach     * @throws PDOException
440c1c7615SGreg Roach     * @return bool  Were any updates applied
450c1c7615SGreg Roach     */
465de77568SGreg Roach    public function updateSchema($namespace, $schema_name, $target_version): bool
470c1c7615SGreg Roach    {
480c1c7615SGreg Roach        try {
490c1c7615SGreg Roach            $current_version = (int) Site::getPreference($schema_name);
500c1c7615SGreg Roach        } catch (PDOException $ex) {
510c1c7615SGreg Roach            // During initial installation, the site_preference table won’t exist.
520c1c7615SGreg Roach            $current_version = 0;
530c1c7615SGreg Roach        }
540c1c7615SGreg Roach
550c1c7615SGreg Roach        $updates_applied = false;
560c1c7615SGreg Roach
570c1c7615SGreg Roach        // Update the schema, one version at a time.
580c1c7615SGreg Roach        while ($current_version < $target_version) {
590c1c7615SGreg Roach            $class = $namespace . '\\Migration' . $current_version;
600c1c7615SGreg Roach            /** @var MigrationInterface $migration */
610c1c7615SGreg Roach            $migration = new $class();
620c1c7615SGreg Roach            $migration->upgrade();
630c1c7615SGreg Roach            $current_version++;
640c1c7615SGreg Roach            Site::setPreference($schema_name, (string) $current_version);
650c1c7615SGreg Roach            $updates_applied = true;
660c1c7615SGreg Roach        }
670c1c7615SGreg Roach
680c1c7615SGreg Roach        return $updates_applied;
690c1c7615SGreg Roach    }
700c1c7615SGreg Roach
710c1c7615SGreg Roach    /**
720c1c7615SGreg Roach     *  Write default data to the database.
730c1c7615SGreg Roach     *
740c1c7615SGreg Roach     * @return void
750c1c7615SGreg Roach     */
760c1c7615SGreg Roach    public function seedDatabase(): void
770c1c7615SGreg Roach    {
780c1c7615SGreg Roach        (new SeedSiteSettingTable())->run();
790c1c7615SGreg Roach        (new SeedUserTable())->run();
800c1c7615SGreg Roach        (new SeedGedcomTable())->run();
810c1c7615SGreg Roach        (new SeedGedcomSettingTable())->run();
820c1c7615SGreg Roach        (new SeedDefaultResnTable())->run();
830c1c7615SGreg Roach    }
840c1c7615SGreg Roach}
85