xref: /webtrees/app/Services/MigrationService.php (revision f7cf8a155e2743f3d124eef3d30a558ab062fa4b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\Schema\MigrationInterface;
23use Fisharebest\Webtrees\Schema\SeedDefaultResnTable;
24use Fisharebest\Webtrees\Schema\SeedGedcomSettingTable;
25use Fisharebest\Webtrees\Schema\SeedGedcomTable;
26use Fisharebest\Webtrees\Schema\SeedSiteSettingTable;
27use Fisharebest\Webtrees\Schema\SeedUserTable;
28use Fisharebest\Webtrees\Site;
29use Illuminate\Database\Capsule\Manager as DB;
30use PDOException;
31
32/**
33 * Update the database schema.
34 */
35class MigrationService
36{
37    /**
38     * Run a series of scripts to bring the database schema up to date.
39     *
40     * @param string $namespace      Where to find our MigrationXXX classes
41     * @param string $schema_name    Which schema to update.
42     * @param int    $target_version Upgrade to this version
43     *
44     * @return bool  Were any updates applied
45     * @throws PDOException
46     */
47    public function updateSchema(string $namespace, string $schema_name, int $target_version): bool
48    {
49        try {
50            $current_version = (int) Site::getPreference($schema_name);
51        } catch (PDOException $ex) {
52            // During initial installation, the site_preference table won’t exist.
53            $current_version = 0;
54        }
55
56        if ($current_version < $target_version) {
57            try {
58                $this->transactionalTables();
59            } catch (PDOException $ex) {
60                // There is probably nothing we can do.
61            }
62        }
63
64        $updates_applied = false;
65
66        // Update the schema, one version at a time.
67        while ($current_version < $target_version) {
68            $class = $namespace . '\\Migration' . $current_version;
69            /** @var MigrationInterface $migration */
70            $migration = new $class();
71            $migration->upgrade();
72            $current_version++;
73            Site::setPreference($schema_name, (string) $current_version);
74            $updates_applied = true;
75        }
76
77        return $updates_applied;
78    }
79
80    /**
81     * Upgrades from older installations may have MyISAM or other non-transactional tables.
82     * These could prevent us from creating foreign key constraints.
83     *
84     * @return void
85     * @throws PDOException
86     */
87    private function transactionalTables(): void
88    {
89        $connection = DB::connection();
90
91        if ($connection->getDriverName() !== 'mysql') {
92            return;
93        }
94
95        $sql = "SELECT table_name FROM information_schema.tables JOIN information_schema.engines USING (engine) WHERE table_schema = ? AND LEFT(table_name, ?) = ? AND transactions <> 'YES'";
96
97        $bindings = [
98            $connection->getDatabaseName(),
99            mb_strlen($connection->getTablePrefix()),
100            $connection->getTablePrefix(),
101        ];
102
103        $rows = DB::connection()->select($sql, $bindings);
104
105        foreach ($rows as $row) {
106            $table = $row->TABLE_NAME ?? $row->table_name;
107            $alter_sql = 'ALTER TABLE `' . $table . '` ENGINE=InnoDB';
108            DB::connection()->statement($alter_sql);
109        }
110    }
111
112    /**
113     *  Write default data to the database.
114     *
115     * @return void
116     */
117    public function seedDatabase(): void
118    {
119        (new SeedSiteSettingTable())->run();
120        (new SeedUserTable())->run();
121        (new SeedGedcomTable())->run();
122        (new SeedGedcomSettingTable())->run();
123        (new SeedDefaultResnTable())->run();
124    }
125}
126