-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathDiffGenerator.php
153 lines (126 loc) · 4.69 KB
/
DiffGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace Doctrine\Migrations\Generator;
use Doctrine\DBAL\Configuration as DBALConfiguration;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractAsset;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Generator\Exception\NoChangesDetected;
use Doctrine\Migrations\Provider\SchemaProvider;
use function method_exists;
use function preg_match;
use function strpos;
use function substr;
/**
* The DiffGenerator class is responsible for comparing two Doctrine\DBAL\Schema\Schema instances and generating a
* migration class with the SQL statements needed to migrate from one schema to the other.
*
* @internal
*/
class DiffGenerator
{
/** @param AbstractSchemaManager<AbstractPlatform> $schemaManager */
public function __construct(
private readonly DBALConfiguration $dbalConfiguration,
private readonly AbstractSchemaManager $schemaManager,
private readonly SchemaProvider $schemaProvider,
private readonly AbstractPlatform $platform,
private readonly Generator $migrationGenerator,
private readonly SqlGenerator $migrationSqlGenerator,
private readonly SchemaProvider $emptySchemaProvider,
) {
}
/** @throws NoChangesDetected */
public function generate(
string $fqcn,
string|null $filterExpression,
bool $formatted = false,
int $lineLength = 120,
bool $checkDbPlatform = true,
bool $fromEmptySchema = false,
): string {
if ($filterExpression !== null) {
$this->dbalConfiguration->setSchemaAssetsFilter(
static function ($assetName) use ($filterExpression) {
if ($assetName instanceof AbstractAsset) {
$assetName = $assetName->getName();
}
return preg_match($filterExpression, $assetName);
},
);
}
$fromSchema = $fromEmptySchema
? $this->createEmptySchema()
: $this->createFromSchema();
$toSchema = $this->createToSchema();
// prior to DBAL 4.0, the schema name was set to the first element in the search path,
// which is not necessarily the default schema name
if (
! method_exists($this->schemaManager, 'getSchemaSearchPaths')
&& $this->platform->supportsSchemas()
) {
$defaultNamespace = $toSchema->getName();
if ($defaultNamespace !== '') {
$toSchema->createNamespace($defaultNamespace);
}
}
$comparator = $this->schemaManager->createComparator();
$upSql = $this->platform->getAlterSchemaSQL($comparator->compareSchemas($fromSchema, $toSchema));
$up = $this->migrationSqlGenerator->generate(
$upSql,
$formatted,
$lineLength,
$checkDbPlatform,
);
$downSql = $this->platform->getAlterSchemaSQL($comparator->compareSchemas($toSchema, $fromSchema));
$down = $this->migrationSqlGenerator->generate(
$downSql,
$formatted,
$lineLength,
$checkDbPlatform,
);
if ($up === '' && $down === '') {
throw NoChangesDetected::new();
}
return $this->migrationGenerator->generateMigration(
$fqcn,
$up,
$down,
);
}
private function createEmptySchema(): Schema
{
return $this->emptySchemaProvider->createSchema();
}
private function createFromSchema(): Schema
{
return $this->schemaManager->introspectSchema();
}
private function createToSchema(): Schema
{
$toSchema = $this->schemaProvider->createSchema();
$schemaAssetsFilter = $this->dbalConfiguration->getSchemaAssetsFilter();
if ($schemaAssetsFilter !== null) {
foreach ($toSchema->getTables() as $table) {
$tableName = $table->getName();
if ($schemaAssetsFilter($this->resolveTableName($tableName))) {
continue;
}
$toSchema->dropTable($tableName);
}
}
return $toSchema;
}
/**
* Resolve a table name from its fully qualified name. The `$name` argument
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
* the table name from that.
*/
private function resolveTableName(string $name): string
{
$pos = strpos($name, '.');
return $pos === false ? $name : substr($name, $pos + 1);
}
}