Passed
Push — 2.1 ( 4f7222...fd5fab )
by Greg
08:07
created

MigrationService::transactionalTables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 0
dl 0
loc 22
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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 <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Services;
21
22
use Fisharebest\Webtrees\Schema\MigrationInterface;
23
use Fisharebest\Webtrees\Schema\SeedDefaultResnTable;
24
use Fisharebest\Webtrees\Schema\SeedGedcomTable;
25
use Fisharebest\Webtrees\Schema\SeedUserTable;
26
use Fisharebest\Webtrees\Site;
27
use PDOException;
28
29
/**
30
 * Update the database schema.
31
 */
32
class 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 Upgrade to this version
40
     *
41
     * @return bool  Were any updates applied
42
     * @throws PDOException
43
     */
44
    public function updateSchema(string $namespace, string $schema_name, int $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 SeedUserTable())->run();
77
        (new SeedGedcomTable())->run();
78
        (new SeedDefaultResnTable())->run();
79
    }
80
}
81