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