Passed
Push — 2.10 ( 4d4464...a0e237 )
by Sergei
13:23 queued 10s
created

testListTableDetailsWhenCurrentSchemaNameQuoted()   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 6
c 1
b 1
f 0
dl 0
loc 9
rs 10
cc 1
nc 2
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Schema;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
7
use Doctrine\DBAL\Schema;
8
use Doctrine\DBAL\Schema\Comparator;
9
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
10
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\DBAL\Schema\TableDiff;
12
use Doctrine\DBAL\Types\BlobType;
13
use Doctrine\DBAL\Types\DecimalType;
14
use Doctrine\DBAL\Types\Type;
15
use Doctrine\DBAL\Types\Types;
16
use function array_map;
17
use function array_pop;
18
use function count;
19
use function strtolower;
20
21
class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
22
{
23
    protected function tearDown() : void
24
    {
25
        parent::tearDown();
26
27
        if (! $this->connection) {
28
            return;
29
        }
30
31
        $this->connection->getConfiguration()->setFilterSchemaAssetsExpression(null);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

31
        /** @scrutinizer ignore-deprecated */ $this->connection->getConfiguration()->setFilterSchemaAssetsExpression(null);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
32
    }
33
34
    /**
35
     * @group DBAL-177
36
     */
37
    public function testGetSearchPath() : void
38
    {
39
        $params = $this->connection->getParams();
40
41
        $paths = $this->schemaManager->getSchemaSearchPaths();
42
        self::assertEquals([$params['user'], 'public'], $paths);
43
    }
44
45
    /**
46
     * @group DBAL-244
47
     */
48
    public function testGetSchemaNames() : void
49
    {
50
        $names = $this->schemaManager->getSchemaNames();
0 ignored issues
show
Bug introduced by
The method getSchemaNames() does not exist on Doctrine\DBAL\Schema\AbstractSchemaManager. It seems like you code against a sub-type of Doctrine\DBAL\Schema\AbstractSchemaManager such as Doctrine\DBAL\Schema\PostgreSqlSchemaManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        /** @scrutinizer ignore-call */ 
51
        $names = $this->schemaManager->getSchemaNames();
Loading history...
51
52
        self::assertIsArray($names);
53
        self::assertNotEmpty($names);
54
        self::assertContains('public', $names, 'The public schema should be found.');
55
    }
56
57
    /**
58
     * @group DBAL-21
59
     */
60
    public function testSupportDomainTypeFallback() : void
61
    {
62
        $createDomainTypeSQL = 'CREATE DOMAIN MyMoney AS DECIMAL(18,2)';
63
        $this->connection->exec($createDomainTypeSQL);
64
65
        $createTableSQL = 'CREATE TABLE domain_type_test (id INT PRIMARY KEY, value MyMoney)';
66
        $this->connection->exec($createTableSQL);
67
68
        $table = $this->connection->getSchemaManager()->listTableDetails('domain_type_test');
69
        self::assertInstanceOf(DecimalType::class, $table->getColumn('value')->getType());
70
71
        Type::addType('MyMoney', MoneyType::class);
72
        $this->connection->getDatabasePlatform()->registerDoctrineTypeMapping('MyMoney', 'MyMoney');
73
74
        $table = $this->connection->getSchemaManager()->listTableDetails('domain_type_test');
75
        self::assertInstanceOf(MoneyType::class, $table->getColumn('value')->getType());
76
    }
77
78
    /**
79
     * @group DBAL-37
80
     */
81
    public function testDetectsAutoIncrement() : void
82
    {
83
        $autoincTable = new Table('autoinc_table');
84
        $column       = $autoincTable->addColumn('id', 'integer');
85
        $column->setAutoincrement(true);
86
        $this->schemaManager->createTable($autoincTable);
87
        $autoincTable = $this->schemaManager->listTableDetails('autoinc_table');
88
89
        self::assertTrue($autoincTable->getColumn('id')->getAutoincrement());
90
    }
91
92
    /**
93
     * @group DBAL-37
94
     */
95
    public function testAlterTableAutoIncrementAdd() : void
96
    {
97
        $tableFrom = new Table('autoinc_table_add');
98
        $column    = $tableFrom->addColumn('id', 'integer');
0 ignored issues
show
Unused Code introduced by
The assignment to $column is dead and can be removed.
Loading history...
99
        $this->schemaManager->createTable($tableFrom);
100
        $tableFrom = $this->schemaManager->listTableDetails('autoinc_table_add');
101
        self::assertFalse($tableFrom->getColumn('id')->getAutoincrement());
102
103
        $tableTo = new Table('autoinc_table_add');
104
        $column  = $tableTo->addColumn('id', 'integer');
105
        $column->setAutoincrement(true);
106
107
        $c    = new Comparator();
108
        $diff = $c->diffTable($tableFrom, $tableTo);
109
        $sql  = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff);
110
        self::assertEquals([
111
            'CREATE SEQUENCE autoinc_table_add_id_seq',
112
            "SELECT setval('autoinc_table_add_id_seq', (SELECT MAX(id) FROM autoinc_table_add))",
113
            "ALTER TABLE autoinc_table_add ALTER id SET DEFAULT nextval('autoinc_table_add_id_seq')",
114
        ], $sql);
115
116
        $this->schemaManager->alterTable($diff);
117
        $tableFinal = $this->schemaManager->listTableDetails('autoinc_table_add');
118
        self::assertTrue($tableFinal->getColumn('id')->getAutoincrement());
119
    }
120
121
    /**
122
     * @group DBAL-37
123
     */
124
    public function testAlterTableAutoIncrementDrop() : void
125
    {
126
        $tableFrom = new Table('autoinc_table_drop');
127
        $column    = $tableFrom->addColumn('id', 'integer');
128
        $column->setAutoincrement(true);
129
        $this->schemaManager->createTable($tableFrom);
130
        $tableFrom = $this->schemaManager->listTableDetails('autoinc_table_drop');
131
        self::assertTrue($tableFrom->getColumn('id')->getAutoincrement());
132
133
        $tableTo = new Table('autoinc_table_drop');
134
        $column  = $tableTo->addColumn('id', 'integer');
0 ignored issues
show
Unused Code introduced by
The assignment to $column is dead and can be removed.
Loading history...
135
136
        $c    = new Comparator();
137
        $diff = $c->diffTable($tableFrom, $tableTo);
138
        self::assertInstanceOf(TableDiff::class, $diff, 'There should be a difference and not false being returned from the table comparison');
139
        self::assertEquals(['ALTER TABLE autoinc_table_drop ALTER id DROP DEFAULT'], $this->connection->getDatabasePlatform()->getAlterTableSQL($diff));
140
141
        $this->schemaManager->alterTable($diff);
142
        $tableFinal = $this->schemaManager->listTableDetails('autoinc_table_drop');
143
        self::assertFalse($tableFinal->getColumn('id')->getAutoincrement());
144
    }
145
146
    /**
147
     * @group DBAL-75
148
     */
149
    public function testTableWithSchema() : void
150
    {
151
        $this->connection->exec('CREATE SCHEMA nested');
152
153
        $nestedRelatedTable = new Table('nested.schemarelated');
154
        $column             = $nestedRelatedTable->addColumn('id', 'integer');
155
        $column->setAutoincrement(true);
156
        $nestedRelatedTable->setPrimaryKey(['id']);
157
158
        $nestedSchemaTable = new Table('nested.schematable');
159
        $column            = $nestedSchemaTable->addColumn('id', 'integer');
160
        $column->setAutoincrement(true);
161
        $nestedSchemaTable->setPrimaryKey(['id']);
162
        $nestedSchemaTable->addUnnamedForeignKeyConstraint($nestedRelatedTable, ['id'], ['id']);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Tab...dForeignKeyConstraint() has been deprecated: Use {@link addForeignKeyConstraint} ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

162
        /** @scrutinizer ignore-deprecated */ $nestedSchemaTable->addUnnamedForeignKeyConstraint($nestedRelatedTable, ['id'], ['id']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
163
164
        $this->schemaManager->createTable($nestedRelatedTable);
165
        $this->schemaManager->createTable($nestedSchemaTable);
166
167
        $tables = $this->schemaManager->listTableNames();
168
        self::assertContains('nested.schematable', $tables, 'The table should be detected with its non-public schema.');
169
170
        $nestedSchemaTable = $this->schemaManager->listTableDetails('nested.schematable');
171
        self::assertTrue($nestedSchemaTable->hasColumn('id'));
172
        self::assertEquals(['id'], $nestedSchemaTable->getPrimaryKey()->getColumns());
173
174
        $relatedFks = $nestedSchemaTable->getForeignKeys();
175
        self::assertCount(1, $relatedFks);
176
        $relatedFk = array_pop($relatedFks);
177
        self::assertEquals('nested.schemarelated', $relatedFk->getForeignTableName());
178
    }
179
180
    /**
181
     * @group DBAL-91
182
     * @group DBAL-88
183
     */
184
    public function testReturnQuotedAssets() : void
185
    {
186
        $sql = 'create table dbal91_something ( id integer  CONSTRAINT id_something PRIMARY KEY NOT NULL  ,"table"   integer );';
187
        $this->connection->exec($sql);
188
189
        $sql = 'ALTER TABLE dbal91_something ADD CONSTRAINT something_input FOREIGN KEY( "table" ) REFERENCES dbal91_something ON UPDATE CASCADE;';
190
        $this->connection->exec($sql);
191
192
        $table = $this->schemaManager->listTableDetails('dbal91_something');
193
194
        self::assertEquals(
195
            [
196
                'CREATE TABLE dbal91_something (id INT NOT NULL, "table" INT DEFAULT NULL, PRIMARY KEY(id))',
197
                'CREATE INDEX IDX_A9401304ECA7352B ON dbal91_something ("table")',
198
            ],
199
            $this->connection->getDatabasePlatform()->getCreateTableSQL($table)
200
        );
201
    }
202
203
    /**
204
     * @group DBAL-204
205
     */
206
    public function testFilterSchemaExpression() : void
207
    {
208
        $testTable = new Table('dbal204_test_prefix');
209
        $column    = $testTable->addColumn('id', 'integer');
0 ignored issues
show
Unused Code introduced by
The assignment to $column is dead and can be removed.
Loading history...
210
        $this->schemaManager->createTable($testTable);
211
        $testTable = new Table('dbal204_without_prefix');
212
        $column    = $testTable->addColumn('id', 'integer');
213
        $this->schemaManager->createTable($testTable);
214
215
        $this->connection->getConfiguration()->setFilterSchemaAssetsExpression('#^dbal204_#');
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated: Use Configuration::setSchemaAssetsFilter() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

215
        /** @scrutinizer ignore-deprecated */ $this->connection->getConfiguration()->setFilterSchemaAssetsExpression('#^dbal204_#');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
216
        $names = $this->schemaManager->listTableNames();
217
        self::assertCount(2, $names);
218
219
        $this->connection->getConfiguration()->setFilterSchemaAssetsExpression('#^dbal204_test#');
220
        $names = $this->schemaManager->listTableNames();
221
        self::assertCount(1, $names);
222
    }
223
224
    public function testListForeignKeys() : void
225
    {
226
        if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) {
227
            $this->markTestSkipped('Does not support foreign key constraints.');
228
        }
229
230
        $fkOptions   = ['SET NULL', 'SET DEFAULT', 'NO ACTION','CASCADE', 'RESTRICT'];
231
        $foreignKeys = [];
232
        $fkTable     = $this->getTestTable('test_create_fk1');
233
        for ($i = 0; $i < count($fkOptions); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
234
            $fkTable->addColumn('foreign_key_test' . $i, 'integer');
235
            $foreignKeys[] = new ForeignKeyConstraint(
236
                ['foreign_key_test' . $i],
237
                'test_create_fk2',
238
                ['id'],
239
                'foreign_key_test' . $i . '_fk',
240
                ['onDelete' => $fkOptions[$i]]
241
            );
242
        }
243
        $this->schemaManager->dropAndCreateTable($fkTable);
244
        $this->createTestTable('test_create_fk2');
245
246
        foreach ($foreignKeys as $foreignKey) {
247
            $this->schemaManager->createForeignKey($foreignKey, 'test_create_fk1');
248
        }
249
        $fkeys = $this->schemaManager->listTableForeignKeys('test_create_fk1');
250
        self::assertEquals(count($foreignKeys), count($fkeys), "Table 'test_create_fk1' has to have " . count($foreignKeys) . ' foreign keys.');
251
        for ($i = 0; $i < count($fkeys); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
252
            self::assertEquals(['foreign_key_test' . $i], array_map('strtolower', $fkeys[$i]->getLocalColumns()));
253
            self::assertEquals(['id'], array_map('strtolower', $fkeys[$i]->getForeignColumns()));
254
            self::assertEquals('test_create_fk2', strtolower($fkeys[0]->getForeignTableName()));
255
            if ($foreignKeys[$i]->getOption('onDelete') === 'NO ACTION') {
256
                self::assertFalse($fkeys[$i]->hasOption('onDelete'), 'Unexpected option: ' . $fkeys[$i]->getOption('onDelete'));
257
            } else {
258
                self::assertEquals($foreignKeys[$i]->getOption('onDelete'), $fkeys[$i]->getOption('onDelete'));
259
            }
260
        }
261
    }
262
263
    /**
264
     * @group DBAL-511
265
     */
266
    public function testDefaultValueCharacterVarying() : void
267
    {
268
        $testTable = new Table('dbal511_default');
269
        $testTable->addColumn('id', 'integer');
270
        $testTable->addColumn('def', 'string', ['default' => 'foo']);
271
        $testTable->setPrimaryKey(['id']);
272
273
        $this->schemaManager->createTable($testTable);
274
275
        $databaseTable = $this->schemaManager->listTableDetails($testTable->getName());
276
277
        self::assertEquals('foo', $databaseTable->getColumn('def')->getDefault());
278
    }
279
280
    /**
281
     * @group DDC-2843
282
     */
283
    public function testBooleanDefault() : void
284
    {
285
        $table = new Table('ddc2843_bools');
286
        $table->addColumn('id', 'integer');
287
        $table->addColumn('checked', 'boolean', ['default' => false]);
288
289
        $this->schemaManager->createTable($table);
290
291
        $databaseTable = $this->schemaManager->listTableDetails($table->getName());
292
293
        $c    = new Comparator();
294
        $diff = $c->diffTable($table, $databaseTable);
295
296
        self::assertFalse($diff);
297
    }
298
299
    public function testListTableWithBinary() : void
300
    {
301
        $tableName = 'test_binary_table';
302
303
        $table = new Table($tableName);
304
        $table->addColumn('id', 'integer');
305
        $table->addColumn('column_varbinary', 'binary', []);
306
        $table->addColumn('column_binary', 'binary', ['fixed' => true]);
307
        $table->setPrimaryKey(['id']);
308
309
        $this->schemaManager->createTable($table);
310
311
        $table = $this->schemaManager->listTableDetails($tableName);
312
313
        self::assertInstanceOf(BlobType::class, $table->getColumn('column_varbinary')->getType());
314
        self::assertFalse($table->getColumn('column_varbinary')->getFixed());
315
316
        self::assertInstanceOf(BlobType::class, $table->getColumn('column_binary')->getType());
317
        self::assertFalse($table->getColumn('column_binary')->getFixed());
318
    }
319
320
    public function testListQuotedTable() : void
321
    {
322
        $offlineTable = new Schema\Table('user');
323
        $offlineTable->addColumn('id', 'integer');
324
        $offlineTable->addColumn('username', 'string');
325
        $offlineTable->addColumn('fk', 'integer');
326
        $offlineTable->setPrimaryKey(['id']);
327
        $offlineTable->addForeignKeyConstraint($offlineTable, ['fk'], ['id']);
328
329
        $this->schemaManager->dropAndCreateTable($offlineTable);
330
331
        $onlineTable = $this->schemaManager->listTableDetails('"user"');
332
333
        $comparator = new Schema\Comparator();
334
335
        self::assertFalse($comparator->diffTable($offlineTable, $onlineTable));
336
    }
337
338
    public function testListTableDetailsWhenCurrentSchemaNameQuoted() : void
339
    {
340
        $this->connection->exec('CREATE SCHEMA "001_test"');
341
        $this->connection->exec('SET search_path TO "001_test"');
342
343
        try {
344
            $this->testListQuotedTable();
345
        } finally {
346
            $this->connection->close();
347
        }
348
    }
349
350
    public function testListTablesExcludesViews() : void
351
    {
352
        $this->createTestTable('list_tables_excludes_views');
353
354
        $name = 'list_tables_excludes_views_test_view';
355
        $sql  = 'SELECT * from list_tables_excludes_views';
356
357
        $view = new Schema\View($name, $sql);
358
359
        $this->schemaManager->dropAndCreateView($view);
360
361
        $tables = $this->schemaManager->listTables();
362
363
        $foundTable = false;
364
        foreach ($tables as $table) {
365
            self::assertInstanceOf(Table::class, $table, 'No Table instance was found in tables array.');
366
            if (strtolower($table->getName()) !== 'list_tables_excludes_views_test_view') {
367
                continue;
368
            }
369
370
            $foundTable = true;
371
        }
372
373
        self::assertFalse($foundTable, 'View "list_tables_excludes_views_test_view" must not be found in table list');
374
    }
375
376
    /**
377
     * @group DBAL-1033
378
     */
379
    public function testPartialIndexes() : void
380
    {
381
        $offlineTable = new Schema\Table('person');
382
        $offlineTable->addColumn('id', 'integer');
383
        $offlineTable->addColumn('name', 'string');
384
        $offlineTable->addColumn('email', 'string');
385
        $offlineTable->addUniqueIndex(['id', 'name'], 'simple_partial_index', ['where' => '(id IS NULL)']);
386
387
        $this->schemaManager->dropAndCreateTable($offlineTable);
388
389
        $onlineTable = $this->schemaManager->listTableDetails('person');
390
391
        $comparator = new Schema\Comparator();
392
393
        self::assertFalse($comparator->diffTable($offlineTable, $onlineTable));
394
        self::assertTrue($onlineTable->hasIndex('simple_partial_index'));
395
        self::assertTrue($onlineTable->getIndex('simple_partial_index')->hasOption('where'));
396
        self::assertSame('(id IS NULL)', $onlineTable->getIndex('simple_partial_index')->getOption('where'));
397
    }
398
399
    /**
400
     * @dataProvider jsonbColumnTypeProvider
401
     */
402
    public function testJsonbColumn(string $type) : void
403
    {
404
        if (! $this->schemaManager->getDatabasePlatform() instanceof PostgreSQL94Platform) {
405
            $this->markTestSkipped('Requires PostgresSQL 9.4+');
406
407
            return;
408
        }
409
410
        $table = new Schema\Table('test_jsonb');
411
        $table->addColumn('foo', $type)->setPlatformOption('jsonb', true);
412
        $this->schemaManager->dropAndCreateTable($table);
413
414
        $columns = $this->schemaManager->listTableColumns('test_jsonb');
415
416
        self::assertSame($type, $columns['foo']->getType()->getName());
417
        self::assertTrue(true, $columns['foo']->getPlatformOption('jsonb'));
418
    }
419
420
    /**
421
     * @return mixed[][]
422
     */
423
    public function jsonbColumnTypeProvider() : array
424
    {
425
        return [
426
            [Types::JSON],
427
            [Types::JSON_ARRAY],
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Types::JSON_ARRAY has been deprecated: json_array type is deprecated, use {@see self::JSON} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

427
            [/** @scrutinizer ignore-deprecated */ Types::JSON_ARRAY],

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
428
        ];
429
    }
430
431
    /**
432
     * @group DBAL-2427
433
     */
434
    public function testListNegativeColumnDefaultValue() : void
435
    {
436
        $table = new Schema\Table('test_default_negative');
437
        $table->addColumn('col_smallint', 'smallint', ['default' => -1]);
438
        $table->addColumn('col_integer', 'integer', ['default' => -1]);
439
        $table->addColumn('col_bigint', 'bigint', ['default' => -1]);
440
        $table->addColumn('col_float', 'float', ['default' => -1.1]);
441
        $table->addColumn('col_decimal', 'decimal', ['default' => -1.1]);
442
        $table->addColumn('col_string', 'string', ['default' => '(-1)']);
443
444
        $this->schemaManager->dropAndCreateTable($table);
445
446
        $columns = $this->schemaManager->listTableColumns('test_default_negative');
447
448
        self::assertEquals(-1, $columns['col_smallint']->getDefault());
449
        self::assertEquals(-1, $columns['col_integer']->getDefault());
450
        self::assertEquals(-1, $columns['col_bigint']->getDefault());
451
        self::assertEquals(-1.1, $columns['col_float']->getDefault());
452
        self::assertEquals(-1.1, $columns['col_decimal']->getDefault());
453
        self::assertEquals('(-1)', $columns['col_string']->getDefault());
454
    }
455
456
    /**
457
     * @return mixed[][]
458
     */
459
    public static function serialTypes() : iterable
460
    {
461
        return [
462
            ['integer'],
463
            ['bigint'],
464
        ];
465
    }
466
467
    /**
468
     * @dataProvider serialTypes
469
     * @group 2906
470
     */
471
    public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValue(string $type) : void
472
    {
473
        $tableName = 'test_serial_type_' . $type;
474
475
        $table = new Schema\Table($tableName);
476
        $table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false]);
477
478
        $this->schemaManager->dropAndCreateTable($table);
479
480
        $columns = $this->schemaManager->listTableColumns($tableName);
481
482
        self::assertNull($columns['id']->getDefault());
483
    }
484
485
    /**
486
     * @dataProvider serialTypes
487
     * @group 2906
488
     */
489
    public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenWhenDefaultIsSet(string $type) : void
490
    {
491
        $tableName = 'test_serial_type_with_default_' . $type;
492
493
        $table = new Schema\Table($tableName);
494
        $table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false, 'default' => 1]);
495
496
        $this->schemaManager->dropAndCreateTable($table);
497
498
        $columns = $this->schemaManager->listTableColumns($tableName);
499
500
        self::assertNull($columns['id']->getDefault());
501
    }
502
503
    /**
504
     * @group 2916
505
     * @dataProvider autoIncrementTypeMigrations
506
     */
507
    public function testAlterTableAutoIncrementIntToBigInt(string $from, string $to, string $expected) : void
508
    {
509
        $tableFrom = new Table('autoinc_type_modification');
510
        $column    = $tableFrom->addColumn('id', $from);
511
        $column->setAutoincrement(true);
512
        $this->schemaManager->dropAndCreateTable($tableFrom);
513
        $tableFrom = $this->schemaManager->listTableDetails('autoinc_type_modification');
514
        self::assertTrue($tableFrom->getColumn('id')->getAutoincrement());
515
516
        $tableTo = new Table('autoinc_type_modification');
517
        $column  = $tableTo->addColumn('id', $to);
518
        $column->setAutoincrement(true);
519
520
        $c    = new Comparator();
521
        $diff = $c->diffTable($tableFrom, $tableTo);
522
        self::assertInstanceOf(TableDiff::class, $diff, 'There should be a difference and not false being returned from the table comparison');
523
        self::assertSame(['ALTER TABLE autoinc_type_modification ALTER id TYPE ' . $expected], $this->connection->getDatabasePlatform()->getAlterTableSQL($diff));
524
525
        $this->schemaManager->alterTable($diff);
526
        $tableFinal = $this->schemaManager->listTableDetails('autoinc_type_modification');
527
        self::assertTrue($tableFinal->getColumn('id')->getAutoincrement());
528
    }
529
530
    /**
531
     * @return mixed[][]
532
     */
533
    public static function autoIncrementTypeMigrations() : iterable
534
    {
535
        return [
536
            'int->bigint' => ['integer', 'bigint', 'BIGINT'],
537
            'bigint->int' => ['bigint', 'integer', 'INT'],
538
        ];
539
    }
540
}
541
542
class MoneyType extends Type
543
{
544
    /**
545
     * {@inheritDoc}
546
     */
547
    public function getName()
548
    {
549
        return 'MyMoney';
550
    }
551
552
    /**
553
     * {@inheritDoc}
554
     */
555
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
556
    {
557
        return 'MyMoney';
558
    }
559
}
560