1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\DBAL\Functional\Schema; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
8
|
|
|
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; |
9
|
|
|
use Doctrine\DBAL\Schema; |
10
|
|
|
use Doctrine\DBAL\Schema\Comparator; |
11
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
12
|
|
|
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager; |
13
|
|
|
use Doctrine\DBAL\Schema\Table; |
14
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
15
|
|
|
use Doctrine\DBAL\Types\BlobType; |
16
|
|
|
use Doctrine\DBAL\Types\DecimalType; |
17
|
|
|
use Doctrine\DBAL\Types\Type; |
18
|
|
|
use Doctrine\DBAL\Types\Types; |
19
|
|
|
use function array_map; |
20
|
|
|
use function array_pop; |
21
|
|
|
use function count; |
22
|
|
|
use function preg_match; |
23
|
|
|
use function strtolower; |
24
|
|
|
|
25
|
|
|
class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase |
26
|
|
|
{ |
27
|
|
|
/** @var PostgreSqlSchemaManager */ |
28
|
|
|
protected $schemaManager; |
29
|
|
|
|
30
|
|
|
protected function tearDown() : void |
31
|
|
|
{ |
32
|
|
|
parent::tearDown(); |
33
|
|
|
|
34
|
|
|
$this->connection->getConfiguration()->setSchemaAssetsFilter(null); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @group DBAL-177 |
39
|
|
|
*/ |
40
|
|
|
public function testGetSearchPath() : void |
41
|
|
|
{ |
42
|
|
|
$params = $this->connection->getParams(); |
43
|
|
|
|
44
|
|
|
$paths = $this->schemaManager->getSchemaSearchPaths(); |
45
|
|
|
self::assertEquals([$params['user'], 'public'], $paths); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @group DBAL-244 |
50
|
|
|
*/ |
51
|
|
|
public function testGetSchemaNames() : void |
52
|
|
|
{ |
53
|
|
|
$names = $this->schemaManager->getSchemaNames(); |
54
|
|
|
|
55
|
|
|
self::assertIsArray($names); |
56
|
|
|
self::assertNotEmpty($names); |
57
|
|
|
self::assertContains('public', $names, 'The public schema should be found.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @group DBAL-21 |
62
|
|
|
*/ |
63
|
|
|
public function testSupportDomainTypeFallback() : void |
64
|
|
|
{ |
65
|
|
|
$createDomainTypeSQL = 'CREATE DOMAIN MyMoney AS DECIMAL(18,2)'; |
66
|
|
|
$this->connection->exec($createDomainTypeSQL); |
67
|
|
|
|
68
|
|
|
$createTableSQL = 'CREATE TABLE domain_type_test (id INT PRIMARY KEY, value MyMoney)'; |
69
|
|
|
$this->connection->exec($createTableSQL); |
70
|
|
|
|
71
|
|
|
$table = $this->connection->getSchemaManager()->listTableDetails('domain_type_test'); |
72
|
|
|
self::assertInstanceOf(DecimalType::class, $table->getColumn('value')->getType()); |
73
|
|
|
|
74
|
|
|
Type::addType('MyMoney', MoneyType::class); |
75
|
|
|
$this->connection->getDatabasePlatform()->registerDoctrineTypeMapping('MyMoney', 'MyMoney'); |
76
|
|
|
|
77
|
|
|
$table = $this->connection->getSchemaManager()->listTableDetails('domain_type_test'); |
78
|
|
|
self::assertInstanceOf(MoneyType::class, $table->getColumn('value')->getType()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @group DBAL-37 |
83
|
|
|
*/ |
84
|
|
|
public function testDetectsAutoIncrement() : void |
85
|
|
|
{ |
86
|
|
|
$autoincTable = new Table('autoinc_table'); |
87
|
|
|
$column = $autoincTable->addColumn('id', 'integer'); |
88
|
|
|
$column->setAutoincrement(true); |
89
|
|
|
$this->schemaManager->createTable($autoincTable); |
90
|
|
|
$autoincTable = $this->schemaManager->listTableDetails('autoinc_table'); |
91
|
|
|
|
92
|
|
|
self::assertTrue($autoincTable->getColumn('id')->getAutoincrement()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @group DBAL-37 |
97
|
|
|
*/ |
98
|
|
|
public function testAlterTableAutoIncrementAdd() : void |
99
|
|
|
{ |
100
|
|
|
$tableFrom = new Table('autoinc_table_add'); |
101
|
|
|
$column = $tableFrom->addColumn('id', 'integer'); |
|
|
|
|
102
|
|
|
$this->schemaManager->createTable($tableFrom); |
103
|
|
|
$tableFrom = $this->schemaManager->listTableDetails('autoinc_table_add'); |
104
|
|
|
self::assertFalse($tableFrom->getColumn('id')->getAutoincrement()); |
105
|
|
|
|
106
|
|
|
$tableTo = new Table('autoinc_table_add'); |
107
|
|
|
$column = $tableTo->addColumn('id', 'integer'); |
108
|
|
|
$column->setAutoincrement(true); |
109
|
|
|
|
110
|
|
|
$c = new Comparator(); |
111
|
|
|
$diff = $c->diffTable($tableFrom, $tableTo); |
112
|
|
|
|
113
|
|
|
self::assertNotNull($diff); |
114
|
|
|
|
115
|
|
|
$sql = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff); |
116
|
|
|
self::assertEquals([ |
117
|
|
|
'CREATE SEQUENCE autoinc_table_add_id_seq', |
118
|
|
|
"SELECT setval('autoinc_table_add_id_seq', (SELECT MAX(id) FROM autoinc_table_add))", |
119
|
|
|
"ALTER TABLE autoinc_table_add ALTER id SET DEFAULT nextval('autoinc_table_add_id_seq')", |
120
|
|
|
], $sql); |
121
|
|
|
|
122
|
|
|
$this->schemaManager->alterTable($diff); |
123
|
|
|
$tableFinal = $this->schemaManager->listTableDetails('autoinc_table_add'); |
124
|
|
|
self::assertTrue($tableFinal->getColumn('id')->getAutoincrement()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @group DBAL-37 |
129
|
|
|
*/ |
130
|
|
|
public function testAlterTableAutoIncrementDrop() : void |
131
|
|
|
{ |
132
|
|
|
$tableFrom = new Table('autoinc_table_drop'); |
133
|
|
|
$column = $tableFrom->addColumn('id', 'integer'); |
134
|
|
|
$column->setAutoincrement(true); |
135
|
|
|
$this->schemaManager->createTable($tableFrom); |
136
|
|
|
$tableFrom = $this->schemaManager->listTableDetails('autoinc_table_drop'); |
137
|
|
|
self::assertTrue($tableFrom->getColumn('id')->getAutoincrement()); |
138
|
|
|
|
139
|
|
|
$tableTo = new Table('autoinc_table_drop'); |
140
|
|
|
$column = $tableTo->addColumn('id', 'integer'); |
|
|
|
|
141
|
|
|
|
142
|
|
|
$c = new Comparator(); |
143
|
|
|
$diff = $c->diffTable($tableFrom, $tableTo); |
144
|
|
|
|
145
|
|
|
self::assertNotNull($diff); |
146
|
|
|
|
147
|
|
|
self::assertInstanceOf(TableDiff::class, $diff, 'There should be a difference and not false being returned from the table comparison'); |
148
|
|
|
self::assertEquals(['ALTER TABLE autoinc_table_drop ALTER id DROP DEFAULT'], $this->connection->getDatabasePlatform()->getAlterTableSQL($diff)); |
149
|
|
|
|
150
|
|
|
$this->schemaManager->alterTable($diff); |
151
|
|
|
$tableFinal = $this->schemaManager->listTableDetails('autoinc_table_drop'); |
152
|
|
|
self::assertFalse($tableFinal->getColumn('id')->getAutoincrement()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @group DBAL-75 |
157
|
|
|
*/ |
158
|
|
|
public function testTableWithSchema() : void |
159
|
|
|
{ |
160
|
|
|
$this->connection->exec('CREATE SCHEMA nested'); |
161
|
|
|
|
162
|
|
|
$nestedRelatedTable = new Table('nested.schemarelated'); |
163
|
|
|
$column = $nestedRelatedTable->addColumn('id', 'integer'); |
164
|
|
|
$column->setAutoincrement(true); |
165
|
|
|
$nestedRelatedTable->setPrimaryKey(['id']); |
166
|
|
|
|
167
|
|
|
$nestedSchemaTable = new Table('nested.schematable'); |
168
|
|
|
$column = $nestedSchemaTable->addColumn('id', 'integer'); |
169
|
|
|
$column->setAutoincrement(true); |
170
|
|
|
$nestedSchemaTable->setPrimaryKey(['id']); |
171
|
|
|
$nestedSchemaTable->addForeignKeyConstraint($nestedRelatedTable, ['id'], ['id']); |
172
|
|
|
|
173
|
|
|
$this->schemaManager->createTable($nestedRelatedTable); |
174
|
|
|
$this->schemaManager->createTable($nestedSchemaTable); |
175
|
|
|
|
176
|
|
|
$tables = $this->schemaManager->listTableNames(); |
177
|
|
|
self::assertContains('nested.schematable', $tables, 'The table should be detected with its non-public schema.'); |
178
|
|
|
|
179
|
|
|
$nestedSchemaTable = $this->schemaManager->listTableDetails('nested.schematable'); |
180
|
|
|
self::assertTrue($nestedSchemaTable->hasColumn('id')); |
181
|
|
|
self::assertEquals(['id'], $nestedSchemaTable->getPrimaryKey()->getColumns()); |
182
|
|
|
|
183
|
|
|
$relatedFks = $nestedSchemaTable->getForeignKeys(); |
184
|
|
|
self::assertCount(1, $relatedFks); |
185
|
|
|
$relatedFk = array_pop($relatedFks); |
186
|
|
|
self::assertEquals('nested.schemarelated', $relatedFk->getForeignTableName()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @group DBAL-91 |
191
|
|
|
* @group DBAL-88 |
192
|
|
|
*/ |
193
|
|
|
public function testReturnQuotedAssets() : void |
194
|
|
|
{ |
195
|
|
|
$sql = 'create table dbal91_something ( id integer CONSTRAINT id_something PRIMARY KEY NOT NULL ,"table" integer );'; |
196
|
|
|
$this->connection->exec($sql); |
197
|
|
|
|
198
|
|
|
$sql = 'ALTER TABLE dbal91_something ADD CONSTRAINT something_input FOREIGN KEY( "table" ) REFERENCES dbal91_something ON UPDATE CASCADE;'; |
199
|
|
|
$this->connection->exec($sql); |
200
|
|
|
|
201
|
|
|
$table = $this->schemaManager->listTableDetails('dbal91_something'); |
202
|
|
|
|
203
|
|
|
self::assertEquals( |
204
|
|
|
[ |
205
|
|
|
'CREATE TABLE dbal91_something (id INT NOT NULL, "table" INT DEFAULT NULL, PRIMARY KEY(id))', |
206
|
|
|
'CREATE INDEX IDX_A9401304ECA7352B ON dbal91_something ("table")', |
207
|
|
|
], |
208
|
|
|
$this->connection->getDatabasePlatform()->getCreateTableSQL($table) |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @group DBAL-204 |
214
|
|
|
*/ |
215
|
|
|
public function testFilterSchemaExpression() : void |
216
|
|
|
{ |
217
|
|
|
$testTable = new Table('dbal204_test_prefix'); |
218
|
|
|
$column = $testTable->addColumn('id', 'integer'); |
|
|
|
|
219
|
|
|
$this->schemaManager->createTable($testTable); |
220
|
|
|
$testTable = new Table('dbal204_without_prefix'); |
221
|
|
|
$column = $testTable->addColumn('id', 'integer'); |
222
|
|
|
$this->schemaManager->createTable($testTable); |
223
|
|
|
|
224
|
|
|
$this->connection->getConfiguration()->setSchemaAssetsFilter(static function (string $name) : bool { |
225
|
|
|
return preg_match('#^dbal204_#', $name) === 1; |
226
|
|
|
}); |
227
|
|
|
$names = $this->schemaManager->listTableNames(); |
228
|
|
|
self::assertCount(2, $names); |
229
|
|
|
|
230
|
|
|
$this->connection->getConfiguration()->setSchemaAssetsFilter(static function (string $name) : bool { |
231
|
|
|
return preg_match('#^dbal204_test#', $name) === 1; |
232
|
|
|
}); |
233
|
|
|
$names = $this->schemaManager->listTableNames(); |
234
|
|
|
self::assertCount(1, $names); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testListForeignKeys() : void |
238
|
|
|
{ |
239
|
|
|
if (! $this->connection->getDatabasePlatform()->supportsForeignKeyConstraints()) { |
240
|
|
|
$this->markTestSkipped('Does not support foreign key constraints.'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$fkOptions = ['SET NULL', 'SET DEFAULT', 'NO ACTION','CASCADE', 'RESTRICT']; |
244
|
|
|
$foreignKeys = []; |
245
|
|
|
$fkTable = $this->getTestTable('test_create_fk1'); |
246
|
|
|
for ($i = 0; $i < count($fkOptions); $i++) { |
|
|
|
|
247
|
|
|
$fkTable->addColumn('foreign_key_test' . $i, 'integer'); |
248
|
|
|
$foreignKeys[] = new ForeignKeyConstraint( |
249
|
|
|
['foreign_key_test' . $i], |
250
|
|
|
'test_create_fk2', |
251
|
|
|
['id'], |
252
|
|
|
'foreign_key_test' . $i . '_fk', |
253
|
|
|
['onDelete' => $fkOptions[$i]] |
254
|
|
|
); |
255
|
|
|
} |
256
|
|
|
$this->schemaManager->dropAndCreateTable($fkTable); |
257
|
|
|
$this->createTestTable('test_create_fk2'); |
258
|
|
|
|
259
|
|
|
foreach ($foreignKeys as $foreignKey) { |
260
|
|
|
$this->schemaManager->createForeignKey($foreignKey, 'test_create_fk1'); |
261
|
|
|
} |
262
|
|
|
$fkeys = $this->schemaManager->listTableForeignKeys('test_create_fk1'); |
263
|
|
|
self::assertEquals(count($foreignKeys), count($fkeys), "Table 'test_create_fk1' has to have " . count($foreignKeys) . ' foreign keys.'); |
264
|
|
|
for ($i = 0; $i < count($fkeys); $i++) { |
|
|
|
|
265
|
|
|
self::assertEquals(['foreign_key_test' . $i], array_map('strtolower', $fkeys[$i]->getLocalColumns())); |
266
|
|
|
self::assertEquals(['id'], array_map('strtolower', $fkeys[$i]->getForeignColumns())); |
267
|
|
|
self::assertEquals('test_create_fk2', strtolower($fkeys[0]->getForeignTableName())); |
268
|
|
|
if ($foreignKeys[$i]->getOption('onDelete') === 'NO ACTION') { |
269
|
|
|
self::assertFalse($fkeys[$i]->hasOption('onDelete'), 'Unexpected option: ' . $fkeys[$i]->getOption('onDelete')); |
270
|
|
|
} else { |
271
|
|
|
self::assertEquals($foreignKeys[$i]->getOption('onDelete'), $fkeys[$i]->getOption('onDelete')); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @group DBAL-511 |
278
|
|
|
*/ |
279
|
|
|
public function testDefaultValueCharacterVarying() : void |
280
|
|
|
{ |
281
|
|
|
$testTable = new Table('dbal511_default'); |
282
|
|
|
$testTable->addColumn('id', 'integer'); |
283
|
|
|
$testTable->addColumn('def', 'string', ['default' => 'foo']); |
284
|
|
|
$testTable->setPrimaryKey(['id']); |
285
|
|
|
|
286
|
|
|
$this->schemaManager->createTable($testTable); |
287
|
|
|
|
288
|
|
|
$databaseTable = $this->schemaManager->listTableDetails($testTable->getName()); |
289
|
|
|
|
290
|
|
|
self::assertEquals('foo', $databaseTable->getColumn('def')->getDefault()); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @group DDC-2843 |
295
|
|
|
*/ |
296
|
|
|
public function testBooleanDefault() : void |
297
|
|
|
{ |
298
|
|
|
$table = new Table('ddc2843_bools'); |
299
|
|
|
$table->addColumn('id', 'integer'); |
300
|
|
|
$table->addColumn('checked', 'boolean', ['default' => false]); |
301
|
|
|
|
302
|
|
|
$this->schemaManager->createTable($table); |
303
|
|
|
|
304
|
|
|
$databaseTable = $this->schemaManager->listTableDetails($table->getName()); |
305
|
|
|
|
306
|
|
|
$c = new Comparator(); |
307
|
|
|
$diff = $c->diffTable($table, $databaseTable); |
308
|
|
|
|
309
|
|
|
self::assertNull($diff); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function testListTableWithBinary() : void |
313
|
|
|
{ |
314
|
|
|
$tableName = 'test_binary_table'; |
315
|
|
|
|
316
|
|
|
$table = new Table($tableName); |
317
|
|
|
$table->addColumn('id', 'integer'); |
318
|
|
|
$table->addColumn('column_varbinary', 'binary', []); |
319
|
|
|
$table->addColumn('column_binary', 'binary', ['fixed' => true]); |
320
|
|
|
$table->setPrimaryKey(['id']); |
321
|
|
|
|
322
|
|
|
$this->schemaManager->createTable($table); |
323
|
|
|
|
324
|
|
|
$table = $this->schemaManager->listTableDetails($tableName); |
325
|
|
|
|
326
|
|
|
self::assertInstanceOf(BlobType::class, $table->getColumn('column_varbinary')->getType()); |
327
|
|
|
self::assertFalse($table->getColumn('column_varbinary')->getFixed()); |
328
|
|
|
|
329
|
|
|
self::assertInstanceOf(BlobType::class, $table->getColumn('column_binary')->getType()); |
330
|
|
|
self::assertFalse($table->getColumn('column_binary')->getFixed()); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function testListQuotedTable() : void |
334
|
|
|
{ |
335
|
|
|
$offlineTable = new Schema\Table('user'); |
336
|
|
|
$offlineTable->addColumn('id', 'integer'); |
337
|
|
|
$offlineTable->addColumn('username', 'string'); |
338
|
|
|
$offlineTable->addColumn('fk', 'integer'); |
339
|
|
|
$offlineTable->setPrimaryKey(['id']); |
340
|
|
|
$offlineTable->addForeignKeyConstraint($offlineTable, ['fk'], ['id']); |
341
|
|
|
|
342
|
|
|
$this->schemaManager->dropAndCreateTable($offlineTable); |
343
|
|
|
|
344
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('"user"'); |
345
|
|
|
|
346
|
|
|
$comparator = new Schema\Comparator(); |
347
|
|
|
|
348
|
|
|
self::assertNull($comparator->diffTable($offlineTable, $onlineTable)); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testListTableDetailsWithNumberSchema() : void |
352
|
|
|
{ |
353
|
|
|
$this->connection->exec('CREATE SCHEMA IF NOT EXISTS "001_test"'); |
354
|
|
|
|
355
|
|
|
$offlineTable = new Schema\Table('"001_test".test'); |
356
|
|
|
$offlineTable->addColumn('id', 'integer'); |
357
|
|
|
$this->schemaManager->dropAndCreateTable($offlineTable); |
358
|
|
|
|
359
|
|
|
$onlineTable = $this->connection->transactional(function () : Table { |
360
|
|
|
$this->connection->exec('SET LOCAL search_path = "001_test"'); |
361
|
|
|
|
362
|
|
|
return $this->schemaManager->listTableDetails('test'); |
363
|
|
|
}); |
364
|
|
|
|
365
|
|
|
$comparator = new Schema\Comparator(); |
366
|
|
|
|
367
|
|
|
self::assertNull($comparator->diffTable($offlineTable, $onlineTable)); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
public function testListTablesExcludesViews() : void |
371
|
|
|
{ |
372
|
|
|
$this->createTestTable('list_tables_excludes_views'); |
373
|
|
|
|
374
|
|
|
$name = 'list_tables_excludes_views_test_view'; |
375
|
|
|
$sql = 'SELECT * from list_tables_excludes_views'; |
376
|
|
|
|
377
|
|
|
$view = new Schema\View($name, $sql); |
378
|
|
|
|
379
|
|
|
$this->schemaManager->dropAndCreateView($view); |
380
|
|
|
|
381
|
|
|
$tables = $this->schemaManager->listTables(); |
382
|
|
|
|
383
|
|
|
$foundTable = false; |
384
|
|
|
foreach ($tables as $table) { |
385
|
|
|
self::assertInstanceOf(Table::class, $table, 'No Table instance was found in tables array.'); |
386
|
|
|
if (strtolower($table->getName()) !== 'list_tables_excludes_views_test_view') { |
387
|
|
|
continue; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
$foundTable = true; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
self::assertFalse($foundTable, 'View "list_tables_excludes_views_test_view" must not be found in table list'); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @group DBAL-1033 |
398
|
|
|
*/ |
399
|
|
|
public function testPartialIndexes() : void |
400
|
|
|
{ |
401
|
|
|
$offlineTable = new Schema\Table('person'); |
402
|
|
|
$offlineTable->addColumn('id', 'integer'); |
403
|
|
|
$offlineTable->addColumn('name', 'string'); |
404
|
|
|
$offlineTable->addColumn('email', 'string'); |
405
|
|
|
$offlineTable->addUniqueIndex(['id', 'name'], 'simple_partial_index', ['where' => '(id IS NULL)']); |
406
|
|
|
|
407
|
|
|
$this->schemaManager->dropAndCreateTable($offlineTable); |
408
|
|
|
|
409
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('person'); |
410
|
|
|
|
411
|
|
|
$comparator = new Schema\Comparator(); |
412
|
|
|
|
413
|
|
|
self::assertNull($comparator->diffTable($offlineTable, $onlineTable)); |
414
|
|
|
self::assertTrue($onlineTable->hasIndex('simple_partial_index')); |
415
|
|
|
self::assertTrue($onlineTable->getIndex('simple_partial_index')->hasOption('where')); |
416
|
|
|
self::assertSame('(id IS NULL)', $onlineTable->getIndex('simple_partial_index')->getOption('where')); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
public function testJsonbColumn() : void |
420
|
|
|
{ |
421
|
|
|
if (! $this->schemaManager->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
422
|
|
|
$this->markTestSkipped('Requires PostgresSQL 9.4+'); |
423
|
|
|
|
424
|
|
|
return; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
$table = new Schema\Table('test_jsonb'); |
428
|
|
|
$table->addColumn('foo', Types::JSON)->setPlatformOption('jsonb', true); |
429
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
430
|
|
|
|
431
|
|
|
$columns = $this->schemaManager->listTableColumns('test_jsonb'); |
432
|
|
|
|
433
|
|
|
self::assertSame(Types::JSON, $columns['foo']->getType()->getName()); |
434
|
|
|
self::assertTrue($columns['foo']->getPlatformOption('jsonb')); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @group DBAL-2427 |
439
|
|
|
*/ |
440
|
|
|
public function testListNegativeColumnDefaultValue() : void |
441
|
|
|
{ |
442
|
|
|
$table = new Schema\Table('test_default_negative'); |
443
|
|
|
$table->addColumn('col_smallint', 'smallint', ['default' => -1]); |
444
|
|
|
$table->addColumn('col_integer', 'integer', ['default' => -1]); |
445
|
|
|
$table->addColumn('col_bigint', 'bigint', ['default' => -1]); |
446
|
|
|
$table->addColumn('col_float', 'float', ['default' => -1.1]); |
447
|
|
|
$table->addColumn('col_decimal', 'decimal', ['default' => -1.1]); |
448
|
|
|
$table->addColumn('col_string', 'string', ['default' => '(-1)']); |
449
|
|
|
|
450
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
451
|
|
|
|
452
|
|
|
$columns = $this->schemaManager->listTableColumns('test_default_negative'); |
453
|
|
|
|
454
|
|
|
self::assertEquals(-1, $columns['col_smallint']->getDefault()); |
455
|
|
|
self::assertEquals(-1, $columns['col_integer']->getDefault()); |
456
|
|
|
self::assertEquals(-1, $columns['col_bigint']->getDefault()); |
457
|
|
|
self::assertEquals(-1.1, $columns['col_float']->getDefault()); |
458
|
|
|
self::assertEquals(-1.1, $columns['col_decimal']->getDefault()); |
459
|
|
|
self::assertEquals('(-1)', $columns['col_string']->getDefault()); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @return mixed[][] |
464
|
|
|
*/ |
465
|
|
|
public static function serialTypes() : iterable |
466
|
|
|
{ |
467
|
|
|
return [ |
468
|
|
|
['integer'], |
469
|
|
|
['bigint'], |
470
|
|
|
]; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* @dataProvider serialTypes |
475
|
|
|
* @group 2906 |
476
|
|
|
*/ |
477
|
|
|
public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValue(string $type) : void |
478
|
|
|
{ |
479
|
|
|
$tableName = 'test_serial_type_' . $type; |
480
|
|
|
|
481
|
|
|
$table = new Schema\Table($tableName); |
482
|
|
|
$table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false]); |
483
|
|
|
|
484
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
485
|
|
|
|
486
|
|
|
$columns = $this->schemaManager->listTableColumns($tableName); |
487
|
|
|
|
488
|
|
|
self::assertNull($columns['id']->getDefault()); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* @dataProvider serialTypes |
493
|
|
|
* @group 2906 |
494
|
|
|
*/ |
495
|
|
|
public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenWhenDefaultIsSet(string $type) : void |
496
|
|
|
{ |
497
|
|
|
$tableName = 'test_serial_type_with_default_' . $type; |
498
|
|
|
|
499
|
|
|
$table = new Schema\Table($tableName); |
500
|
|
|
$table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false, 'default' => 1]); |
501
|
|
|
|
502
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
503
|
|
|
|
504
|
|
|
$columns = $this->schemaManager->listTableColumns($tableName); |
505
|
|
|
|
506
|
|
|
self::assertNull($columns['id']->getDefault()); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* @group 2916 |
511
|
|
|
* @dataProvider autoIncrementTypeMigrations |
512
|
|
|
*/ |
513
|
|
|
public function testAlterTableAutoIncrementIntToBigInt(string $from, string $to, string $expected) : void |
514
|
|
|
{ |
515
|
|
|
$tableFrom = new Table('autoinc_type_modification'); |
516
|
|
|
$column = $tableFrom->addColumn('id', $from); |
517
|
|
|
$column->setAutoincrement(true); |
518
|
|
|
$this->schemaManager->dropAndCreateTable($tableFrom); |
519
|
|
|
$tableFrom = $this->schemaManager->listTableDetails('autoinc_type_modification'); |
520
|
|
|
self::assertTrue($tableFrom->getColumn('id')->getAutoincrement()); |
521
|
|
|
|
522
|
|
|
$tableTo = new Table('autoinc_type_modification'); |
523
|
|
|
$column = $tableTo->addColumn('id', $to); |
524
|
|
|
$column->setAutoincrement(true); |
525
|
|
|
|
526
|
|
|
$c = new Comparator(); |
527
|
|
|
$diff = $c->diffTable($tableFrom, $tableTo); |
528
|
|
|
self::assertInstanceOf(TableDiff::class, $diff, 'There should be a difference and not false being returned from the table comparison'); |
529
|
|
|
self::assertSame(['ALTER TABLE autoinc_type_modification ALTER id TYPE ' . $expected], $this->connection->getDatabasePlatform()->getAlterTableSQL($diff)); |
530
|
|
|
|
531
|
|
|
$this->schemaManager->alterTable($diff); |
532
|
|
|
$tableFinal = $this->schemaManager->listTableDetails('autoinc_type_modification'); |
533
|
|
|
self::assertTrue($tableFinal->getColumn('id')->getAutoincrement()); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* @return mixed[][] |
538
|
|
|
*/ |
539
|
|
|
public static function autoIncrementTypeMigrations() : iterable |
540
|
|
|
{ |
541
|
|
|
return [ |
542
|
|
|
'int->bigint' => ['integer', 'bigint', 'BIGINT'], |
543
|
|
|
'bigint->int' => ['bigint', 'integer', 'INT'], |
544
|
|
|
]; |
545
|
|
|
} |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
class MoneyType extends Type |
549
|
|
|
{ |
550
|
|
|
/** |
551
|
|
|
* {@inheritDoc} |
552
|
|
|
*/ |
553
|
|
|
public function getName() : string |
554
|
|
|
{ |
555
|
|
|
return 'MyMoney'; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* {@inheritDoc} |
560
|
|
|
*/ |
561
|
|
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) : string |
562
|
|
|
{ |
563
|
|
|
return 'MyMoney'; |
564
|
|
|
} |
565
|
|
|
} |
566
|
|
|
|