|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Configuration as DBALConfiguration; |
|
8
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
9
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
|
10
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
11
|
|
|
use Doctrine\Migrations\Provider\SchemaProviderInterface; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
use function preg_match; |
|
14
|
|
|
use function strpos; |
|
15
|
|
|
use function substr; |
|
16
|
|
|
|
|
17
|
|
|
class MigrationDiffGenerator |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var DBALConfiguration */ |
|
20
|
|
|
private $dbalConfiguration; |
|
21
|
|
|
|
|
22
|
|
|
/** @var AbstractSchemaManager */ |
|
23
|
|
|
private $schemaManager; |
|
24
|
|
|
|
|
25
|
|
|
/** @var SchemaProviderInterface */ |
|
26
|
|
|
private $schemaProvider; |
|
27
|
|
|
|
|
28
|
|
|
/** @var AbstractPlatform */ |
|
29
|
|
|
private $platform; |
|
30
|
|
|
|
|
31
|
|
|
/** @var MigrationGenerator */ |
|
32
|
|
|
private $migrationGenerator; |
|
33
|
|
|
|
|
34
|
|
|
/** @var MigrationSqlGenerator */ |
|
35
|
|
|
private $migrationSqlGenerator; |
|
36
|
|
|
|
|
37
|
6 |
|
public function __construct( |
|
38
|
|
|
DBALConfiguration $dbalConfiguration, |
|
39
|
|
|
AbstractSchemaManager $schemaManager, |
|
40
|
|
|
SchemaProviderInterface $schemaProvider, |
|
41
|
|
|
AbstractPlatform $platform, |
|
42
|
|
|
MigrationGenerator $migrationGenerator, |
|
43
|
|
|
MigrationSqlGenerator $migrationSqlGenerator |
|
44
|
|
|
) { |
|
45
|
6 |
|
$this->dbalConfiguration = $dbalConfiguration; |
|
46
|
6 |
|
$this->schemaManager = $schemaManager; |
|
47
|
6 |
|
$this->schemaProvider = $schemaProvider; |
|
48
|
6 |
|
$this->platform = $platform; |
|
49
|
6 |
|
$this->migrationGenerator = $migrationGenerator; |
|
50
|
6 |
|
$this->migrationSqlGenerator = $migrationSqlGenerator; |
|
51
|
6 |
|
} |
|
52
|
|
|
|
|
53
|
6 |
|
public function generate( |
|
54
|
|
|
string $versionNumber, |
|
55
|
|
|
?string $filterExpression, |
|
56
|
|
|
bool $formatted = false, |
|
57
|
|
|
int $lineLength = 120 |
|
58
|
|
|
) : string { |
|
59
|
6 |
|
if ($filterExpression !== null) { |
|
60
|
1 |
|
$this->dbalConfiguration->setFilterSchemaAssetsExpression($filterExpression); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
6 |
|
$fromSchema = $this->createFromSchema(); |
|
64
|
|
|
|
|
65
|
6 |
|
$toSchema = $this->createToSchema(); |
|
66
|
|
|
|
|
67
|
6 |
|
$up = $this->migrationSqlGenerator->generate( |
|
68
|
6 |
|
$fromSchema->getMigrateToSql($toSchema, $this->platform), |
|
69
|
6 |
|
$formatted, |
|
70
|
6 |
|
$lineLength |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
6 |
|
$down = $this->migrationSqlGenerator->generate( |
|
74
|
6 |
|
$fromSchema->getMigrateFromSql($toSchema, $this->platform), |
|
75
|
6 |
|
$formatted, |
|
76
|
6 |
|
$lineLength |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
6 |
|
if ($up === '' && $down === '') { |
|
80
|
|
|
throw new RuntimeException('No changes detected in your mapping information.'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
6 |
|
return $this->migrationGenerator->generateMigration( |
|
84
|
6 |
|
$versionNumber, |
|
85
|
6 |
|
$up, |
|
86
|
6 |
|
$down |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
6 |
|
private function createFromSchema() : Schema |
|
91
|
|
|
{ |
|
92
|
6 |
|
return $this->schemaManager->createSchema(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
private function createToSchema() : Schema |
|
96
|
|
|
{ |
|
97
|
6 |
|
$toSchema = $this->schemaProvider->createSchema(); |
|
98
|
|
|
|
|
99
|
6 |
|
$filterExpression = $this->dbalConfiguration->getFilterSchemaAssetsExpression(); |
|
100
|
|
|
|
|
101
|
6 |
|
if ($filterExpression !== null) { |
|
102
|
3 |
|
foreach ($toSchema->getTables() as $table) { |
|
103
|
3 |
|
$tableName = $table->getName(); |
|
104
|
|
|
|
|
105
|
3 |
|
if (preg_match($filterExpression, $this->resolveTableName($tableName))) { |
|
106
|
3 |
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
$toSchema->dropTable($tableName); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
6 |
|
return $toSchema; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Resolve a table name from its fully qualified name. The `$name` argument |
|
118
|
|
|
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return |
|
119
|
|
|
* a namespaced name with the form `{namespace}.{tableName}`. This extracts |
|
120
|
|
|
* the table name from that. |
|
121
|
|
|
*/ |
|
122
|
3 |
|
private function resolveTableName(string $name) : string |
|
123
|
|
|
{ |
|
124
|
3 |
|
$pos = strpos($name, '.'); |
|
125
|
|
|
|
|
126
|
3 |
|
return $pos === false ? $name : substr($name, $pos + 1); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|