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