|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
8
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use function count; |
|
11
|
|
|
use function implode; |
|
12
|
|
|
|
|
13
|
|
|
class SchemaDumper |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var AbstractPlatform */ |
|
16
|
|
|
private $platform; |
|
17
|
|
|
|
|
18
|
|
|
/** @var AbstractSchemaManager */ |
|
19
|
|
|
private $schemaManager; |
|
20
|
|
|
|
|
21
|
|
|
/** @var MigrationGenerator */ |
|
22
|
|
|
private $migrationGenerator; |
|
23
|
|
|
|
|
24
|
|
|
/** @var MigrationSqlGenerator */ |
|
25
|
|
|
private $migrationSqlGenerator; |
|
26
|
|
|
|
|
27
|
3 |
|
public function __construct( |
|
28
|
|
|
AbstractPlatform $platform, |
|
29
|
|
|
AbstractSchemaManager $schemaManager, |
|
30
|
|
|
MigrationGenerator $migrationGenerator, |
|
31
|
|
|
MigrationSqlGenerator $migrationSqlGenerator |
|
32
|
|
|
) { |
|
33
|
3 |
|
$this->platform = $platform; |
|
34
|
3 |
|
$this->schemaManager = $schemaManager; |
|
35
|
3 |
|
$this->migrationGenerator = $migrationGenerator; |
|
36
|
3 |
|
$this->migrationSqlGenerator = $migrationSqlGenerator; |
|
37
|
3 |
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
public function dump( |
|
40
|
|
|
string $versionNumber, |
|
41
|
|
|
bool $formatted = false, |
|
42
|
|
|
int $lineLength = 120 |
|
43
|
|
|
) : string { |
|
44
|
2 |
|
$schema = $this->schemaManager->createSchema(); |
|
45
|
|
|
|
|
46
|
2 |
|
$up = []; |
|
47
|
2 |
|
$down = []; |
|
48
|
|
|
|
|
49
|
2 |
|
foreach ($schema->getTables() as $table) { |
|
50
|
1 |
|
$upSql = $this->platform->getCreateTableSQL($table); |
|
51
|
|
|
|
|
52
|
1 |
|
$upCode = $this->migrationSqlGenerator->generate( |
|
53
|
1 |
|
$upSql, |
|
54
|
1 |
|
$formatted, |
|
55
|
1 |
|
$lineLength |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
1 |
|
if ($upCode !== '') { |
|
59
|
1 |
|
$up[] = $upCode; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
$downSql = [$this->platform->getDropTableSQL($table)]; |
|
63
|
|
|
|
|
64
|
1 |
|
$downCode = $this->migrationSqlGenerator->generate( |
|
65
|
1 |
|
$downSql, |
|
66
|
1 |
|
$formatted, |
|
67
|
1 |
|
$lineLength |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
1 |
|
if ($downCode === '') { |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$down[] = $downCode; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
if (count($up) === 0) { |
|
78
|
1 |
|
throw new RuntimeException('Your database schema does not contain any tables.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$up = implode("\n", $up); |
|
82
|
1 |
|
$down = implode("\n", $down); |
|
83
|
|
|
|
|
84
|
1 |
|
return $this->migrationGenerator->generateMigration( |
|
85
|
1 |
|
$versionNumber, |
|
86
|
1 |
|
$up, |
|
87
|
1 |
|
$down |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|