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