xref: /webtrees/app/Services/MigrationService.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\Services;
20
21use Fisharebest\Webtrees\Schema\MigrationInterface;
22use Fisharebest\Webtrees\Schema\SeedDefaultResnTable;
23use Fisharebest\Webtrees\Schema\SeedGedcomSettingTable;
24use Fisharebest\Webtrees\Schema\SeedGedcomTable;
25use Fisharebest\Webtrees\Schema\SeedSiteSettingTable;
26use Fisharebest\Webtrees\Schema\SeedUserTable;
27use Fisharebest\Webtrees\Site;
28use PDOException;
29
30/**
31 * Update the database schema.
32 */
33class MigrationService
34{
35    /**
36     * Run a series of scripts to bring the database schema up to date.
37     *
38     * @param string $namespace      Where to find our MigrationXXX classes
39     * @param string $schema_name    Which schema to update.
40     * @param int    $target_version Updade to this version
41     *
42     * @throws PDOException
43     * @return bool  Were any updates applied
44     */
45    public function updateSchema($namespace, $schema_name, $target_version): bool
46    {
47        try {
48            $current_version = (int) Site::getPreference($schema_name);
49        } catch (PDOException $ex) {
50            // During initial installation, the site_preference table won’t exist.
51            $current_version = 0;
52        }
53
54        $updates_applied = false;
55
56        // Update the schema, one version at a time.
57        while ($current_version < $target_version) {
58            $class = $namespace . '\\Migration' . $current_version;
59            /** @var MigrationInterface $migration */
60            $migration = new $class();
61            $migration->upgrade();
62            $current_version++;
63            Site::setPreference($schema_name, (string) $current_version);
64            $updates_applied = true;
65        }
66
67        return $updates_applied;
68    }
69
70    /**
71     *  Write default data to the database.
72     *
73     * @return void
74     */
75    public function seedDatabase(): void
76    {
77        (new SeedSiteSettingTable())->run();
78        (new SeedUserTable())->run();
79        (new SeedGedcomTable())->run();
80        (new SeedGedcomSettingTable())->run();
81        (new SeedDefaultResnTable())->run();
82    }
83}
84