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