1 | <?php |
||
2 | |||
3 | namespace Doctrine\Tests\DBAL\Functional\Schema; |
||
4 | |||
5 | use Doctrine\Common\EventManager; |
||
6 | use Doctrine\DBAL\DBALException; |
||
7 | use Doctrine\DBAL\Driver\Connection; |
||
8 | use Doctrine\DBAL\Events; |
||
9 | use Doctrine\DBAL\Platforms\OraclePlatform; |
||
10 | use Doctrine\DBAL\Schema\AbstractAsset; |
||
11 | use Doctrine\DBAL\Schema\AbstractSchemaManager; |
||
12 | use Doctrine\DBAL\Schema\Column; |
||
13 | use Doctrine\DBAL\Schema\ColumnDiff; |
||
14 | use Doctrine\DBAL\Schema\Comparator; |
||
15 | use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
||
16 | use Doctrine\DBAL\Schema\Index; |
||
17 | use Doctrine\DBAL\Schema\SchemaDiff; |
||
18 | use Doctrine\DBAL\Schema\Sequence; |
||
19 | use Doctrine\DBAL\Schema\Table; |
||
20 | use Doctrine\DBAL\Schema\TableDiff; |
||
21 | use Doctrine\DBAL\Schema\View; |
||
22 | use Doctrine\DBAL\Types\ArrayType; |
||
23 | use Doctrine\DBAL\Types\BinaryType; |
||
24 | use Doctrine\DBAL\Types\DateIntervalType; |
||
25 | use Doctrine\DBAL\Types\DateTimeType; |
||
26 | use Doctrine\DBAL\Types\DecimalType; |
||
27 | use Doctrine\DBAL\Types\IntegerType; |
||
28 | use Doctrine\DBAL\Types\ObjectType; |
||
29 | use Doctrine\DBAL\Types\StringType; |
||
30 | use Doctrine\DBAL\Types\TextType; |
||
31 | use Doctrine\DBAL\Types\Type; |
||
32 | use Doctrine\Tests\DbalFunctionalTestCase; |
||
33 | use function array_filter; |
||
34 | use function array_keys; |
||
35 | use function array_map; |
||
36 | use function array_search; |
||
37 | use function array_values; |
||
38 | use function count; |
||
39 | use function current; |
||
40 | use function end; |
||
41 | use function explode; |
||
42 | use function in_array; |
||
43 | use function str_replace; |
||
44 | use function strcasecmp; |
||
45 | use function strlen; |
||
46 | use function strtolower; |
||
47 | use function substr; |
||
48 | |||
49 | class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase |
||
50 | { |
||
51 | /** @var AbstractSchemaManager */ |
||
52 | protected $schemaManager; |
||
53 | |||
54 | protected function getPlatformName() |
||
55 | { |
||
56 | $class = static::class; |
||
57 | $e = explode('\\', $class); |
||
58 | $testClass = end($e); |
||
59 | return strtolower(str_replace('SchemaManagerTest', null, $testClass)); |
||
60 | } |
||
61 | |||
62 | protected function setUp() : void |
||
63 | { |
||
64 | parent::setUp(); |
||
65 | |||
66 | $dbms = $this->getPlatformName(); |
||
67 | |||
68 | if ($this->connection->getDatabasePlatform()->getName() !== $dbms) { |
||
69 | $this->markTestSkipped(static::class . ' requires the use of ' . $dbms); |
||
70 | } |
||
71 | |||
72 | $this->schemaManager = $this->connection->getSchemaManager(); |
||
73 | } |
||
74 | |||
75 | |||
76 | protected function tearDown() : void |
||
77 | { |
||
78 | parent::tearDown(); |
||
79 | |||
80 | $this->schemaManager->tryMethod('dropTable', 'testschema.my_table_in_namespace'); |
||
81 | |||
82 | //TODO: SchemaDiff does not drop removed namespaces? |
||
83 | try { |
||
84 | //sql server versions below 2016 do not support 'IF EXISTS' so we have to catch the exception here |
||
85 | $this->connection->exec('DROP SCHEMA testschema'); |
||
86 | } catch (DBALException $e) { |
||
87 | return; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | |||
92 | /** |
||
93 | * @group DBAL-1220 |
||
94 | */ |
||
95 | public function testDropsDatabaseWithActiveConnections() |
||
96 | { |
||
97 | if (! $this->schemaManager->getDatabasePlatform()->supportsCreateDropDatabase()) { |
||
98 | $this->markTestSkipped('Cannot drop Database client side with this Driver.'); |
||
99 | } |
||
100 | |||
101 | $this->schemaManager->dropAndCreateDatabase('test_drop_database'); |
||
102 | |||
103 | $knownDatabases = $this->schemaManager->listDatabases(); |
||
104 | if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
105 | self::assertContains('TEST_DROP_DATABASE', $knownDatabases); |
||
106 | } else { |
||
107 | self::assertContains('test_drop_database', $knownDatabases); |
||
108 | } |
||
109 | |||
110 | $params = $this->connection->getParams(); |
||
111 | if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
112 | $params['user'] = 'test_drop_database'; |
||
113 | } else { |
||
114 | $params['dbname'] = 'test_drop_database'; |
||
115 | } |
||
116 | |||
117 | $user = $params['user'] ?? null; |
||
118 | $password = $params['password'] ?? null; |
||
119 | |||
120 | $connection = $this->connection->getDriver()->connect($params, $user, $password); |
||
121 | |||
122 | self::assertInstanceOf(Connection::class, $connection); |
||
123 | |||
124 | $this->schemaManager->dropDatabase('test_drop_database'); |
||
125 | |||
126 | self::assertNotContains('test_drop_database', $this->schemaManager->listDatabases()); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @group DBAL-195 |
||
131 | */ |
||
132 | public function testDropAndCreateSequence() |
||
133 | { |
||
134 | if (! $this->connection->getDatabasePlatform()->supportsSequences()) { |
||
135 | $this->markTestSkipped($this->connection->getDriver()->getName() . ' does not support sequences.'); |
||
136 | } |
||
137 | |||
138 | $name = 'dropcreate_sequences_test_seq'; |
||
139 | |||
140 | $this->schemaManager->dropAndCreateSequence(new Sequence($name, 20, 10)); |
||
141 | |||
142 | self::assertTrue($this->hasElementWithName($this->schemaManager->listSequences(), $name)); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param AbstractAsset[] $items |
||
147 | */ |
||
148 | private function hasElementWithName(array $items, string $name) : bool |
||
149 | { |
||
150 | $filteredList = array_filter( |
||
151 | $items, |
||
152 | static function (AbstractAsset $item) use ($name) : bool { |
||
153 | return $item->getShortestName($item->getNamespaceName()) === $name; |
||
154 | } |
||
155 | ); |
||
156 | |||
157 | return count($filteredList) === 1; |
||
158 | } |
||
159 | |||
160 | public function testListSequences() |
||
161 | { |
||
162 | if (! $this->connection->getDatabasePlatform()->supportsSequences()) { |
||
163 | $this->markTestSkipped($this->connection->getDriver()->getName() . ' does not support sequences.'); |
||
164 | } |
||
165 | |||
166 | $sequence = new Sequence('list_sequences_test_seq', 20, 10); |
||
167 | $this->schemaManager->createSequence($sequence); |
||
168 | |||
169 | $sequences = $this->schemaManager->listSequences(); |
||
170 | |||
171 | self::assertIsArray($sequences, 'listSequences() should return an array.'); |
||
172 | |||
173 | $foundSequence = null; |
||
174 | foreach ($sequences as $sequence) { |
||
175 | self::assertInstanceOf(Sequence::class, $sequence, 'Array elements of listSequences() should be Sequence instances.'); |
||
176 | if (strtolower($sequence->getName()) !== 'list_sequences_test_seq') { |
||
177 | continue; |
||
178 | } |
||
179 | |||
180 | $foundSequence = $sequence; |
||
181 | } |
||
182 | |||
183 | self::assertNotNull($foundSequence, "Sequence with name 'list_sequences_test_seq' was not found."); |
||
184 | self::assertSame(20, $foundSequence->getAllocationSize(), 'Allocation Size is expected to be 20.'); |
||
185 | self::assertSame(10, $foundSequence->getInitialValue(), 'Initial Value is expected to be 10.'); |
||
186 | } |
||
187 | |||
188 | public function testListDatabases() |
||
189 | { |
||
190 | if (! $this->schemaManager->getDatabasePlatform()->supportsCreateDropDatabase()) { |
||
191 | $this->markTestSkipped('Cannot drop Database client side with this Driver.'); |
||
192 | } |
||
193 | |||
194 | $this->schemaManager->dropAndCreateDatabase('test_create_database'); |
||
195 | $databases = $this->schemaManager->listDatabases(); |
||
196 | |||
197 | $databases = array_map('strtolower', $databases); |
||
198 | |||
199 | self::assertContains('test_create_database', $databases); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @group DBAL-1058 |
||
204 | */ |
||
205 | public function testListNamespaceNames() |
||
206 | { |
||
207 | if (! $this->schemaManager->getDatabasePlatform()->supportsSchemas()) { |
||
208 | $this->markTestSkipped('Platform does not support schemas.'); |
||
209 | } |
||
210 | |||
211 | // Currently dropping schemas is not supported, so we have to workaround here. |
||
212 | $namespaces = $this->schemaManager->listNamespaceNames(); |
||
213 | $namespaces = array_map('strtolower', $namespaces); |
||
214 | |||
215 | if (! in_array('test_create_schema', $namespaces)) { |
||
216 | $this->connection->executeUpdate($this->schemaManager->getDatabasePlatform()->getCreateSchemaSQL('test_create_schema')); |
||
217 | |||
218 | $namespaces = $this->schemaManager->listNamespaceNames(); |
||
219 | $namespaces = array_map('strtolower', $namespaces); |
||
220 | } |
||
221 | |||
222 | self::assertContains('test_create_schema', $namespaces); |
||
223 | } |
||
224 | |||
225 | public function testListTables() |
||
226 | { |
||
227 | $this->createTestTable('list_tables_test'); |
||
228 | $tables = $this->schemaManager->listTables(); |
||
229 | |||
230 | self::assertIsArray($tables); |
||
231 | self::assertTrue(count($tables) > 0, "List Tables has to find at least one table named 'list_tables_test'."); |
||
232 | |||
233 | $foundTable = false; |
||
234 | foreach ($tables as $table) { |
||
235 | self::assertInstanceOf(Table::class, $table); |
||
236 | if (strtolower($table->getName()) !== 'list_tables_test') { |
||
237 | continue; |
||
238 | } |
||
239 | |||
240 | $foundTable = true; |
||
241 | |||
242 | self::assertTrue($table->hasColumn('id')); |
||
243 | self::assertTrue($table->hasColumn('test')); |
||
244 | self::assertTrue($table->hasColumn('foreign_key_test')); |
||
245 | } |
||
246 | |||
247 | self::assertTrue($foundTable, "The 'list_tables_test' table has to be found."); |
||
248 | } |
||
249 | |||
250 | public function createListTableColumns() |
||
251 | { |
||
252 | $table = new Table('list_table_columns'); |
||
253 | $table->addColumn('id', 'integer', ['notnull' => true]); |
||
254 | $table->addColumn('test', 'string', ['length' => 255, 'notnull' => false, 'default' => 'expected default']); |
||
255 | $table->addColumn('foo', 'text', ['notnull' => true]); |
||
256 | $table->addColumn('bar', 'decimal', ['precision' => 10, 'scale' => 4, 'notnull' => false]); |
||
257 | $table->addColumn('baz1', 'datetime'); |
||
258 | $table->addColumn('baz2', 'time'); |
||
259 | $table->addColumn('baz3', 'date'); |
||
260 | $table->setPrimaryKey(['id']); |
||
261 | |||
262 | return $table; |
||
263 | } |
||
264 | |||
265 | public function testListTableColumns() |
||
266 | { |
||
267 | $table = $this->createListTableColumns(); |
||
268 | |||
269 | $this->schemaManager->dropAndCreateTable($table); |
||
270 | |||
271 | $columns = $this->schemaManager->listTableColumns('list_table_columns'); |
||
272 | $columnsKeys = array_keys($columns); |
||
273 | |||
274 | self::assertArrayHasKey('id', $columns); |
||
275 | self::assertEquals(0, array_search('id', $columnsKeys)); |
||
276 | self::assertEquals('id', strtolower($columns['id']->getname())); |
||
277 | self::assertInstanceOf(IntegerType::class, $columns['id']->gettype()); |
||
278 | self::assertEquals(false, $columns['id']->getunsigned()); |
||
279 | self::assertEquals(true, $columns['id']->getnotnull()); |
||
280 | self::assertEquals(null, $columns['id']->getdefault()); |
||
281 | self::assertIsArray($columns['id']->getPlatformOptions()); |
||
282 | |||
283 | self::assertArrayHasKey('test', $columns); |
||
284 | self::assertEquals(1, array_search('test', $columnsKeys)); |
||
285 | self::assertEquals('test', strtolower($columns['test']->getname())); |
||
286 | self::assertInstanceOf(StringType::class, $columns['test']->gettype()); |
||
287 | self::assertEquals(255, $columns['test']->getlength()); |
||
288 | self::assertEquals(false, $columns['test']->getfixed()); |
||
289 | self::assertEquals(false, $columns['test']->getnotnull()); |
||
290 | self::assertEquals('expected default', $columns['test']->getdefault()); |
||
291 | self::assertIsArray($columns['test']->getPlatformOptions()); |
||
292 | |||
293 | self::assertEquals('foo', strtolower($columns['foo']->getname())); |
||
294 | self::assertEquals(2, array_search('foo', $columnsKeys)); |
||
295 | self::assertInstanceOf(TextType::class, $columns['foo']->gettype()); |
||
296 | self::assertEquals(false, $columns['foo']->getunsigned()); |
||
297 | self::assertEquals(false, $columns['foo']->getfixed()); |
||
298 | self::assertEquals(true, $columns['foo']->getnotnull()); |
||
299 | self::assertEquals(null, $columns['foo']->getdefault()); |
||
300 | self::assertIsArray($columns['foo']->getPlatformOptions()); |
||
301 | |||
302 | self::assertEquals('bar', strtolower($columns['bar']->getname())); |
||
303 | self::assertEquals(3, array_search('bar', $columnsKeys)); |
||
304 | self::assertInstanceOf(DecimalType::class, $columns['bar']->gettype()); |
||
305 | self::assertEquals(null, $columns['bar']->getlength()); |
||
306 | self::assertEquals(10, $columns['bar']->getprecision()); |
||
307 | self::assertEquals(4, $columns['bar']->getscale()); |
||
308 | self::assertEquals(false, $columns['bar']->getunsigned()); |
||
309 | self::assertEquals(false, $columns['bar']->getfixed()); |
||
310 | self::assertEquals(false, $columns['bar']->getnotnull()); |
||
311 | self::assertEquals(null, $columns['bar']->getdefault()); |
||
312 | self::assertIsArray($columns['bar']->getPlatformOptions()); |
||
313 | |||
314 | self::assertEquals('baz1', strtolower($columns['baz1']->getname())); |
||
315 | self::assertEquals(4, array_search('baz1', $columnsKeys)); |
||
316 | self::assertInstanceOf(DateTimeType::class, $columns['baz1']->gettype()); |
||
317 | self::assertEquals(true, $columns['baz1']->getnotnull()); |
||
318 | self::assertEquals(null, $columns['baz1']->getdefault()); |
||
319 | self::assertIsArray($columns['baz1']->getPlatformOptions()); |
||
320 | |||
321 | self::assertEquals('baz2', strtolower($columns['baz2']->getname())); |
||
322 | self::assertEquals(5, array_search('baz2', $columnsKeys)); |
||
323 | self::assertContains($columns['baz2']->gettype()->getName(), ['time', 'date', 'datetime']); |
||
324 | self::assertEquals(true, $columns['baz2']->getnotnull()); |
||
325 | self::assertEquals(null, $columns['baz2']->getdefault()); |
||
326 | self::assertIsArray($columns['baz2']->getPlatformOptions()); |
||
327 | |||
328 | self::assertEquals('baz3', strtolower($columns['baz3']->getname())); |
||
329 | self::assertEquals(6, array_search('baz3', $columnsKeys)); |
||
330 | self::assertContains($columns['baz3']->gettype()->getName(), ['time', 'date', 'datetime']); |
||
331 | self::assertEquals(true, $columns['baz3']->getnotnull()); |
||
332 | self::assertEquals(null, $columns['baz3']->getdefault()); |
||
333 | self::assertIsArray($columns['baz3']->getPlatformOptions()); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @group DBAL-1078 |
||
338 | */ |
||
339 | public function testListTableColumnsWithFixedStringColumn() |
||
340 | { |
||
341 | $tableName = 'test_list_table_fixed_string'; |
||
342 | |||
343 | $table = new Table($tableName); |
||
344 | $table->addColumn('column_char', 'string', ['fixed' => true, 'length' => 2]); |
||
345 | |||
346 | $this->schemaManager->createTable($table); |
||
347 | |||
348 | $columns = $this->schemaManager->listTableColumns($tableName); |
||
349 | |||
350 | self::assertArrayHasKey('column_char', $columns); |
||
351 | self::assertInstanceOf(StringType::class, $columns['column_char']->getType()); |
||
352 | self::assertTrue($columns['column_char']->getFixed()); |
||
353 | self::assertSame(2, $columns['column_char']->getLength()); |
||
354 | } |
||
355 | |||
356 | public function testListTableColumnsDispatchEvent() |
||
357 | { |
||
358 | $table = $this->createListTableColumns(); |
||
359 | |||
360 | $this->schemaManager->dropAndCreateTable($table); |
||
361 | |||
362 | $listenerMock = $this |
||
363 | ->getMockBuilder('ListTableColumnsDispatchEventListener') |
||
364 | ->setMethods(['onSchemaColumnDefinition']) |
||
365 | ->getMock(); |
||
366 | $listenerMock |
||
367 | ->expects($this->exactly(7)) |
||
368 | ->method('onSchemaColumnDefinition'); |
||
369 | |||
370 | $oldEventManager = $this->schemaManager->getDatabasePlatform()->getEventManager(); |
||
371 | |||
372 | $eventManager = new EventManager(); |
||
373 | $eventManager->addEventListener([Events::onSchemaColumnDefinition], $listenerMock); |
||
374 | |||
375 | $this->schemaManager->getDatabasePlatform()->setEventManager($eventManager); |
||
376 | |||
377 | $this->schemaManager->listTableColumns('list_table_columns'); |
||
378 | |||
379 | $this->schemaManager->getDatabasePlatform()->setEventManager($oldEventManager); |
||
380 | } |
||
381 | |||
382 | public function testListTableIndexesDispatchEvent() |
||
383 | { |
||
384 | $table = $this->getTestTable('list_table_indexes_test'); |
||
385 | $table->addUniqueIndex(['test'], 'test_index_name'); |
||
386 | $table->addIndex(['id', 'test'], 'test_composite_idx'); |
||
387 | |||
388 | $this->schemaManager->dropAndCreateTable($table); |
||
389 | |||
390 | $listenerMock = $this |
||
391 | ->getMockBuilder('ListTableIndexesDispatchEventListener') |
||
392 | ->setMethods(['onSchemaIndexDefinition']) |
||
393 | ->getMock(); |
||
394 | $listenerMock |
||
395 | ->expects($this->exactly(3)) |
||
396 | ->method('onSchemaIndexDefinition'); |
||
397 | |||
398 | $oldEventManager = $this->schemaManager->getDatabasePlatform()->getEventManager(); |
||
399 | |||
400 | $eventManager = new EventManager(); |
||
401 | $eventManager->addEventListener([Events::onSchemaIndexDefinition], $listenerMock); |
||
402 | |||
403 | $this->schemaManager->getDatabasePlatform()->setEventManager($eventManager); |
||
404 | |||
405 | $this->schemaManager->listTableIndexes('list_table_indexes_test'); |
||
406 | |||
407 | $this->schemaManager->getDatabasePlatform()->setEventManager($oldEventManager); |
||
408 | } |
||
409 | |||
410 | public function testDiffListTableColumns() |
||
411 | { |
||
412 | if ($this->schemaManager->getDatabasePlatform()->getName() === 'oracle') { |
||
413 | $this->markTestSkipped('Does not work with Oracle, since it cannot detect DateTime, Date and Time differenecs (at the moment).'); |
||
414 | } |
||
415 | |||
416 | $offlineTable = $this->createListTableColumns(); |
||
417 | $this->schemaManager->dropAndCreateTable($offlineTable); |
||
418 | $onlineTable = $this->schemaManager->listTableDetails('list_table_columns'); |
||
419 | |||
420 | $comparator = new Comparator(); |
||
421 | $diff = $comparator->diffTable($offlineTable, $onlineTable); |
||
422 | |||
423 | self::assertFalse($diff, 'No differences should be detected with the offline vs online schema.'); |
||
424 | } |
||
425 | |||
426 | public function testListTableIndexes() |
||
427 | { |
||
428 | $table = $this->getTestCompositeTable('list_table_indexes_test'); |
||
429 | $table->addUniqueIndex(['test'], 'test_index_name'); |
||
430 | $table->addIndex(['id', 'test'], 'test_composite_idx'); |
||
431 | |||
432 | $this->schemaManager->dropAndCreateTable($table); |
||
433 | |||
434 | $tableIndexes = $this->schemaManager->listTableIndexes('list_table_indexes_test'); |
||
435 | |||
436 | self::assertEquals(3, count($tableIndexes)); |
||
437 | |||
438 | self::assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.'); |
||
439 | self::assertEquals(['id', 'other_id'], array_map('strtolower', $tableIndexes['primary']->getColumns())); |
||
440 | self::assertTrue($tableIndexes['primary']->isUnique()); |
||
441 | self::assertTrue($tableIndexes['primary']->isPrimary()); |
||
442 | |||
443 | self::assertEquals('test_index_name', strtolower($tableIndexes['test_index_name']->getName())); |
||
444 | self::assertEquals(['test'], array_map('strtolower', $tableIndexes['test_index_name']->getColumns())); |
||
445 | self::assertTrue($tableIndexes['test_index_name']->isUnique()); |
||
446 | self::assertFalse($tableIndexes['test_index_name']->isPrimary()); |
||
447 | |||
448 | self::assertEquals('test_composite_idx', strtolower($tableIndexes['test_composite_idx']->getName())); |
||
449 | self::assertEquals(['id', 'test'], array_map('strtolower', $tableIndexes['test_composite_idx']->getColumns())); |
||
450 | self::assertFalse($tableIndexes['test_composite_idx']->isUnique()); |
||
451 | self::assertFalse($tableIndexes['test_composite_idx']->isPrimary()); |
||
452 | } |
||
453 | |||
454 | public function testDropAndCreateIndex() |
||
455 | { |
||
456 | $table = $this->getTestTable('test_create_index'); |
||
457 | $table->addUniqueIndex(['test'], 'test'); |
||
458 | $this->schemaManager->dropAndCreateTable($table); |
||
459 | |||
460 | $this->schemaManager->dropAndCreateIndex($table->getIndex('test'), $table); |
||
461 | $tableIndexes = $this->schemaManager->listTableIndexes('test_create_index'); |
||
462 | self::assertIsArray($tableIndexes); |
||
463 | |||
464 | self::assertEquals('test', strtolower($tableIndexes['test']->getName())); |
||
465 | self::assertEquals(['test'], array_map('strtolower', $tableIndexes['test']->getColumns())); |
||
466 | self::assertTrue($tableIndexes['test']->isUnique()); |
||
467 | self::assertFalse($tableIndexes['test']->isPrimary()); |
||
468 | } |
||
469 | |||
470 | public function testCreateTableWithForeignKeys() |
||
471 | { |
||
472 | if (! $this->schemaManager->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
||
473 | $this->markTestSkipped('Platform does not support foreign keys.'); |
||
474 | } |
||
475 | |||
476 | $tableB = $this->getTestTable('test_foreign'); |
||
477 | |||
478 | $this->schemaManager->dropAndCreateTable($tableB); |
||
479 | |||
480 | $tableA = $this->getTestTable('test_create_fk'); |
||
481 | $tableA->addForeignKeyConstraint('test_foreign', ['foreign_key_test'], ['id']); |
||
482 | |||
483 | $this->schemaManager->dropAndCreateTable($tableA); |
||
484 | |||
485 | $fkTable = $this->schemaManager->listTableDetails('test_create_fk'); |
||
486 | $fkConstraints = $fkTable->getForeignKeys(); |
||
487 | self::assertEquals(1, count($fkConstraints), "Table 'test_create_fk1' has to have one foreign key."); |
||
488 | |||
489 | $fkConstraint = current($fkConstraints); |
||
490 | self::assertInstanceOf(ForeignKeyConstraint::class, $fkConstraint); |
||
491 | self::assertEquals('test_foreign', strtolower($fkConstraint->getForeignTableName())); |
||
492 | self::assertEquals(['foreign_key_test'], array_map('strtolower', $fkConstraint->getColumns())); |
||
493 | self::assertEquals(['id'], array_map('strtolower', $fkConstraint->getForeignColumns())); |
||
494 | |||
495 | self::assertTrue($fkTable->columnsAreIndexed($fkConstraint->getColumns()), 'The columns of a foreign key constraint should always be indexed.'); |
||
496 | } |
||
497 | |||
498 | public function testListForeignKeys() |
||
499 | { |
||
500 | if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
||
501 | $this->markTestSkipped('Does not support foreign key constraints.'); |
||
502 | } |
||
503 | |||
504 | $this->createTestTable('test_create_fk1'); |
||
505 | $this->createTestTable('test_create_fk2'); |
||
506 | |||
507 | $foreignKey = new ForeignKeyConstraint( |
||
508 | ['foreign_key_test'], |
||
509 | 'test_create_fk2', |
||
510 | ['id'], |
||
511 | 'foreign_key_test_fk', |
||
512 | ['onDelete' => 'CASCADE'] |
||
513 | ); |
||
514 | |||
515 | $this->schemaManager->createForeignKey($foreignKey, 'test_create_fk1'); |
||
516 | |||
517 | $fkeys = $this->schemaManager->listTableForeignKeys('test_create_fk1'); |
||
518 | |||
519 | self::assertEquals(1, count($fkeys), "Table 'test_create_fk1' has to have one foreign key."); |
||
520 | |||
521 | self::assertInstanceOf(ForeignKeyConstraint::class, $fkeys[0]); |
||
522 | self::assertEquals(['foreign_key_test'], array_map('strtolower', $fkeys[0]->getLocalColumns())); |
||
523 | self::assertEquals(['id'], array_map('strtolower', $fkeys[0]->getForeignColumns())); |
||
524 | self::assertEquals('test_create_fk2', strtolower($fkeys[0]->getForeignTableName())); |
||
525 | |||
526 | if (! $fkeys[0]->hasOption('onDelete')) { |
||
527 | return; |
||
528 | } |
||
529 | |||
530 | self::assertEquals('CASCADE', $fkeys[0]->getOption('onDelete')); |
||
531 | } |
||
532 | |||
533 | protected function getCreateExampleViewSql() |
||
534 | { |
||
535 | $this->markTestSkipped('No Create Example View SQL was defined for this SchemaManager'); |
||
536 | } |
||
537 | |||
538 | public function testCreateSchema() |
||
539 | { |
||
540 | $this->createTestTable('test_table'); |
||
541 | |||
542 | $schema = $this->schemaManager->createSchema(); |
||
543 | self::assertTrue($schema->hasTable('test_table')); |
||
544 | } |
||
545 | |||
546 | public function testAlterTableScenario() |
||
547 | { |
||
548 | if (! $this->schemaManager->getDatabasePlatform()->supportsAlterTable()) { |
||
549 | $this->markTestSkipped('Alter Table is not supported by this platform.'); |
||
550 | } |
||
551 | |||
552 | $alterTable = $this->createTestTable('alter_table'); |
||
553 | $this->createTestTable('alter_table_foreign'); |
||
554 | |||
555 | $table = $this->schemaManager->listTableDetails('alter_table'); |
||
556 | self::assertTrue($table->hasColumn('id')); |
||
557 | self::assertTrue($table->hasColumn('test')); |
||
558 | self::assertTrue($table->hasColumn('foreign_key_test')); |
||
559 | self::assertEquals(0, count($table->getForeignKeys())); |
||
560 | self::assertEquals(1, count($table->getIndexes())); |
||
561 | |||
562 | $tableDiff = new TableDiff('alter_table'); |
||
563 | $tableDiff->fromTable = $alterTable; |
||
564 | $tableDiff->addedColumns['foo'] = new Column('foo', Type::getType('integer')); |
||
565 | $tableDiff->removedColumns['test'] = $table->getColumn('test'); |
||
566 | |||
567 | $this->schemaManager->alterTable($tableDiff); |
||
568 | |||
569 | $table = $this->schemaManager->listTableDetails('alter_table'); |
||
570 | self::assertFalse($table->hasColumn('test')); |
||
571 | self::assertTrue($table->hasColumn('foo')); |
||
572 | |||
573 | $tableDiff = new TableDiff('alter_table'); |
||
574 | $tableDiff->fromTable = $table; |
||
575 | $tableDiff->addedIndexes[] = new Index('foo_idx', ['foo']); |
||
576 | |||
577 | $this->schemaManager->alterTable($tableDiff); |
||
578 | |||
579 | $table = $this->schemaManager->listTableDetails('alter_table'); |
||
580 | self::assertEquals(2, count($table->getIndexes())); |
||
581 | self::assertTrue($table->hasIndex('foo_idx')); |
||
582 | self::assertEquals(['foo'], array_map('strtolower', $table->getIndex('foo_idx')->getColumns())); |
||
583 | self::assertFalse($table->getIndex('foo_idx')->isPrimary()); |
||
584 | self::assertFalse($table->getIndex('foo_idx')->isUnique()); |
||
585 | |||
586 | $tableDiff = new TableDiff('alter_table'); |
||
587 | $tableDiff->fromTable = $table; |
||
588 | $tableDiff->changedIndexes[] = new Index('foo_idx', ['foo', 'foreign_key_test']); |
||
589 | |||
590 | $this->schemaManager->alterTable($tableDiff); |
||
591 | |||
592 | $table = $this->schemaManager->listTableDetails('alter_table'); |
||
593 | self::assertEquals(2, count($table->getIndexes())); |
||
594 | self::assertTrue($table->hasIndex('foo_idx')); |
||
595 | self::assertEquals(['foo', 'foreign_key_test'], array_map('strtolower', $table->getIndex('foo_idx')->getColumns())); |
||
596 | |||
597 | $tableDiff = new TableDiff('alter_table'); |
||
598 | $tableDiff->fromTable = $table; |
||
599 | $tableDiff->renamedIndexes['foo_idx'] = new Index('bar_idx', ['foo', 'foreign_key_test']); |
||
600 | |||
601 | $this->schemaManager->alterTable($tableDiff); |
||
602 | |||
603 | $table = $this->schemaManager->listTableDetails('alter_table'); |
||
604 | self::assertEquals(2, count($table->getIndexes())); |
||
605 | self::assertTrue($table->hasIndex('bar_idx')); |
||
606 | self::assertFalse($table->hasIndex('foo_idx')); |
||
607 | self::assertEquals(['foo', 'foreign_key_test'], array_map('strtolower', $table->getIndex('bar_idx')->getColumns())); |
||
608 | self::assertFalse($table->getIndex('bar_idx')->isPrimary()); |
||
609 | self::assertFalse($table->getIndex('bar_idx')->isUnique()); |
||
610 | |||
611 | $tableDiff = new TableDiff('alter_table'); |
||
612 | $tableDiff->fromTable = $table; |
||
613 | $tableDiff->removedIndexes[] = new Index('bar_idx', ['foo', 'foreign_key_test']); |
||
614 | $fk = new ForeignKeyConstraint(['foreign_key_test'], 'alter_table_foreign', ['id']); |
||
615 | $tableDiff->addedForeignKeys[] = $fk; |
||
616 | |||
617 | $this->schemaManager->alterTable($tableDiff); |
||
618 | $table = $this->schemaManager->listTableDetails('alter_table'); |
||
619 | |||
620 | // dont check for index size here, some platforms automatically add indexes for foreign keys. |
||
621 | self::assertFalse($table->hasIndex('bar_idx')); |
||
622 | |||
623 | if (! $this->schemaManager->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
||
624 | return; |
||
625 | } |
||
626 | |||
627 | $fks = $table->getForeignKeys(); |
||
628 | self::assertCount(1, $fks); |
||
629 | $foreignKey = current($fks); |
||
630 | self::assertEquals('alter_table_foreign', strtolower($foreignKey->getForeignTableName())); |
||
631 | self::assertEquals(['foreign_key_test'], array_map('strtolower', $foreignKey->getColumns())); |
||
632 | self::assertEquals(['id'], array_map('strtolower', $foreignKey->getForeignColumns())); |
||
633 | } |
||
634 | |||
635 | |||
636 | public function testTableInNamespace() |
||
637 | { |
||
638 | if (! $this->schemaManager->getDatabasePlatform()->supportsSchemas()) { |
||
639 | $this->markTestSkipped('Schema definition is not supported by this platform.'); |
||
640 | } |
||
641 | |||
642 | //create schema |
||
643 | $diff = new SchemaDiff(); |
||
644 | $diff->newNamespaces[] = 'testschema'; |
||
645 | |||
646 | foreach ($diff->toSql($this->schemaManager->getDatabasePlatform()) as $sql) { |
||
647 | $this->connection->exec($sql); |
||
648 | } |
||
649 | |||
650 | //test if table is create in namespace |
||
651 | $this->createTestTable('testschema.my_table_in_namespace'); |
||
652 | self::assertContains('testschema.my_table_in_namespace', $this->schemaManager->listTableNames()); |
||
653 | |||
654 | //tables without namespace should be created in default namespace |
||
655 | //default namespaces are ignored in table listings |
||
656 | $this->createTestTable('my_table_not_in_namespace'); |
||
657 | self::assertContains('my_table_not_in_namespace', $this->schemaManager->listTableNames()); |
||
658 | } |
||
659 | |||
660 | public function testCreateAndListViews() |
||
661 | { |
||
662 | if (! $this->schemaManager->getDatabasePlatform()->supportsViews()) { |
||
663 | $this->markTestSkipped('Views is not supported by this platform.'); |
||
664 | } |
||
665 | |||
666 | $this->createTestTable('view_test_table'); |
||
667 | |||
668 | $name = 'doctrine_test_view'; |
||
669 | $sql = 'SELECT * FROM view_test_table'; |
||
670 | |||
671 | $view = new View($name, $sql); |
||
672 | |||
673 | $this->schemaManager->dropAndCreateView($view); |
||
674 | |||
675 | self::assertTrue($this->hasElementWithName($this->schemaManager->listViews(), $name)); |
||
676 | } |
||
677 | |||
678 | public function testAutoincrementDetection() |
||
679 | { |
||
680 | if (! $this->schemaManager->getDatabasePlatform()->supportsIdentityColumns()) { |
||
681 | $this->markTestSkipped('This test is only supported on platforms that have autoincrement'); |
||
682 | } |
||
683 | |||
684 | $table = new Table('test_autoincrement'); |
||
685 | $table->setSchemaConfig($this->schemaManager->createSchemaConfig()); |
||
686 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
687 | $table->setPrimaryKey(['id']); |
||
688 | |||
689 | $this->schemaManager->createTable($table); |
||
690 | |||
691 | $inferredTable = $this->schemaManager->listTableDetails('test_autoincrement'); |
||
692 | self::assertTrue($inferredTable->hasColumn('id')); |
||
693 | self::assertTrue($inferredTable->getColumn('id')->getAutoincrement()); |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * @group DBAL-792 |
||
698 | */ |
||
699 | public function testAutoincrementDetectionMulticolumns() |
||
700 | { |
||
701 | if (! $this->schemaManager->getDatabasePlatform()->supportsIdentityColumns()) { |
||
702 | $this->markTestSkipped('This test is only supported on platforms that have autoincrement'); |
||
703 | } |
||
704 | |||
705 | $table = new Table('test_not_autoincrement'); |
||
706 | $table->setSchemaConfig($this->schemaManager->createSchemaConfig()); |
||
707 | $table->addColumn('id', 'integer'); |
||
708 | $table->addColumn('other_id', 'integer'); |
||
709 | $table->setPrimaryKey(['id', 'other_id']); |
||
710 | |||
711 | $this->schemaManager->createTable($table); |
||
712 | |||
713 | $inferredTable = $this->schemaManager->listTableDetails('test_not_autoincrement'); |
||
714 | self::assertTrue($inferredTable->hasColumn('id')); |
||
715 | self::assertFalse($inferredTable->getColumn('id')->getAutoincrement()); |
||
716 | } |
||
717 | |||
718 | /** |
||
719 | * @group DDC-887 |
||
720 | */ |
||
721 | public function testUpdateSchemaWithForeignKeyRenaming() |
||
722 | { |
||
723 | if (! $this->schemaManager->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
||
724 | $this->markTestSkipped('This test is only supported on platforms that have foreign keys.'); |
||
725 | } |
||
726 | |||
727 | $table = new Table('test_fk_base'); |
||
728 | $table->addColumn('id', 'integer'); |
||
729 | $table->setPrimaryKey(['id']); |
||
730 | |||
731 | $tableFK = new Table('test_fk_rename'); |
||
732 | $tableFK->setSchemaConfig($this->schemaManager->createSchemaConfig()); |
||
733 | $tableFK->addColumn('id', 'integer'); |
||
734 | $tableFK->addColumn('fk_id', 'integer'); |
||
735 | $tableFK->setPrimaryKey(['id']); |
||
736 | $tableFK->addIndex(['fk_id'], 'fk_idx'); |
||
737 | $tableFK->addForeignKeyConstraint('test_fk_base', ['fk_id'], ['id']); |
||
738 | |||
739 | $this->schemaManager->createTable($table); |
||
740 | $this->schemaManager->createTable($tableFK); |
||
741 | |||
742 | $tableFKNew = new Table('test_fk_rename'); |
||
743 | $tableFKNew->setSchemaConfig($this->schemaManager->createSchemaConfig()); |
||
744 | $tableFKNew->addColumn('id', 'integer'); |
||
745 | $tableFKNew->addColumn('rename_fk_id', 'integer'); |
||
746 | $tableFKNew->setPrimaryKey(['id']); |
||
747 | $tableFKNew->addIndex(['rename_fk_id'], 'fk_idx'); |
||
748 | $tableFKNew->addForeignKeyConstraint('test_fk_base', ['rename_fk_id'], ['id']); |
||
749 | |||
750 | $c = new Comparator(); |
||
751 | $tableDiff = $c->diffTable($tableFK, $tableFKNew); |
||
752 | |||
753 | $this->schemaManager->alterTable($tableDiff); |
||
754 | |||
755 | $table = $this->schemaManager->listTableDetails('test_fk_rename'); |
||
756 | $foreignKeys = $table->getForeignKeys(); |
||
757 | |||
758 | self::assertTrue($table->hasColumn('rename_fk_id')); |
||
759 | self::assertCount(1, $foreignKeys); |
||
760 | self::assertSame(['rename_fk_id'], array_map('strtolower', current($foreignKeys)->getColumns())); |
||
761 | } |
||
762 | |||
763 | /** |
||
764 | * @group DBAL-1062 |
||
765 | */ |
||
766 | public function testRenameIndexUsedInForeignKeyConstraint() |
||
767 | { |
||
768 | if (! $this->schemaManager->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
||
769 | $this->markTestSkipped('This test is only supported on platforms that have foreign keys.'); |
||
770 | } |
||
771 | |||
772 | $primaryTable = new Table('test_rename_index_primary'); |
||
773 | $primaryTable->addColumn('id', 'integer'); |
||
774 | $primaryTable->setPrimaryKey(['id']); |
||
775 | |||
776 | $foreignTable = new Table('test_rename_index_foreign'); |
||
777 | $foreignTable->addColumn('fk', 'integer'); |
||
778 | $foreignTable->addIndex(['fk'], 'rename_index_fk_idx'); |
||
779 | $foreignTable->addForeignKeyConstraint( |
||
780 | 'test_rename_index_primary', |
||
781 | ['fk'], |
||
782 | ['id'], |
||
783 | [], |
||
784 | 'fk_constraint' |
||
785 | ); |
||
786 | |||
787 | $this->schemaManager->dropAndCreateTable($primaryTable); |
||
788 | $this->schemaManager->dropAndCreateTable($foreignTable); |
||
789 | |||
790 | $foreignTable2 = clone $foreignTable; |
||
791 | $foreignTable2->renameIndex('rename_index_fk_idx', 'renamed_index_fk_idx'); |
||
792 | |||
793 | $comparator = new Comparator(); |
||
794 | |||
795 | $this->schemaManager->alterTable($comparator->diffTable($foreignTable, $foreignTable2)); |
||
796 | |||
797 | $foreignTable = $this->schemaManager->listTableDetails('test_rename_index_foreign'); |
||
798 | |||
799 | self::assertFalse($foreignTable->hasIndex('rename_index_fk_idx')); |
||
800 | self::assertTrue($foreignTable->hasIndex('renamed_index_fk_idx')); |
||
801 | self::assertTrue($foreignTable->hasForeignKey('fk_constraint')); |
||
802 | } |
||
803 | |||
804 | /** |
||
805 | * @group DBAL-42 |
||
806 | */ |
||
807 | public function testGetColumnComment() |
||
808 | { |
||
809 | if (! $this->connection->getDatabasePlatform()->supportsInlineColumnComments() && |
||
810 | ! $this->connection->getDatabasePlatform()->supportsCommentOnStatement() && |
||
811 | $this->connection->getDatabasePlatform()->getName() !== 'mssql') { |
||
812 | $this->markTestSkipped('Database does not support column comments.'); |
||
813 | } |
||
814 | |||
815 | $table = new Table('column_comment_test'); |
||
816 | $table->addColumn('id', 'integer', ['comment' => 'This is a comment']); |
||
817 | $table->setPrimaryKey(['id']); |
||
818 | |||
819 | $this->schemaManager->createTable($table); |
||
820 | |||
821 | $columns = $this->schemaManager->listTableColumns('column_comment_test'); |
||
822 | self::assertEquals(1, count($columns)); |
||
823 | self::assertEquals('This is a comment', $columns['id']->getComment()); |
||
824 | |||
825 | $tableDiff = new TableDiff('column_comment_test'); |
||
826 | $tableDiff->fromTable = $table; |
||
827 | $tableDiff->changedColumns['id'] = new ColumnDiff( |
||
828 | 'id', |
||
829 | new Column( |
||
830 | 'id', |
||
831 | Type::getType('integer') |
||
832 | ), |
||
833 | ['comment'], |
||
834 | new Column( |
||
835 | 'id', |
||
836 | Type::getType('integer'), |
||
837 | ['comment' => 'This is a comment'] |
||
838 | ) |
||
839 | ); |
||
840 | |||
841 | $this->schemaManager->alterTable($tableDiff); |
||
842 | |||
843 | $columns = $this->schemaManager->listTableColumns('column_comment_test'); |
||
844 | self::assertEquals(1, count($columns)); |
||
845 | self::assertEmpty($columns['id']->getComment()); |
||
846 | } |
||
847 | |||
848 | /** |
||
849 | * @group DBAL-42 |
||
850 | */ |
||
851 | public function testAutomaticallyAppendCommentOnMarkedColumns() |
||
852 | { |
||
853 | if (! $this->connection->getDatabasePlatform()->supportsInlineColumnComments() && |
||
854 | ! $this->connection->getDatabasePlatform()->supportsCommentOnStatement() && |
||
855 | $this->connection->getDatabasePlatform()->getName() !== 'mssql') { |
||
856 | $this->markTestSkipped('Database does not support column comments.'); |
||
857 | } |
||
858 | |||
859 | $table = new Table('column_comment_test2'); |
||
860 | $table->addColumn('id', 'integer', ['comment' => 'This is a comment']); |
||
861 | $table->addColumn('obj', 'object', ['comment' => 'This is a comment']); |
||
862 | $table->addColumn('arr', 'array', ['comment' => 'This is a comment']); |
||
863 | $table->setPrimaryKey(['id']); |
||
864 | |||
865 | $this->schemaManager->createTable($table); |
||
866 | |||
867 | $columns = $this->schemaManager->listTableColumns('column_comment_test2'); |
||
868 | self::assertEquals(3, count($columns)); |
||
869 | self::assertEquals('This is a comment', $columns['id']->getComment()); |
||
870 | self::assertEquals('This is a comment', $columns['obj']->getComment(), 'The Doctrine2 Typehint should be stripped from comment.'); |
||
871 | self::assertInstanceOf(ObjectType::class, $columns['obj']->getType(), 'The Doctrine2 should be detected from comment hint.'); |
||
872 | self::assertEquals('This is a comment', $columns['arr']->getComment(), 'The Doctrine2 Typehint should be stripped from comment.'); |
||
873 | self::assertInstanceOf(ArrayType::class, $columns['arr']->getType(), 'The Doctrine2 should be detected from comment hint.'); |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * @group DBAL-1228 |
||
878 | */ |
||
879 | public function testCommentHintOnDateIntervalTypeColumn() |
||
880 | { |
||
881 | if (! $this->connection->getDatabasePlatform()->supportsInlineColumnComments() && |
||
882 | ! $this->connection->getDatabasePlatform()->supportsCommentOnStatement() && |
||
883 | $this->connection->getDatabasePlatform()->getName() !== 'mssql') { |
||
884 | $this->markTestSkipped('Database does not support column comments.'); |
||
885 | } |
||
886 | |||
887 | $table = new Table('column_dateinterval_comment'); |
||
888 | $table->addColumn('id', 'integer', ['comment' => 'This is a comment']); |
||
889 | $table->addColumn('date_interval', 'dateinterval', ['comment' => 'This is a comment']); |
||
890 | $table->setPrimaryKey(['id']); |
||
891 | |||
892 | $this->schemaManager->createTable($table); |
||
893 | |||
894 | $columns = $this->schemaManager->listTableColumns('column_dateinterval_comment'); |
||
895 | self::assertEquals(2, count($columns)); |
||
896 | self::assertEquals('This is a comment', $columns['id']->getComment()); |
||
897 | self::assertEquals('This is a comment', $columns['date_interval']->getComment(), 'The Doctrine2 Typehint should be stripped from comment.'); |
||
898 | self::assertInstanceOf(DateIntervalType::class, $columns['date_interval']->getType(), 'The Doctrine2 should be detected from comment hint.'); |
||
899 | } |
||
900 | |||
901 | /** |
||
902 | * @group DBAL-825 |
||
903 | */ |
||
904 | public function testChangeColumnsTypeWithDefaultValue() |
||
905 | { |
||
906 | $tableName = 'column_def_change_type'; |
||
907 | $table = new Table($tableName); |
||
908 | |||
909 | $table->addColumn('col_int', 'smallint', ['default' => 666]); |
||
910 | $table->addColumn('col_string', 'string', ['default' => 'foo']); |
||
911 | |||
912 | $this->schemaManager->dropAndCreateTable($table); |
||
913 | |||
914 | $tableDiff = new TableDiff($tableName); |
||
915 | $tableDiff->fromTable = $table; |
||
916 | $tableDiff->changedColumns['col_int'] = new ColumnDiff( |
||
917 | 'col_int', |
||
918 | new Column('col_int', Type::getType('integer'), ['default' => 666]), |
||
919 | ['type'], |
||
920 | new Column('col_int', Type::getType('smallint'), ['default' => 666]) |
||
921 | ); |
||
922 | |||
923 | $tableDiff->changedColumns['col_string'] = new ColumnDiff( |
||
924 | 'col_string', |
||
925 | new Column('col_string', Type::getType('string'), ['default' => 'foo', 'fixed' => true]), |
||
926 | ['fixed'], |
||
927 | new Column('col_string', Type::getType('string'), ['default' => 'foo']) |
||
928 | ); |
||
929 | |||
930 | $this->schemaManager->alterTable($tableDiff); |
||
931 | |||
932 | $columns = $this->schemaManager->listTableColumns($tableName); |
||
933 | |||
934 | self::assertInstanceOf(IntegerType::class, $columns['col_int']->getType()); |
||
935 | self::assertEquals(666, $columns['col_int']->getDefault()); |
||
936 | |||
937 | self::assertInstanceOf(StringType::class, $columns['col_string']->getType()); |
||
938 | self::assertEquals('foo', $columns['col_string']->getDefault()); |
||
939 | } |
||
940 | |||
941 | /** |
||
942 | * @group DBAL-197 |
||
943 | */ |
||
944 | public function testListTableWithBlob() |
||
945 | { |
||
946 | $table = new Table('test_blob_table'); |
||
947 | $table->addColumn('id', 'integer', ['comment' => 'This is a comment']); |
||
948 | $table->addColumn('binarydata', 'blob', []); |
||
949 | $table->setPrimaryKey(['id']); |
||
950 | |||
951 | $this->schemaManager->createTable($table); |
||
952 | |||
953 | $created = $this->schemaManager->listTableDetails('test_blob_table'); |
||
954 | |||
955 | self::assertTrue($created->hasColumn('id')); |
||
956 | self::assertTrue($created->hasColumn('binarydata')); |
||
957 | self::assertTrue($created->hasPrimaryKey()); |
||
958 | } |
||
959 | |||
960 | /** |
||
961 | * @param string $name |
||
962 | * @param mixed[] $data |
||
963 | * |
||
964 | * @return Table |
||
965 | */ |
||
966 | protected function createTestTable($name = 'test_table', array $data = []) |
||
967 | { |
||
968 | $options = $data['options'] ?? []; |
||
969 | |||
970 | $table = $this->getTestTable($name, $options); |
||
971 | |||
972 | $this->schemaManager->dropAndCreateTable($table); |
||
973 | |||
974 | return $table; |
||
975 | } |
||
976 | |||
977 | protected function getTestTable($name, $options = []) |
||
978 | { |
||
979 | $table = new Table($name, [], [], [], false, $options); |
||
980 | $table->setSchemaConfig($this->schemaManager->createSchemaConfig()); |
||
981 | $table->addColumn('id', 'integer', ['notnull' => true]); |
||
982 | $table->setPrimaryKey(['id']); |
||
983 | $table->addColumn('test', 'string', ['length' => 255]); |
||
984 | $table->addColumn('foreign_key_test', 'integer'); |
||
985 | return $table; |
||
986 | } |
||
987 | |||
988 | protected function getTestCompositeTable($name) |
||
989 | { |
||
990 | $table = new Table($name, [], [], [], false, []); |
||
991 | $table->setSchemaConfig($this->schemaManager->createSchemaConfig()); |
||
992 | $table->addColumn('id', 'integer', ['notnull' => true]); |
||
993 | $table->addColumn('other_id', 'integer', ['notnull' => true]); |
||
994 | $table->setPrimaryKey(['id', 'other_id']); |
||
995 | $table->addColumn('test', 'string', ['length' => 255]); |
||
996 | return $table; |
||
997 | } |
||
998 | |||
999 | protected function assertHasTable($tables, $tableName) |
||
0 ignored issues
–
show
|
|||
1000 | { |
||
1001 | $foundTable = false; |
||
1002 | foreach ($tables as $table) { |
||
1003 | self::assertInstanceOf(Table::class, $table, 'No Table instance was found in tables array.'); |
||
1004 | if (strtolower($table->getName()) !== 'list_tables_test_new_name') { |
||
1005 | continue; |
||
1006 | } |
||
1007 | |||
1008 | $foundTable = true; |
||
1009 | } |
||
1010 | self::assertTrue($foundTable, 'Could not find new table'); |
||
1011 | } |
||
1012 | |||
1013 | public function testListForeignKeysComposite() |
||
1014 | { |
||
1015 | if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
||
1016 | $this->markTestSkipped('Does not support foreign key constraints.'); |
||
1017 | } |
||
1018 | |||
1019 | $this->schemaManager->createTable($this->getTestTable('test_create_fk3')); |
||
1020 | $this->schemaManager->createTable($this->getTestCompositeTable('test_create_fk4')); |
||
1021 | |||
1022 | $foreignKey = new ForeignKeyConstraint( |
||
1023 | ['id', 'foreign_key_test'], |
||
1024 | 'test_create_fk4', |
||
1025 | ['id', 'other_id'], |
||
1026 | 'foreign_key_test_fk2' |
||
1027 | ); |
||
1028 | |||
1029 | $this->schemaManager->createForeignKey($foreignKey, 'test_create_fk3'); |
||
1030 | |||
1031 | $fkeys = $this->schemaManager->listTableForeignKeys('test_create_fk3'); |
||
1032 | |||
1033 | self::assertEquals(1, count($fkeys), "Table 'test_create_fk3' has to have one foreign key."); |
||
1034 | |||
1035 | self::assertInstanceOf(ForeignKeyConstraint::class, $fkeys[0]); |
||
1036 | self::assertEquals(['id', 'foreign_key_test'], array_map('strtolower', $fkeys[0]->getLocalColumns())); |
||
1037 | self::assertEquals(['id', 'other_id'], array_map('strtolower', $fkeys[0]->getForeignColumns())); |
||
1038 | } |
||
1039 | |||
1040 | /** |
||
1041 | * @group DBAL-44 |
||
1042 | */ |
||
1043 | public function testColumnDefaultLifecycle() |
||
1044 | { |
||
1045 | $table = new Table('col_def_lifecycle'); |
||
1046 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
1047 | $table->addColumn('column1', 'string', ['default' => null]); |
||
1048 | $table->addColumn('column2', 'string', ['default' => false]); |
||
1049 | $table->addColumn('column3', 'string', ['default' => true]); |
||
1050 | $table->addColumn('column4', 'string', ['default' => 0]); |
||
1051 | $table->addColumn('column5', 'string', ['default' => '']); |
||
1052 | $table->addColumn('column6', 'string', ['default' => 'def']); |
||
1053 | $table->addColumn('column7', 'integer', ['default' => 0]); |
||
1054 | $table->setPrimaryKey(['id']); |
||
1055 | |||
1056 | $this->schemaManager->dropAndCreateTable($table); |
||
1057 | |||
1058 | $columns = $this->schemaManager->listTableColumns('col_def_lifecycle'); |
||
1059 | |||
1060 | self::assertNull($columns['id']->getDefault()); |
||
1061 | self::assertNull($columns['column1']->getDefault()); |
||
1062 | self::assertSame('', $columns['column2']->getDefault()); |
||
1063 | self::assertSame('1', $columns['column3']->getDefault()); |
||
1064 | self::assertSame('0', $columns['column4']->getDefault()); |
||
1065 | self::assertSame('', $columns['column5']->getDefault()); |
||
1066 | self::assertSame('def', $columns['column6']->getDefault()); |
||
1067 | self::assertSame('0', $columns['column7']->getDefault()); |
||
1068 | |||
1069 | $diffTable = clone $table; |
||
1070 | |||
1071 | $diffTable->changeColumn('column1', ['default' => false]); |
||
1072 | $diffTable->changeColumn('column2', ['default' => null]); |
||
1073 | $diffTable->changeColumn('column3', ['default' => false]); |
||
1074 | $diffTable->changeColumn('column4', ['default' => null]); |
||
1075 | $diffTable->changeColumn('column5', ['default' => false]); |
||
1076 | $diffTable->changeColumn('column6', ['default' => 666]); |
||
1077 | $diffTable->changeColumn('column7', ['default' => null]); |
||
1078 | |||
1079 | $comparator = new Comparator(); |
||
1080 | |||
1081 | $this->schemaManager->alterTable($comparator->diffTable($table, $diffTable)); |
||
1082 | |||
1083 | $columns = $this->schemaManager->listTableColumns('col_def_lifecycle'); |
||
1084 | |||
1085 | self::assertSame('', $columns['column1']->getDefault()); |
||
1086 | self::assertNull($columns['column2']->getDefault()); |
||
1087 | self::assertSame('', $columns['column3']->getDefault()); |
||
1088 | self::assertNull($columns['column4']->getDefault()); |
||
1089 | self::assertSame('', $columns['column5']->getDefault()); |
||
1090 | self::assertSame('666', $columns['column6']->getDefault()); |
||
1091 | self::assertNull($columns['column7']->getDefault()); |
||
1092 | } |
||
1093 | |||
1094 | public function testListTableWithBinary() |
||
1095 | { |
||
1096 | $tableName = 'test_binary_table'; |
||
1097 | |||
1098 | $table = new Table($tableName); |
||
1099 | $table->addColumn('id', 'integer'); |
||
1100 | $table->addColumn('column_varbinary', 'binary', []); |
||
1101 | $table->addColumn('column_binary', 'binary', ['fixed' => true]); |
||
1102 | $table->setPrimaryKey(['id']); |
||
1103 | |||
1104 | $this->schemaManager->createTable($table); |
||
1105 | |||
1106 | $table = $this->schemaManager->listTableDetails($tableName); |
||
1107 | |||
1108 | self::assertInstanceOf(BinaryType::class, $table->getColumn('column_varbinary')->getType()); |
||
1109 | self::assertFalse($table->getColumn('column_varbinary')->getFixed()); |
||
1110 | |||
1111 | self::assertInstanceOf(BinaryType::class, $table->getColumn('column_binary')->getType()); |
||
1112 | self::assertTrue($table->getColumn('column_binary')->getFixed()); |
||
1113 | } |
||
1114 | |||
1115 | public function testListTableDetailsWithFullQualifiedTableName() |
||
1116 | { |
||
1117 | if (! $this->schemaManager->getDatabasePlatform()->supportsSchemas()) { |
||
1118 | $this->markTestSkipped('Test only works on platforms that support schemas.'); |
||
1119 | } |
||
1120 | |||
1121 | $defaultSchemaName = $this->schemaManager->getDatabasePlatform()->getDefaultSchemaName(); |
||
1122 | $primaryTableName = 'primary_table'; |
||
1123 | $foreignTableName = 'foreign_table'; |
||
1124 | |||
1125 | $table = new Table($foreignTableName); |
||
1126 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
1127 | $table->setPrimaryKey(['id']); |
||
1128 | |||
1129 | $this->schemaManager->dropAndCreateTable($table); |
||
1130 | |||
1131 | $table = new Table($primaryTableName); |
||
1132 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
1133 | $table->addColumn('foo', 'integer'); |
||
1134 | $table->addColumn('bar', 'string'); |
||
1135 | $table->addForeignKeyConstraint($foreignTableName, ['foo'], ['id']); |
||
1136 | $table->addIndex(['bar']); |
||
1137 | $table->setPrimaryKey(['id']); |
||
1138 | |||
1139 | $this->schemaManager->dropAndCreateTable($table); |
||
1140 | |||
1141 | self::assertEquals( |
||
1142 | $this->schemaManager->listTableColumns($primaryTableName), |
||
1143 | $this->schemaManager->listTableColumns($defaultSchemaName . '.' . $primaryTableName) |
||
1144 | ); |
||
1145 | self::assertEquals( |
||
1146 | $this->schemaManager->listTableIndexes($primaryTableName), |
||
1147 | $this->schemaManager->listTableIndexes($defaultSchemaName . '.' . $primaryTableName) |
||
1148 | ); |
||
1149 | self::assertEquals( |
||
1150 | $this->schemaManager->listTableForeignKeys($primaryTableName), |
||
1151 | $this->schemaManager->listTableForeignKeys($defaultSchemaName . '.' . $primaryTableName) |
||
1152 | ); |
||
1153 | } |
||
1154 | |||
1155 | public function testCommentStringsAreQuoted() |
||
1156 | { |
||
1157 | if (! $this->connection->getDatabasePlatform()->supportsInlineColumnComments() && |
||
1158 | ! $this->connection->getDatabasePlatform()->supportsCommentOnStatement() && |
||
1159 | $this->connection->getDatabasePlatform()->getName() !== 'mssql') { |
||
1160 | $this->markTestSkipped('Database does not support column comments.'); |
||
1161 | } |
||
1162 | |||
1163 | $table = new Table('my_table'); |
||
1164 | $table->addColumn('id', 'integer', ['comment' => "It's a comment with a quote"]); |
||
1165 | $table->setPrimaryKey(['id']); |
||
1166 | |||
1167 | $this->schemaManager->createTable($table); |
||
1168 | |||
1169 | $columns = $this->schemaManager->listTableColumns('my_table'); |
||
1170 | self::assertEquals("It's a comment with a quote", $columns['id']->getComment()); |
||
1171 | } |
||
1172 | |||
1173 | public function testCommentNotDuplicated() |
||
1174 | { |
||
1175 | if (! $this->connection->getDatabasePlatform()->supportsInlineColumnComments()) { |
||
1176 | $this->markTestSkipped('Database does not support column comments.'); |
||
1177 | } |
||
1178 | |||
1179 | $options = [ |
||
1180 | 'type' => Type::getType('integer'), |
||
1181 | 'default' => 0, |
||
1182 | 'notnull' => true, |
||
1183 | 'comment' => 'expected+column+comment', |
||
1184 | ]; |
||
1185 | $columnDefinition = substr($this->connection->getDatabasePlatform()->getColumnDeclarationSQL('id', $options), strlen('id') + 1); |
||
1186 | |||
1187 | $table = new Table('my_table'); |
||
1188 | $table->addColumn('id', 'integer', ['columnDefinition' => $columnDefinition, 'comment' => 'unexpected_column_comment']); |
||
1189 | $sql = $this->connection->getDatabasePlatform()->getCreateTableSQL($table); |
||
1190 | |||
1191 | self::assertStringContainsString('expected+column+comment', $sql[0]); |
||
1192 | self::assertStringNotContainsString('unexpected_column_comment', $sql[0]); |
||
1193 | } |
||
1194 | |||
1195 | /** |
||
1196 | * @group DBAL-1009 |
||
1197 | * @dataProvider getAlterColumnComment |
||
1198 | */ |
||
1199 | public function testAlterColumnComment($comment1, $expectedComment1, $comment2, $expectedComment2) |
||
1200 | { |
||
1201 | if (! $this->connection->getDatabasePlatform()->supportsInlineColumnComments() && |
||
1202 | ! $this->connection->getDatabasePlatform()->supportsCommentOnStatement() && |
||
1203 | $this->connection->getDatabasePlatform()->getName() !== 'mssql') { |
||
1204 | $this->markTestSkipped('Database does not support column comments.'); |
||
1205 | } |
||
1206 | |||
1207 | $offlineTable = new Table('alter_column_comment_test'); |
||
1208 | $offlineTable->addColumn('comment1', 'integer', ['comment' => $comment1]); |
||
1209 | $offlineTable->addColumn('comment2', 'integer', ['comment' => $comment2]); |
||
1210 | $offlineTable->addColumn('no_comment1', 'integer'); |
||
1211 | $offlineTable->addColumn('no_comment2', 'integer'); |
||
1212 | $this->schemaManager->dropAndCreateTable($offlineTable); |
||
1213 | |||
1214 | $onlineTable = $this->schemaManager->listTableDetails('alter_column_comment_test'); |
||
1215 | |||
1216 | self::assertSame($expectedComment1, $onlineTable->getColumn('comment1')->getComment()); |
||
1217 | self::assertSame($expectedComment2, $onlineTable->getColumn('comment2')->getComment()); |
||
1218 | self::assertNull($onlineTable->getColumn('no_comment1')->getComment()); |
||
1219 | self::assertNull($onlineTable->getColumn('no_comment2')->getComment()); |
||
1220 | |||
1221 | $onlineTable->changeColumn('comment1', ['comment' => $comment2]); |
||
1222 | $onlineTable->changeColumn('comment2', ['comment' => $comment1]); |
||
1223 | $onlineTable->changeColumn('no_comment1', ['comment' => $comment1]); |
||
1224 | $onlineTable->changeColumn('no_comment2', ['comment' => $comment2]); |
||
1225 | |||
1226 | $comparator = new Comparator(); |
||
1227 | |||
1228 | $tableDiff = $comparator->diffTable($offlineTable, $onlineTable); |
||
1229 | |||
1230 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
1231 | |||
1232 | $this->schemaManager->alterTable($tableDiff); |
||
1233 | |||
1234 | $onlineTable = $this->schemaManager->listTableDetails('alter_column_comment_test'); |
||
1235 | |||
1236 | self::assertSame($expectedComment2, $onlineTable->getColumn('comment1')->getComment()); |
||
1237 | self::assertSame($expectedComment1, $onlineTable->getColumn('comment2')->getComment()); |
||
1238 | self::assertSame($expectedComment1, $onlineTable->getColumn('no_comment1')->getComment()); |
||
1239 | self::assertSame($expectedComment2, $onlineTable->getColumn('no_comment2')->getComment()); |
||
1240 | } |
||
1241 | |||
1242 | public function getAlterColumnComment() |
||
1243 | { |
||
1244 | return [ |
||
1245 | [null, null, ' ', ' '], |
||
1246 | [null, null, '0', '0'], |
||
1247 | [null, null, 'foo', 'foo'], |
||
1248 | |||
1249 | ['', null, ' ', ' '], |
||
1250 | ['', null, '0', '0'], |
||
1251 | ['', null, 'foo', 'foo'], |
||
1252 | |||
1253 | [' ', ' ', '0', '0'], |
||
1254 | [' ', ' ', 'foo', 'foo'], |
||
1255 | |||
1256 | ['0', '0', 'foo', 'foo'], |
||
1257 | ]; |
||
1258 | } |
||
1259 | |||
1260 | /** |
||
1261 | * @group DBAL-1095 |
||
1262 | */ |
||
1263 | public function testDoesNotListIndexesImplicitlyCreatedByForeignKeys() |
||
1264 | { |
||
1265 | if (! $this->schemaManager->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
||
1266 | $this->markTestSkipped('This test is only supported on platforms that have foreign keys.'); |
||
1267 | } |
||
1268 | |||
1269 | $primaryTable = new Table('test_list_index_impl_primary'); |
||
1270 | $primaryTable->addColumn('id', 'integer'); |
||
1271 | $primaryTable->setPrimaryKey(['id']); |
||
1272 | |||
1273 | $foreignTable = new Table('test_list_index_impl_foreign'); |
||
1274 | $foreignTable->addColumn('fk1', 'integer'); |
||
1275 | $foreignTable->addColumn('fk2', 'integer'); |
||
1276 | $foreignTable->addIndex(['fk1'], 'explicit_fk1_idx'); |
||
1277 | $foreignTable->addForeignKeyConstraint('test_list_index_impl_primary', ['fk1'], ['id']); |
||
1278 | $foreignTable->addForeignKeyConstraint('test_list_index_impl_primary', ['fk2'], ['id']); |
||
1279 | |||
1280 | $this->schemaManager->dropAndCreateTable($primaryTable); |
||
1281 | $this->schemaManager->dropAndCreateTable($foreignTable); |
||
1282 | |||
1283 | $indexes = $this->schemaManager->listTableIndexes('test_list_index_impl_foreign'); |
||
1284 | |||
1285 | self::assertCount(2, $indexes); |
||
1286 | self::assertArrayHasKey('explicit_fk1_idx', $indexes); |
||
1287 | self::assertArrayHasKey('idx_3d6c147fdc58d6c', $indexes); |
||
1288 | } |
||
1289 | |||
1290 | /** |
||
1291 | * @after |
||
1292 | */ |
||
1293 | public function removeJsonArrayTable() : void |
||
1294 | { |
||
1295 | if (! $this->schemaManager->tablesExist(['json_array_test'])) { |
||
1296 | return; |
||
1297 | } |
||
1298 | |||
1299 | $this->schemaManager->dropTable('json_array_test'); |
||
1300 | } |
||
1301 | |||
1302 | /** |
||
1303 | * @group 2782 |
||
1304 | * @group 6654 |
||
1305 | */ |
||
1306 | public function testComparatorShouldReturnFalseWhenLegacyJsonArrayColumnHasComment() : void |
||
1307 | { |
||
1308 | $table = new Table('json_array_test'); |
||
1309 | $table->addColumn('parameters', 'json_array'); |
||
1310 | |||
1311 | $this->schemaManager->createTable($table); |
||
1312 | |||
1313 | $comparator = new Comparator(); |
||
1314 | $tableDiff = $comparator->diffTable($this->schemaManager->listTableDetails('json_array_test'), $table); |
||
1315 | |||
1316 | self::assertFalse($tableDiff); |
||
1317 | } |
||
1318 | |||
1319 | /** |
||
1320 | * @group 2782 |
||
1321 | * @group 6654 |
||
1322 | */ |
||
1323 | public function testComparatorShouldModifyOnlyTheCommentWhenUpdatingFromJsonArrayTypeOnLegacyPlatforms() : void |
||
1324 | { |
||
1325 | if ($this->schemaManager->getDatabasePlatform()->hasNativeJsonType()) { |
||
1326 | $this->markTestSkipped('This test is only supported on platforms that do not have native JSON type.'); |
||
1327 | } |
||
1328 | |||
1329 | $table = new Table('json_array_test'); |
||
1330 | $table->addColumn('parameters', 'json_array'); |
||
1331 | |||
1332 | $this->schemaManager->createTable($table); |
||
1333 | |||
1334 | $table = new Table('json_array_test'); |
||
1335 | $table->addColumn('parameters', 'json'); |
||
1336 | |||
1337 | $comparator = new Comparator(); |
||
1338 | $tableDiff = $comparator->diffTable($this->schemaManager->listTableDetails('json_array_test'), $table); |
||
1339 | |||
1340 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
1341 | |||
1342 | $changedColumn = $tableDiff->changedColumns['parameters'] ?? $tableDiff->changedColumns['PARAMETERS']; |
||
1343 | |||
1344 | self::assertSame(['comment'], $changedColumn->changedProperties); |
||
1345 | } |
||
1346 | |||
1347 | /** |
||
1348 | * @group 2782 |
||
1349 | * @group 6654 |
||
1350 | */ |
||
1351 | public function testComparatorShouldAddCommentToLegacyJsonArrayTypeThatDoesNotHaveIt() : void |
||
1352 | { |
||
1353 | if (! $this->schemaManager->getDatabasePlatform()->hasNativeJsonType()) { |
||
1354 | $this->markTestSkipped('This test is only supported on platforms that have native JSON type.'); |
||
1355 | } |
||
1356 | |||
1357 | $this->connection->executeQuery('CREATE TABLE json_array_test (parameters JSON NOT NULL)'); |
||
1358 | |||
1359 | $table = new Table('json_array_test'); |
||
1360 | $table->addColumn('parameters', 'json_array'); |
||
1361 | |||
1362 | $comparator = new Comparator(); |
||
1363 | $tableDiff = $comparator->diffTable($this->schemaManager->listTableDetails('json_array_test'), $table); |
||
1364 | |||
1365 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
1366 | self::assertSame(['comment'], $tableDiff->changedColumns['parameters']->changedProperties); |
||
1367 | } |
||
1368 | |||
1369 | /** |
||
1370 | * @group 2782 |
||
1371 | * @group 6654 |
||
1372 | */ |
||
1373 | public function testComparatorShouldReturnAllChangesWhenUsingLegacyJsonArrayType() : void |
||
1374 | { |
||
1375 | if (! $this->schemaManager->getDatabasePlatform()->hasNativeJsonType()) { |
||
1376 | $this->markTestSkipped('This test is only supported on platforms that have native JSON type.'); |
||
1377 | } |
||
1378 | |||
1379 | $this->connection->executeQuery('CREATE TABLE json_array_test (parameters JSON DEFAULT NULL)'); |
||
1380 | |||
1381 | $table = new Table('json_array_test'); |
||
1382 | $table->addColumn('parameters', 'json_array'); |
||
1383 | |||
1384 | $comparator = new Comparator(); |
||
1385 | $tableDiff = $comparator->diffTable($this->schemaManager->listTableDetails('json_array_test'), $table); |
||
1386 | |||
1387 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
1388 | self::assertSame(['notnull', 'comment'], $tableDiff->changedColumns['parameters']->changedProperties); |
||
1389 | } |
||
1390 | |||
1391 | /** |
||
1392 | * @group 2782 |
||
1393 | * @group 6654 |
||
1394 | */ |
||
1395 | public function testComparatorShouldReturnAllChangesWhenUsingLegacyJsonArrayTypeEvenWhenPlatformHasJsonSupport() : void |
||
1396 | { |
||
1397 | if (! $this->schemaManager->getDatabasePlatform()->hasNativeJsonType()) { |
||
1398 | $this->markTestSkipped('This test is only supported on platforms that have native JSON type.'); |
||
1399 | } |
||
1400 | |||
1401 | $this->connection->executeQuery('CREATE TABLE json_array_test (parameters JSON DEFAULT NULL)'); |
||
1402 | |||
1403 | $table = new Table('json_array_test'); |
||
1404 | $table->addColumn('parameters', 'json_array'); |
||
1405 | |||
1406 | $comparator = new Comparator(); |
||
1407 | $tableDiff = $comparator->diffTable($this->schemaManager->listTableDetails('json_array_test'), $table); |
||
1408 | |||
1409 | self::assertInstanceOf(TableDiff::class, $tableDiff); |
||
1410 | self::assertSame(['notnull', 'comment'], $tableDiff->changedColumns['parameters']->changedProperties); |
||
1411 | } |
||
1412 | |||
1413 | /** |
||
1414 | * @group 2782 |
||
1415 | * @group 6654 |
||
1416 | */ |
||
1417 | public function testComparatorShouldNotAddCommentToJsonTypeSinceItIsTheDefaultNow() : void |
||
1418 | { |
||
1419 | if (! $this->schemaManager->getDatabasePlatform()->hasNativeJsonType()) { |
||
1420 | $this->markTestSkipped('This test is only supported on platforms that have native JSON type.'); |
||
1421 | } |
||
1422 | |||
1423 | $this->connection->executeQuery('CREATE TABLE json_test (parameters JSON NOT NULL)'); |
||
1424 | |||
1425 | $table = new Table('json_test'); |
||
1426 | $table->addColumn('parameters', 'json'); |
||
1427 | |||
1428 | $comparator = new Comparator(); |
||
1429 | $tableDiff = $comparator->diffTable($this->schemaManager->listTableDetails('json_test'), $table); |
||
1430 | |||
1431 | self::assertFalse($tableDiff); |
||
1432 | } |
||
1433 | |||
1434 | /** |
||
1435 | * @dataProvider commentsProvider |
||
1436 | * @group 2596 |
||
1437 | */ |
||
1438 | public function testExtractDoctrineTypeFromComment(string $comment, string $expected, string $currentType) : void |
||
1439 | { |
||
1440 | $result = $this->schemaManager->extractDoctrineTypeFromComment($comment, $currentType); |
||
1441 | |||
1442 | self::assertSame($expected, $result); |
||
1443 | } |
||
1444 | |||
1445 | /** |
||
1446 | * @return string[][] |
||
1447 | */ |
||
1448 | public function commentsProvider() : array |
||
1449 | { |
||
1450 | $currentType = 'current type'; |
||
1451 | |||
1452 | return [ |
||
1453 | 'invalid custom type comments' => ['should.return.current.type', $currentType, $currentType], |
||
1454 | 'valid doctrine type' => ['(DC2Type:guid)', 'guid', $currentType], |
||
1455 | 'valid with dots' => ['(DC2Type:type.should.return)', 'type.should.return', $currentType], |
||
1456 | 'valid with namespace' => ['(DC2Type:Namespace\Class)', 'Namespace\Class', $currentType], |
||
1457 | 'valid with extra closing bracket' => ['(DC2Type:should.stop)).before)', 'should.stop', $currentType], |
||
1458 | 'valid with extra opening brackets' => ['(DC2Type:should((.stop)).before)', 'should((.stop', $currentType], |
||
1459 | ]; |
||
1460 | } |
||
1461 | |||
1462 | public function testCreateAndListSequences() : void |
||
1463 | { |
||
1464 | if (! $this->schemaManager->getDatabasePlatform()->supportsSequences()) { |
||
1465 | self::markTestSkipped('This test is only supported on platforms that support sequences.'); |
||
1466 | } |
||
1467 | |||
1468 | $sequence1Name = 'sequence_1'; |
||
1469 | $sequence1AllocationSize = 1; |
||
1470 | $sequence1InitialValue = 2; |
||
1471 | $sequence2Name = 'sequence_2'; |
||
1472 | $sequence2AllocationSize = 3; |
||
1473 | $sequence2InitialValue = 4; |
||
1474 | $sequence1 = new Sequence($sequence1Name, $sequence1AllocationSize, $sequence1InitialValue); |
||
1475 | $sequence2 = new Sequence($sequence2Name, $sequence2AllocationSize, $sequence2InitialValue); |
||
1476 | |||
1477 | $this->schemaManager->createSequence($sequence1); |
||
1478 | $this->schemaManager->createSequence($sequence2); |
||
1479 | |||
1480 | /** @var Sequence[] $actualSequences */ |
||
1481 | $actualSequences = []; |
||
1482 | foreach ($this->schemaManager->listSequences() as $sequence) { |
||
1483 | $actualSequences[$sequence->getName()] = $sequence; |
||
1484 | } |
||
1485 | |||
1486 | $actualSequence1 = $actualSequences[$sequence1Name]; |
||
1487 | $actualSequence2 = $actualSequences[$sequence2Name]; |
||
1488 | |||
1489 | self::assertSame($sequence1Name, $actualSequence1->getName()); |
||
1490 | self::assertEquals($sequence1AllocationSize, $actualSequence1->getAllocationSize()); |
||
1491 | self::assertEquals($sequence1InitialValue, $actualSequence1->getInitialValue()); |
||
1492 | |||
1493 | self::assertSame($sequence2Name, $actualSequence2->getName()); |
||
1494 | self::assertEquals($sequence2AllocationSize, $actualSequence2->getAllocationSize()); |
||
1495 | self::assertEquals($sequence2InitialValue, $actualSequence2->getInitialValue()); |
||
1496 | } |
||
1497 | |||
1498 | /** |
||
1499 | * @group #3086 |
||
1500 | */ |
||
1501 | public function testComparisonWithAutoDetectedSequenceDefinition() : void |
||
1502 | { |
||
1503 | if (! $this->schemaManager->getDatabasePlatform()->supportsSequences()) { |
||
1504 | self::markTestSkipped('This test is only supported on platforms that support sequences.'); |
||
1505 | } |
||
1506 | |||
1507 | $sequenceName = 'sequence_auto_detect_test'; |
||
1508 | $sequenceAllocationSize = 5; |
||
1509 | $sequenceInitialValue = 10; |
||
1510 | $sequence = new Sequence($sequenceName, $sequenceAllocationSize, $sequenceInitialValue); |
||
1511 | |||
1512 | $this->schemaManager->dropAndCreateSequence($sequence); |
||
1513 | |||
1514 | $createdSequence = array_values( |
||
1515 | array_filter( |
||
1516 | $this->schemaManager->listSequences(), |
||
1517 | static function (Sequence $sequence) use ($sequenceName) : bool { |
||
1518 | return strcasecmp($sequence->getName(), $sequenceName) === 0; |
||
1519 | } |
||
1520 | ) |
||
1521 | )[0] ?? null; |
||
1522 | |||
1523 | self::assertNotNull($createdSequence); |
||
1524 | |||
1525 | $comparator = new Comparator(); |
||
1526 | $tableDiff = $comparator->diffSequence($createdSequence, $sequence); |
||
1527 | |||
1528 | self::assertFalse($tableDiff); |
||
1529 | } |
||
1530 | |||
1531 | /** |
||
1532 | * @group DBAL-2921 |
||
1533 | */ |
||
1534 | public function testPrimaryKeyAutoIncrement() |
||
1535 | { |
||
1536 | $table = new Table('test_pk_auto_increment'); |
||
1537 | $table->addColumn('id', 'integer', ['autoincrement' => true]); |
||
1538 | $table->addColumn('text', 'string'); |
||
1539 | $table->setPrimaryKey(['id']); |
||
1540 | $this->schemaManager->dropAndCreateTable($table); |
||
1541 | |||
1542 | $this->connection->insert('test_pk_auto_increment', ['text' => '1']); |
||
1543 | |||
1544 | $query = $this->connection->query('SELECT id FROM test_pk_auto_increment WHERE text = \'1\''); |
||
1545 | $query->execute(); |
||
1546 | $lastUsedIdBeforeDelete = (int) $query->fetchColumn(); |
||
1547 | |||
1548 | $this->connection->query('DELETE FROM test_pk_auto_increment'); |
||
1549 | |||
1550 | $this->connection->insert('test_pk_auto_increment', ['text' => '2']); |
||
1551 | |||
1552 | $query = $this->connection->query('SELECT id FROM test_pk_auto_increment WHERE text = \'2\''); |
||
1553 | $query->execute(); |
||
1554 | $lastUsedIdAfterDelete = (int) $query->fetchColumn(); |
||
1555 | |||
1556 | $this->assertGreaterThan($lastUsedIdBeforeDelete, $lastUsedIdAfterDelete); |
||
1557 | } |
||
1558 | |||
1559 | public function testGenerateAnIndexWithPartialColumnLength() : void |
||
1560 | { |
||
1561 | if (! $this->schemaManager->getDatabasePlatform()->supportsColumnLengthIndexes()) { |
||
1562 | self::markTestSkipped('This test is only supported on platforms that support indexes with column length definitions.'); |
||
1563 | } |
||
1564 | |||
1565 | $table = new Table('test_partial_column_index'); |
||
1566 | $table->addColumn('long_column', 'string', ['length' => 40]); |
||
1567 | $table->addColumn('standard_column', 'integer'); |
||
1568 | $table->addIndex(['long_column'], 'partial_long_column_idx', [], ['lengths' => [4]]); |
||
1569 | $table->addIndex(['standard_column', 'long_column'], 'standard_and_partial_idx', [], ['lengths' => [null, 2]]); |
||
1570 | |||
1571 | $expected = $table->getIndexes(); |
||
1572 | |||
1573 | $this->schemaManager->dropAndCreateTable($table); |
||
1574 | |||
1575 | $onlineTable = $this->schemaManager->listTableDetails('test_partial_column_index'); |
||
1576 | self::assertEquals($expected, $onlineTable->getIndexes()); |
||
1577 | } |
||
1578 | } |
||
1579 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.