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
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Schema; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Driver\AbstractMySQLDriver; |
23
|
|
|
use Fisharebest\Webtrees\DB; |
24
|
|
|
use Fisharebest\Webtrees\DB\WebtreesSchema; |
25
|
|
|
|
26
|
|
|
use function usort; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Upgrade the database schema from version 45 to version 46. |
30
|
|
|
*/ |
31
|
|
|
class Migration45 implements MigrationInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Upgrade to the next version |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function upgrade(): void |
39
|
|
|
{ |
40
|
|
|
$platform = DB::getDBALConnection()->getDatabasePlatform(); |
41
|
|
|
$platform->registerDoctrineTypeMapping(dbType: 'enum', doctrineType: 'string'); |
42
|
|
|
|
43
|
|
|
$schema_manager = DB::getDBALConnection()->createSchemaManager(); |
44
|
|
|
$comparator = $schema_manager->createComparator(); |
45
|
|
|
$source = $schema_manager->introspectSchema(); |
46
|
|
|
$target = WebtreesSchema::schema(); |
47
|
|
|
|
48
|
|
|
// doctrine/dbal 4.0 does not have the concept of "saveSQL" |
49
|
|
|
foreach ($source->getTables() as $table) { |
50
|
|
|
if (!$target->hasTable($table->getName())) { |
51
|
|
|
$source->dropTable($table->getName()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$schema_diff = $comparator->compareSchemas(oldSchema: $source, newSchema: $target); |
56
|
|
|
$queries = $platform->getAlterSchemaSQL(diff: $schema_diff); |
57
|
|
|
|
58
|
|
|
// Workaround for https://github.com/doctrine/dbal/issues/6092 |
59
|
|
|
$phase = static fn (string $query): int => match (true) { |
60
|
|
|
str_contains(haystack: $query, needle: 'DROP FOREIGN KEY') => 1, |
61
|
|
|
default => 2, |
62
|
|
|
str_contains(haystack: $query, needle: 'FOREIGN KEY') => 3, |
63
|
|
|
}; |
64
|
|
|
$fn = static fn (string $query1, string $query2): int => $phase(query: $query1) <=> $phase(query: $query2); |
65
|
|
|
usort(array: $queries, callback: $fn); |
66
|
|
|
|
67
|
|
|
// SQLite, PostgreSQL and SQL-Server all support DDL in transactions |
68
|
|
|
if (DB::driverName() === DB::MYSQL) { |
69
|
|
|
$queries = [ |
70
|
|
|
'SET FOREIGN_KEY_CHECKS := 0', |
71
|
|
|
...$queries, |
72
|
|
|
'SET FOREIGN_KEY_CHECKS := 1', |
73
|
|
|
]; |
74
|
|
|
} else { |
75
|
|
|
$queries = [ |
76
|
|
|
'START TRANSACTION', |
77
|
|
|
...$queries, |
78
|
|
|
'COMMIT', |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
foreach ($queries as $query) { |
83
|
|
|
DB::exec(sql: $query); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|