|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Generator; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
8
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
|
9
|
|
|
use SqlFormatter; |
|
10
|
|
|
use function array_unshift; |
|
11
|
|
|
use function count; |
|
12
|
|
|
use function implode; |
|
13
|
|
|
use function sprintf; |
|
14
|
|
|
use function stripos; |
|
15
|
|
|
use function strlen; |
|
16
|
|
|
use function var_export; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The SqlGenerator class is responsible for generating the body of the up() and down() methods for a migration |
|
20
|
|
|
* from an array of SQL queries. |
|
21
|
|
|
* |
|
22
|
|
|
* @internal |
|
23
|
|
|
*/ |
|
24
|
|
|
class SqlGenerator |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var Configuration */ |
|
27
|
|
|
private $configuration; |
|
28
|
|
|
|
|
29
|
|
|
/** @var AbstractPlatform */ |
|
30
|
|
|
private $platform; |
|
31
|
|
|
|
|
32
|
9 |
|
public function __construct(Configuration $configuration, AbstractPlatform $platform) |
|
33
|
|
|
{ |
|
34
|
9 |
|
$this->configuration = $configuration; |
|
35
|
9 |
|
$this->platform = $platform; |
|
36
|
9 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** @param string[] $sql */ |
|
39
|
8 |
|
public function generate( |
|
40
|
|
|
array $sql, |
|
41
|
|
|
bool $formatted = false, |
|
42
|
|
|
int $lineLength = 120, |
|
43
|
|
|
bool $checkDbPlatform = true |
|
44
|
|
|
) : string { |
|
45
|
8 |
|
$code = []; |
|
46
|
|
|
|
|
47
|
8 |
|
foreach ($sql as $query) { |
|
48
|
8 |
|
if (stripos($query, $this->configuration->getMigrationsTableName()) !== false) { |
|
49
|
6 |
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
if ($formatted) { |
|
53
|
4 |
|
$maxLength = $lineLength - 18 - 8; // max - php code length - indentation |
|
54
|
|
|
|
|
55
|
4 |
|
if (strlen($query) > $maxLength) { |
|
56
|
4 |
|
$query = SqlFormatter::format($query, false); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
8 |
|
$code[] = sprintf('$this->addSql(%s);', var_export($query, true)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
8 |
|
if (count($code) !== 0 && $checkDbPlatform && $this->configuration->isDatabasePlatformChecked()) { |
|
64
|
6 |
|
$currentPlatform = $this->platform->getName(); |
|
65
|
|
|
|
|
66
|
6 |
|
array_unshift( |
|
67
|
6 |
|
$code, |
|
68
|
6 |
|
sprintf( |
|
69
|
6 |
|
'$this->abortIf($this->connection->getDatabasePlatform()->getName() !== %s, %s);', |
|
70
|
6 |
|
var_export($currentPlatform, true), |
|
71
|
6 |
|
var_export(sprintf("Migration can only be executed safely on '%s'.", $currentPlatform), true) |
|
72
|
|
|
), |
|
73
|
6 |
|
'' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
8 |
|
return implode("\n", $code); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|