forked from brainbits/functional-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMysqlBasedSchemaStrategy.php
More file actions
152 lines (121 loc) · 4.68 KB
/
MysqlBasedSchemaStrategy.php
File metadata and controls
152 lines (121 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
namespace Brainbits\FunctionalTestHelpers\Schema\Strategy;
use ArrayObject;
use Brainbits\FunctionalTestHelpers\Schema\DataBuilder;
use Brainbits\FunctionalTestHelpers\Schema\SchemaBuilder;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Identifier;
use function count;
use function sprintf;
final class MysqlBasedSchemaStrategy implements SchemaStrategy
{
/** @var ArrayObject<string, int|string> */
public static ArrayObject $executedStatements;
public function __construct(bool $resetExecutedStatements = false)
{
if ($resetExecutedStatements) {
self::$executedStatements = new ArrayObject();
}
self::$executedStatements ??= new ArrayObject();
}
public function applySchema(SchemaBuilder $schemaBuilder, Connection $connection): void
{
$platform = $connection->getDatabasePlatform();
$existingTables = $connection->createSchemaManager()->listTableNames();
if (count($existingTables) > 0) {
if (count(self::$executedStatements) === 0) {
$this->dropTables($existingTables, $connection);
self::$executedStatements = new ArrayObject();
}
}
foreach ($schemaBuilder->getSchema()->toSql($platform) as $sql) {
if (self::$executedStatements->offsetExists($sql)) {
continue;
}
self::$executedStatements[$sql] = $connection->executeStatement($sql);
}
}
public function deleteData(Connection $connection): void
{
$tablesWithRows = $connection->executeQuery(
'SELECT `TABLE_NAME` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = :table',
['table' => $connection->getDatabase()],
)->fetchFirstColumn();
if (!$tablesWithRows) {
return;
}
try {
$connection->executeStatement('SET foreign_key_checks = 0');
foreach ($tablesWithRows as $table) {
$connection->executeStatement(
sprintf('DELETE FROM %s', $connection->quoteIdentifier($table)),
);
}
} finally {
$connection->executeStatement('SET foreign_key_checks = 1');
}
}
public function resetSequences(Connection $connection): void
{
$tablesWithAutoIncrements = $connection->executeQuery(
'SELECT `TABLE_NAME` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = :table AND `AUTO_INCREMENT` > 1',
['table' => $connection->getDatabase()],
)->fetchFirstColumn();
foreach ($tablesWithAutoIncrements as $table) {
$connection->executeStatement(
sprintf('ALTER TABLE %s AUTO_INCREMENT = 1', $connection->quoteIdentifier($table)),
);
}
}
public function applyData(DataBuilder $dataBuilder, Connection $connection): void
{
try {
$connection->executeStatement('SET foreign_key_checks = 0');
foreach ($dataBuilder->getData() as $table => $rows) {
$table = $this->quoteIdentifier($connection, $table);
foreach ($rows as $row) {
$row = $this->quoteKeys($connection, $row);
$connection->insert($table, $row);
}
}
} finally {
$connection->executeStatement('SET foreign_key_checks = 1');
}
}
/** @param list<string> $tableNames */
private function dropTables(array $tableNames, Connection $connection): void
{
$schemaManager = $connection->createSchemaManager();
try {
$connection->executeStatement('SET foreign_key_checks = 0');
foreach ($tableNames as $tableName) {
foreach ($schemaManager->listTableForeignKeys($tableName) as $foreignKey) {
$schemaManager->dropForeignKey($foreignKey->getName(), $tableName);
}
$schemaManager->dropTable($tableName);
}
} finally {
$connection->executeStatement('SET foreign_key_checks = 1');
}
}
private function quoteIdentifier(Connection $connection, string $identifier): string
{
return (new Identifier($identifier, true))->getQuotedName($connection->getDatabasePlatform());
}
/**
* @param array<string, mixed> $row
*
* @return array<string, mixed>
*
* @interal
*/
private function quoteKeys(Connection $connection, mixed $row): array
{
$quotedRow = [];
foreach ($row as $key => $value) {
$quotedRow[$this->quoteIdentifier($connection, $key)] = $value;
}
return $quotedRow;
}
}