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
|
|
|
use function var_dump; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Upgrade the database schema from version 45 to version 46. |
31
|
|
|
*/ |
32
|
|
|
class Migration45 implements MigrationInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Upgrade to the next version |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function upgrade(): void |
40
|
|
|
{ |
41
|
|
|
$platform = DB::getDBALConnection()->getDatabasePlatform(); |
42
|
|
|
$platform->registerDoctrineTypeMapping(dbType: 'enum', doctrineType: 'string'); |
43
|
|
|
|
44
|
|
|
$schema_manager = DB::getDBALConnection()->createSchemaManager(); |
45
|
|
|
$comparator = $schema_manager->createComparator(); |
46
|
|
|
$source = $schema_manager->introspectSchema(); |
47
|
|
|
$target = WebtreesSchema::schema(); |
48
|
|
|
|
49
|
|
|
// doctrine/dbal 4.0 does not have the concept of "saveSQL" |
50
|
|
|
foreach ($source->getTables() as $table) { |
51
|
|
|
if (!$target->hasTable($table->getName())) { |
52
|
|
|
$source->dropTable($table->getName()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$schema_diff = $comparator->compareSchemas(oldSchema: $source, newSchema: $target); |
57
|
|
|
$queries = $platform->getAlterSchemaSQL(diff: $schema_diff); |
58
|
|
|
|
59
|
|
|
// Workaround for https://github.com/doctrine/dbal/issues/6092 |
60
|
|
|
$phase = static fn (string $query): int => match (true) { |
61
|
|
|
str_contains(haystack: $query, needle: 'DROP FOREIGN KEY') => 1, |
62
|
|
|
default => 2, |
63
|
|
|
str_contains(haystack: $query, needle: 'FOREIGN KEY') => 3, |
64
|
|
|
}; |
65
|
|
|
$fn = static fn (string $query1, string $query2): int => $phase(query: $query1) <=> $phase(query: $query2); |
66
|
|
|
usort(array: $queries, callback: $fn); |
67
|
|
|
|
68
|
|
|
foreach ($queries as $query) { |
69
|
|
|
echo '<p>' . $query . '</p>'; |
70
|
|
|
DB::exec(sql: $query); |
71
|
|
|
} |
72
|
|
|
exit; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.