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