1*0c1c7615SGreg Roach<?php 2*0c1c7615SGreg Roach/** 3*0c1c7615SGreg Roach * webtrees: online genealogy 4*0c1c7615SGreg Roach * Copyright (C) 2019 webtrees development team 5*0c1c7615SGreg Roach * This program is free software: you can redistribute it and/or modify 6*0c1c7615SGreg Roach * it under the terms of the GNU General Public License as published by 7*0c1c7615SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8*0c1c7615SGreg Roach * (at your option) any later version. 9*0c1c7615SGreg Roach * This program is distributed in the hope that it will be useful, 10*0c1c7615SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*0c1c7615SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*0c1c7615SGreg Roach * GNU General Public License for more details. 13*0c1c7615SGreg Roach * You should have received a copy of the GNU General Public License 14*0c1c7615SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15*0c1c7615SGreg Roach */ 16*0c1c7615SGreg Roachdeclare(strict_types=1); 17*0c1c7615SGreg Roach 18*0c1c7615SGreg Roachnamespace Fisharebest\Webtrees\Services; 19*0c1c7615SGreg Roach 20*0c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\MigrationInterface; 21*0c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedDefaultResnTable; 22*0c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedGedcomSettingTable; 23*0c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedGedcomTable; 24*0c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedSiteSettingTable; 25*0c1c7615SGreg Roachuse Fisharebest\Webtrees\Schema\SeedUserTable; 26*0c1c7615SGreg Roachuse Fisharebest\Webtrees\Site; 27*0c1c7615SGreg Roachuse PDOException; 28*0c1c7615SGreg Roach 29*0c1c7615SGreg Roach/** 30*0c1c7615SGreg Roach * Update the database schema. 31*0c1c7615SGreg Roach */ 32*0c1c7615SGreg Roachclass MigrationService 33*0c1c7615SGreg Roach{ 34*0c1c7615SGreg Roach /** 35*0c1c7615SGreg Roach * Run a series of scripts to bring the database schema up to date. 36*0c1c7615SGreg Roach * 37*0c1c7615SGreg Roach * @param string $namespace Where to find our MigrationXXX classes 38*0c1c7615SGreg Roach * @param string $schema_name Which schema to update. 39*0c1c7615SGreg Roach * @param int $target_version Updade to this version 40*0c1c7615SGreg Roach * 41*0c1c7615SGreg Roach * @throws PDOException 42*0c1c7615SGreg Roach * @return bool Were any updates applied 43*0c1c7615SGreg Roach */ 44*0c1c7615SGreg Roach public static function updateSchema($namespace, $schema_name, $target_version): bool 45*0c1c7615SGreg Roach { 46*0c1c7615SGreg Roach try { 47*0c1c7615SGreg Roach $current_version = (int) Site::getPreference($schema_name); 48*0c1c7615SGreg Roach } catch (PDOException $ex) { 49*0c1c7615SGreg Roach // During initial installation, the site_preference table won’t exist. 50*0c1c7615SGreg Roach $current_version = 0; 51*0c1c7615SGreg Roach } 52*0c1c7615SGreg Roach 53*0c1c7615SGreg Roach $updates_applied = false; 54*0c1c7615SGreg Roach 55*0c1c7615SGreg Roach // Update the schema, one version at a time. 56*0c1c7615SGreg Roach while ($current_version < $target_version) { 57*0c1c7615SGreg Roach $class = $namespace . '\\Migration' . $current_version; 58*0c1c7615SGreg Roach /** @var MigrationInterface $migration */ 59*0c1c7615SGreg Roach $migration = new $class(); 60*0c1c7615SGreg Roach $migration->upgrade(); 61*0c1c7615SGreg Roach $current_version++; 62*0c1c7615SGreg Roach Site::setPreference($schema_name, (string) $current_version); 63*0c1c7615SGreg Roach $updates_applied = true; 64*0c1c7615SGreg Roach } 65*0c1c7615SGreg Roach 66*0c1c7615SGreg Roach return $updates_applied; 67*0c1c7615SGreg Roach } 68*0c1c7615SGreg Roach 69*0c1c7615SGreg Roach /** 70*0c1c7615SGreg Roach * Write default data to the database. 71*0c1c7615SGreg Roach * 72*0c1c7615SGreg Roach * @return void 73*0c1c7615SGreg Roach */ 74*0c1c7615SGreg Roach public function seedDatabase(): void 75*0c1c7615SGreg Roach { 76*0c1c7615SGreg Roach (new SeedSiteSettingTable())->run(); 77*0c1c7615SGreg Roach (new SeedUserTable())->run(); 78*0c1c7615SGreg Roach (new SeedGedcomTable())->run(); 79*0c1c7615SGreg Roach (new SeedGedcomSettingTable())->run(); 80*0c1c7615SGreg Roach (new SeedDefaultResnTable())->run(); 81*0c1c7615SGreg Roach } 82*0c1c7615SGreg Roach} 83