1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional\Schema; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\DBAL\Platforms\MariaDb1027Platform; |
7
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
8
|
|
|
use Doctrine\DBAL\Schema\Comparator; |
9
|
|
|
use Doctrine\DBAL\Schema\Schema; |
10
|
|
|
use Doctrine\DBAL\Schema\Table; |
11
|
|
|
use Doctrine\DBAL\Types\Type; |
12
|
|
|
use Doctrine\Tests\Types\MySqlPointType; |
13
|
|
|
|
14
|
|
|
class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase |
15
|
|
|
{ |
16
|
|
|
protected function setUp() : void |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
if (Type::hasType('point')) { |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this->resetSharedConn(); |
25
|
|
|
|
26
|
|
|
Type::addType('point', MySqlPointType::class); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testSwitchPrimaryKeyColumns() |
30
|
|
|
{ |
31
|
|
|
$tableOld = new Table('switch_primary_key_columns'); |
32
|
|
|
$tableOld->addColumn('foo_id', 'integer'); |
33
|
|
|
$tableOld->addColumn('bar_id', 'integer'); |
34
|
|
|
|
35
|
|
|
$this->schemaManager->createTable($tableOld); |
36
|
|
|
$tableFetched = $this->schemaManager->listTableDetails('switch_primary_key_columns'); |
37
|
|
|
$tableNew = clone $tableFetched; |
38
|
|
|
$tableNew->setPrimaryKey(['bar_id', 'foo_id']); |
39
|
|
|
|
40
|
|
|
$comparator = new Comparator(); |
41
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($tableFetched, $tableNew)); |
42
|
|
|
|
43
|
|
|
$table = $this->schemaManager->listTableDetails('switch_primary_key_columns'); |
44
|
|
|
$primaryKey = $table->getPrimaryKeyColumns(); |
45
|
|
|
|
46
|
|
|
self::assertCount(2, $primaryKey); |
47
|
|
|
self::assertContains('bar_id', $primaryKey); |
48
|
|
|
self::assertContains('foo_id', $primaryKey); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testDiffTableBug() |
52
|
|
|
{ |
53
|
|
|
$schema = new Schema(); |
54
|
|
|
$table = $schema->createTable('diffbug_routing_translations'); |
55
|
|
|
$table->addColumn('id', 'integer'); |
56
|
|
|
$table->addColumn('route', 'string'); |
57
|
|
|
$table->addColumn('locale', 'string'); |
58
|
|
|
$table->addColumn('attribute', 'string'); |
59
|
|
|
$table->addColumn('localized_value', 'string'); |
60
|
|
|
$table->addColumn('original_value', 'string'); |
61
|
|
|
$table->setPrimaryKey(['id']); |
62
|
|
|
$table->addUniqueIndex(['route', 'locale', 'attribute']); |
63
|
|
|
$table->addIndex(['localized_value']); // this is much more selective than the unique index |
64
|
|
|
|
65
|
|
|
$this->schemaManager->createTable($table); |
66
|
|
|
$tableFetched = $this->schemaManager->listTableDetails('diffbug_routing_translations'); |
67
|
|
|
|
68
|
|
|
$comparator = new Comparator(); |
69
|
|
|
$diff = $comparator->diffTable($tableFetched, $table); |
70
|
|
|
|
71
|
|
|
self::assertFalse($diff, 'no changes expected.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testFulltextIndex() |
75
|
|
|
{ |
76
|
|
|
$table = new Table('fulltext_index'); |
77
|
|
|
$table->addColumn('text', 'text'); |
78
|
|
|
$table->addIndex(['text'], 'f_index'); |
79
|
|
|
$table->addOption('engine', 'MyISAM'); |
80
|
|
|
|
81
|
|
|
$index = $table->getIndex('f_index'); |
82
|
|
|
$index->addFlag('fulltext'); |
83
|
|
|
|
84
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
85
|
|
|
|
86
|
|
|
$indexes = $this->schemaManager->listTableIndexes('fulltext_index'); |
87
|
|
|
self::assertArrayHasKey('f_index', $indexes); |
88
|
|
|
self::assertTrue($indexes['f_index']->hasFlag('fulltext')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testSpatialIndex() |
92
|
|
|
{ |
93
|
|
|
$table = new Table('spatial_index'); |
94
|
|
|
$table->addColumn('point', 'point'); |
95
|
|
|
$table->addIndex(['point'], 's_index'); |
96
|
|
|
$table->addOption('engine', 'MyISAM'); |
97
|
|
|
|
98
|
|
|
$index = $table->getIndex('s_index'); |
99
|
|
|
$index->addFlag('spatial'); |
100
|
|
|
|
101
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
102
|
|
|
|
103
|
|
|
$indexes = $this->schemaManager->listTableIndexes('spatial_index'); |
104
|
|
|
self::assertArrayHasKey('s_index', $indexes); |
105
|
|
|
self::assertTrue($indexes['s_index']->hasFlag('spatial')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testIndexWithLength() : void |
109
|
|
|
{ |
110
|
|
|
$table = new Table('index_length'); |
111
|
|
|
$table->addColumn('text', 'string', ['length' => 255]); |
112
|
|
|
$table->addIndex(['text'], 'text_index', [], ['lengths' => [128]]); |
113
|
|
|
|
114
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
115
|
|
|
|
116
|
|
|
$indexes = $this->schemaManager->listTableIndexes('index_length'); |
117
|
|
|
self::assertArrayHasKey('text_index', $indexes); |
118
|
|
|
self::assertSame([128], $indexes['text_index']->getOption('lengths')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @group DBAL-400 |
123
|
|
|
*/ |
124
|
|
|
public function testAlterTableAddPrimaryKey() |
125
|
|
|
{ |
126
|
|
|
$table = new Table('alter_table_add_pk'); |
127
|
|
|
$table->addColumn('id', 'integer'); |
128
|
|
|
$table->addColumn('foo', 'integer'); |
129
|
|
|
$table->addIndex(['id'], 'idx_id'); |
130
|
|
|
|
131
|
|
|
$this->schemaManager->createTable($table); |
132
|
|
|
|
133
|
|
|
$comparator = new Comparator(); |
134
|
|
|
$diffTable = clone $table; |
135
|
|
|
|
136
|
|
|
$diffTable->dropIndex('idx_id'); |
137
|
|
|
$diffTable->setPrimaryKey(['id']); |
138
|
|
|
|
139
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($table, $diffTable)); |
|
|
|
|
140
|
|
|
|
141
|
|
|
$table = $this->schemaManager->listTableDetails('alter_table_add_pk'); |
142
|
|
|
|
143
|
|
|
self::assertFalse($table->hasIndex('idx_id')); |
144
|
|
|
self::assertTrue($table->hasPrimaryKey()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @group DBAL-464 |
149
|
|
|
*/ |
150
|
|
|
public function testDropPrimaryKeyWithAutoincrementColumn() |
151
|
|
|
{ |
152
|
|
|
$table = new Table('drop_primary_key'); |
153
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]); |
154
|
|
|
$table->addColumn('foo', 'integer'); |
155
|
|
|
$table->setPrimaryKey(['id', 'foo']); |
156
|
|
|
|
157
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
158
|
|
|
|
159
|
|
|
$diffTable = clone $table; |
160
|
|
|
|
161
|
|
|
$diffTable->dropPrimaryKey(); |
162
|
|
|
|
163
|
|
|
$comparator = new Comparator(); |
164
|
|
|
|
165
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($table, $diffTable)); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$table = $this->schemaManager->listTableDetails('drop_primary_key'); |
168
|
|
|
|
169
|
|
|
self::assertFalse($table->hasPrimaryKey()); |
170
|
|
|
self::assertFalse($table->getColumn('id')->getAutoincrement()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @group DBAL-789 |
175
|
|
|
*/ |
176
|
|
|
public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes() |
177
|
|
|
{ |
178
|
|
|
if ($this->schemaManager->getDatabasePlatform() instanceof MariaDb1027Platform) { |
179
|
|
|
$this->markTestSkipped( |
180
|
|
|
'MariaDb102Platform supports default values for BLOB and TEXT columns and will propagate values' |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$table = new Table('text_blob_default_value'); |
185
|
|
|
$table->addColumn('def_text', 'text', ['default' => 'def']); |
186
|
|
|
$table->addColumn('def_text_null', 'text', ['notnull' => false, 'default' => 'def']); |
187
|
|
|
$table->addColumn('def_blob', 'blob', ['default' => 'def']); |
188
|
|
|
$table->addColumn('def_blob_null', 'blob', ['notnull' => false, 'default' => 'def']); |
189
|
|
|
|
190
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
191
|
|
|
|
192
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('text_blob_default_value'); |
193
|
|
|
|
194
|
|
|
self::assertNull($onlineTable->getColumn('def_text')->getDefault()); |
195
|
|
|
self::assertNull($onlineTable->getColumn('def_text_null')->getDefault()); |
196
|
|
|
self::assertFalse($onlineTable->getColumn('def_text_null')->getNotnull()); |
197
|
|
|
self::assertNull($onlineTable->getColumn('def_blob')->getDefault()); |
198
|
|
|
self::assertNull($onlineTable->getColumn('def_blob_null')->getDefault()); |
199
|
|
|
self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull()); |
200
|
|
|
|
201
|
|
|
$comparator = new Comparator(); |
202
|
|
|
|
203
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($table, $onlineTable)); |
204
|
|
|
|
205
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('text_blob_default_value'); |
206
|
|
|
|
207
|
|
|
self::assertNull($onlineTable->getColumn('def_text')->getDefault()); |
208
|
|
|
self::assertNull($onlineTable->getColumn('def_text_null')->getDefault()); |
209
|
|
|
self::assertFalse($onlineTable->getColumn('def_text_null')->getNotnull()); |
210
|
|
|
self::assertNull($onlineTable->getColumn('def_blob')->getDefault()); |
211
|
|
|
self::assertNull($onlineTable->getColumn('def_blob_null')->getDefault()); |
212
|
|
|
self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull()); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testColumnCharset() |
216
|
|
|
{ |
217
|
|
|
$table = new Table('test_column_charset'); |
218
|
|
|
$table->addColumn('id', 'integer'); |
219
|
|
|
$table->addColumn('no_charset', 'text'); |
220
|
|
|
$table->addColumn('foo', 'text')->setPlatformOption('charset', 'ascii'); |
221
|
|
|
$table->addColumn('bar', 'text')->setPlatformOption('charset', 'latin1'); |
222
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
223
|
|
|
|
224
|
|
|
$columns = $this->schemaManager->listTableColumns('test_column_charset'); |
225
|
|
|
|
226
|
|
|
self::assertFalse($columns['id']->hasPlatformOption('charset')); |
227
|
|
|
self::assertEquals('utf8', $columns['no_charset']->getPlatformOption('charset')); |
228
|
|
|
self::assertEquals('ascii', $columns['foo']->getPlatformOption('charset')); |
229
|
|
|
self::assertEquals('latin1', $columns['bar']->getPlatformOption('charset')); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testAlterColumnCharset() |
233
|
|
|
{ |
234
|
|
|
$tableName = 'test_alter_column_charset'; |
235
|
|
|
|
236
|
|
|
$table = new Table($tableName); |
237
|
|
|
$table->addColumn('col_text', 'text')->setPlatformOption('charset', 'utf8'); |
238
|
|
|
|
239
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
240
|
|
|
|
241
|
|
|
$diffTable = clone $table; |
242
|
|
|
$diffTable->getColumn('col_text')->setPlatformOption('charset', 'ascii'); |
243
|
|
|
|
244
|
|
|
$comparator = new Comparator(); |
245
|
|
|
|
246
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($table, $diffTable)); |
|
|
|
|
247
|
|
|
|
248
|
|
|
$table = $this->schemaManager->listTableDetails($tableName); |
249
|
|
|
|
250
|
|
|
self::assertEquals('ascii', $table->getColumn('col_text')->getPlatformOption('charset')); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testColumnCharsetChange() |
254
|
|
|
{ |
255
|
|
|
$table = new Table('test_column_charset_change'); |
256
|
|
|
$table->addColumn('col_string', 'string')->setLength(100)->setNotnull(true)->setPlatformOption('charset', 'utf8'); |
257
|
|
|
|
258
|
|
|
$diffTable = clone $table; |
259
|
|
|
$diffTable->getColumn('col_string')->setPlatformOption('charset', 'ascii'); |
260
|
|
|
|
261
|
|
|
$fromSchema = new Schema([$table]); |
262
|
|
|
$toSchema = new Schema([$diffTable]); |
263
|
|
|
|
264
|
|
|
$diff = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform()); |
265
|
|
|
self::assertContains('ALTER TABLE test_column_charset_change CHANGE col_string col_string VARCHAR(100) CHARACTER SET ascii NOT NULL', $diff); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testColumnCollation() |
269
|
|
|
{ |
270
|
|
|
$table = new Table('test_collation'); |
271
|
|
|
$table->addOption('collate', $collation = 'latin1_swedish_ci'); |
272
|
|
|
$table->addOption('charset', 'latin1'); |
273
|
|
|
$table->addColumn('id', 'integer'); |
274
|
|
|
$table->addColumn('text', 'text'); |
275
|
|
|
$table->addColumn('foo', 'text')->setPlatformOption('collation', 'latin1_swedish_ci'); |
276
|
|
|
$table->addColumn('bar', 'text')->setPlatformOption('collation', 'utf8_general_ci'); |
277
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
278
|
|
|
|
279
|
|
|
$columns = $this->schemaManager->listTableColumns('test_collation'); |
280
|
|
|
|
281
|
|
|
self::assertArrayNotHasKey('collation', $columns['id']->getPlatformOptions()); |
282
|
|
|
self::assertEquals('latin1_swedish_ci', $columns['text']->getPlatformOption('collation')); |
283
|
|
|
self::assertEquals('latin1_swedish_ci', $columns['foo']->getPlatformOption('collation')); |
284
|
|
|
self::assertEquals('utf8_general_ci', $columns['bar']->getPlatformOption('collation')); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @group DBAL-843 |
289
|
|
|
*/ |
290
|
|
|
public function testListLobTypeColumns() |
291
|
|
|
{ |
292
|
|
|
$tableName = 'lob_type_columns'; |
293
|
|
|
$table = new Table($tableName); |
294
|
|
|
|
295
|
|
|
$table->addColumn('col_tinytext', 'text', ['length' => MySqlPlatform::LENGTH_LIMIT_TINYTEXT]); |
296
|
|
|
$table->addColumn('col_text', 'text', ['length' => MySqlPlatform::LENGTH_LIMIT_TEXT]); |
297
|
|
|
$table->addColumn('col_mediumtext', 'text', ['length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT]); |
298
|
|
|
$table->addColumn('col_longtext', 'text'); |
299
|
|
|
|
300
|
|
|
$table->addColumn('col_tinyblob', 'text', ['length' => MySqlPlatform::LENGTH_LIMIT_TINYBLOB]); |
301
|
|
|
$table->addColumn('col_blob', 'blob', ['length' => MySqlPlatform::LENGTH_LIMIT_BLOB]); |
302
|
|
|
$table->addColumn('col_mediumblob', 'blob', ['length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB]); |
303
|
|
|
$table->addColumn('col_longblob', 'blob'); |
304
|
|
|
|
305
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
306
|
|
|
|
307
|
|
|
$platform = $this->schemaManager->getDatabasePlatform(); |
308
|
|
|
$offlineColumns = $table->getColumns(); |
309
|
|
|
$onlineColumns = $this->schemaManager->listTableColumns($tableName); |
310
|
|
|
|
311
|
|
|
self::assertSame( |
312
|
|
|
$platform->getClobTypeDeclarationSQL($offlineColumns['col_tinytext']->toArray()), |
313
|
|
|
$platform->getClobTypeDeclarationSQL($onlineColumns['col_tinytext']->toArray()) |
314
|
|
|
); |
315
|
|
|
self::assertSame( |
316
|
|
|
$platform->getClobTypeDeclarationSQL($offlineColumns['col_text']->toArray()), |
317
|
|
|
$platform->getClobTypeDeclarationSQL($onlineColumns['col_text']->toArray()) |
318
|
|
|
); |
319
|
|
|
self::assertSame( |
320
|
|
|
$platform->getClobTypeDeclarationSQL($offlineColumns['col_mediumtext']->toArray()), |
321
|
|
|
$platform->getClobTypeDeclarationSQL($onlineColumns['col_mediumtext']->toArray()) |
322
|
|
|
); |
323
|
|
|
self::assertSame( |
324
|
|
|
$platform->getClobTypeDeclarationSQL($offlineColumns['col_longtext']->toArray()), |
325
|
|
|
$platform->getClobTypeDeclarationSQL($onlineColumns['col_longtext']->toArray()) |
326
|
|
|
); |
327
|
|
|
|
328
|
|
|
self::assertSame( |
329
|
|
|
$platform->getBlobTypeDeclarationSQL($offlineColumns['col_tinyblob']->toArray()), |
330
|
|
|
$platform->getBlobTypeDeclarationSQL($onlineColumns['col_tinyblob']->toArray()) |
331
|
|
|
); |
332
|
|
|
self::assertSame( |
333
|
|
|
$platform->getBlobTypeDeclarationSQL($offlineColumns['col_blob']->toArray()), |
334
|
|
|
$platform->getBlobTypeDeclarationSQL($onlineColumns['col_blob']->toArray()) |
335
|
|
|
); |
336
|
|
|
self::assertSame( |
337
|
|
|
$platform->getBlobTypeDeclarationSQL($offlineColumns['col_mediumblob']->toArray()), |
338
|
|
|
$platform->getBlobTypeDeclarationSQL($onlineColumns['col_mediumblob']->toArray()) |
339
|
|
|
); |
340
|
|
|
self::assertSame( |
341
|
|
|
$platform->getBlobTypeDeclarationSQL($offlineColumns['col_longblob']->toArray()), |
342
|
|
|
$platform->getBlobTypeDeclarationSQL($onlineColumns['col_longblob']->toArray()) |
343
|
|
|
); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @group DBAL-423 |
348
|
|
|
*/ |
349
|
|
|
public function testDiffListGuidTableColumn() |
350
|
|
|
{ |
351
|
|
|
$offlineTable = new Table('list_guid_table_column'); |
352
|
|
|
$offlineTable->addColumn('col_guid', 'guid'); |
353
|
|
|
|
354
|
|
|
$this->schemaManager->dropAndCreateTable($offlineTable); |
355
|
|
|
|
356
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('list_guid_table_column'); |
357
|
|
|
|
358
|
|
|
$comparator = new Comparator(); |
359
|
|
|
|
360
|
|
|
self::assertFalse( |
361
|
|
|
$comparator->diffTable($offlineTable, $onlineTable), |
362
|
|
|
'No differences should be detected with the offline vs online schema.' |
363
|
|
|
); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @group DBAL-1082 |
368
|
|
|
*/ |
369
|
|
|
public function testListDecimalTypeColumns() |
370
|
|
|
{ |
371
|
|
|
$tableName = 'test_list_decimal_columns'; |
372
|
|
|
$table = new Table($tableName); |
373
|
|
|
|
374
|
|
|
$table->addColumn('col', 'decimal'); |
375
|
|
|
$table->addColumn('col_unsigned', 'decimal', ['unsigned' => true]); |
376
|
|
|
|
377
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
378
|
|
|
|
379
|
|
|
$columns = $this->schemaManager->listTableColumns($tableName); |
380
|
|
|
|
381
|
|
|
self::assertArrayHasKey('col', $columns); |
382
|
|
|
self::assertArrayHasKey('col_unsigned', $columns); |
383
|
|
|
self::assertFalse($columns['col']->getUnsigned()); |
384
|
|
|
self::assertTrue($columns['col_unsigned']->getUnsigned()); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @group DBAL-1082 |
389
|
|
|
*/ |
390
|
|
|
public function testListFloatTypeColumns() |
391
|
|
|
{ |
392
|
|
|
$tableName = 'test_list_float_columns'; |
393
|
|
|
$table = new Table($tableName); |
394
|
|
|
|
395
|
|
|
$table->addColumn('col', 'float'); |
396
|
|
|
$table->addColumn('col_unsigned', 'float', ['unsigned' => true]); |
397
|
|
|
|
398
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
399
|
|
|
|
400
|
|
|
$columns = $this->schemaManager->listTableColumns($tableName); |
401
|
|
|
|
402
|
|
|
self::assertArrayHasKey('col', $columns); |
403
|
|
|
self::assertArrayHasKey('col_unsigned', $columns); |
404
|
|
|
self::assertFalse($columns['col']->getUnsigned()); |
405
|
|
|
self::assertTrue($columns['col_unsigned']->getUnsigned()); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
public function testJsonColumnType() : void |
409
|
|
|
{ |
410
|
|
|
$table = new Table('test_mysql_json'); |
411
|
|
|
$table->addColumn('col_json', 'json'); |
412
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
413
|
|
|
|
414
|
|
|
$columns = $this->schemaManager->listTableColumns('test_mysql_json'); |
415
|
|
|
|
416
|
|
|
self::assertSame(Type::JSON, $columns['col_json']->getType()->getName()); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
public function testColumnDefaultCurrentTimestamp() : void |
420
|
|
|
{ |
421
|
|
|
$platform = $this->schemaManager->getDatabasePlatform(); |
422
|
|
|
|
423
|
|
|
$table = new Table('test_column_defaults_current_timestamp'); |
424
|
|
|
|
425
|
|
|
$currentTimeStampSql = $platform->getCurrentTimestampSQL(); |
426
|
|
|
|
427
|
|
|
$table->addColumn('col_datetime', 'datetime', ['notnull' => true, 'default' => $currentTimeStampSql]); |
428
|
|
|
$table->addColumn('col_datetime_nullable', 'datetime', ['default' => $currentTimeStampSql]); |
429
|
|
|
|
430
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
431
|
|
|
|
432
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_column_defaults_current_timestamp'); |
433
|
|
|
self::assertSame($currentTimeStampSql, $onlineTable->getColumn('col_datetime')->getDefault()); |
434
|
|
|
self::assertSame($currentTimeStampSql, $onlineTable->getColumn('col_datetime_nullable')->getDefault()); |
435
|
|
|
|
436
|
|
|
$comparator = new Comparator(); |
437
|
|
|
|
438
|
|
|
$diff = $comparator->diffTable($table, $onlineTable); |
439
|
|
|
self::assertFalse($diff, 'Tables should be identical with column defaults.'); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
public function testColumnDefaultsAreValid() |
443
|
|
|
{ |
444
|
|
|
$table = new Table('test_column_defaults_are_valid'); |
445
|
|
|
|
446
|
|
|
$currentTimeStampSql = $this->schemaManager->getDatabasePlatform()->getCurrentTimestampSQL(); |
447
|
|
|
$table->addColumn('col_datetime', 'datetime', ['default' => $currentTimeStampSql]); |
448
|
|
|
$table->addColumn('col_datetime_null', 'datetime', ['notnull' => false, 'default' => null]); |
449
|
|
|
$table->addColumn('col_int', 'integer', ['default' => 1]); |
450
|
|
|
$table->addColumn('col_neg_int', 'integer', ['default' => -1]); |
451
|
|
|
$table->addColumn('col_string', 'string', ['default' => 'A']); |
452
|
|
|
$table->addColumn('col_decimal', 'decimal', ['scale' => 3, 'precision' => 6, 'default' => -2.3]); |
453
|
|
|
$table->addColumn('col_date', 'date', ['default' => '2012-12-12']); |
454
|
|
|
|
455
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
456
|
|
|
|
457
|
|
|
$this->connection->executeUpdate( |
458
|
|
|
'INSERT INTO test_column_defaults_are_valid () VALUES()' |
459
|
|
|
); |
460
|
|
|
|
461
|
|
|
$row = $this->connection->fetchAssoc( |
462
|
|
|
'SELECT *, DATEDIFF(CURRENT_TIMESTAMP(), col_datetime) as diff_seconds FROM test_column_defaults_are_valid' |
463
|
|
|
); |
464
|
|
|
|
465
|
|
|
self::assertInstanceOf(DateTime::class, DateTime::createFromFormat('Y-m-d H:i:s', $row['col_datetime'])); |
466
|
|
|
self::assertNull($row['col_datetime_null']); |
467
|
|
|
self::assertSame('2012-12-12', $row['col_date']); |
468
|
|
|
self::assertSame('A', $row['col_string']); |
469
|
|
|
self::assertEquals(1, $row['col_int']); |
470
|
|
|
self::assertEquals(-1, $row['col_neg_int']); |
471
|
|
|
self::assertEquals('-2.300', $row['col_decimal']); |
472
|
|
|
self::assertLessThan(5, $row['diff_seconds']); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* MariaDB 10.2+ does support CURRENT_TIME and CURRENT_DATE as |
477
|
|
|
* column default values for time and date columns. |
478
|
|
|
* (Not supported on Mysql as of 5.7.19) |
479
|
|
|
* |
480
|
|
|
* Note that MariaDB 10.2+, when storing default in information_schema, |
481
|
|
|
* silently change CURRENT_TIMESTAMP as 'current_timestamp()', |
482
|
|
|
* CURRENT_TIME as 'currtime()' and CURRENT_DATE as 'currdate()'. |
483
|
|
|
* This test also ensure proper aliasing to not trigger a table diff. |
484
|
|
|
*/ |
485
|
|
|
public function testColumnDefaultValuesCurrentTimeAndDate() : void |
486
|
|
|
{ |
487
|
|
|
if (! $this->schemaManager->getDatabasePlatform() instanceof MariaDb1027Platform) { |
488
|
|
|
$this->markTestSkipped('Only relevant for MariaDb102Platform.'); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
$platform = $this->schemaManager->getDatabasePlatform(); |
492
|
|
|
|
493
|
|
|
$table = new Table('test_column_defaults_current_time_and_date'); |
494
|
|
|
|
495
|
|
|
$currentTimestampSql = $platform->getCurrentTimestampSQL(); |
496
|
|
|
$currentTimeSql = $platform->getCurrentTimeSQL(); |
497
|
|
|
$currentDateSql = $platform->getCurrentDateSQL(); |
498
|
|
|
|
499
|
|
|
$table->addColumn('col_datetime', 'datetime', ['default' => $currentTimestampSql]); |
500
|
|
|
$table->addColumn('col_date', 'date', ['default' => $currentDateSql]); |
501
|
|
|
$table->addColumn('col_time', 'time', ['default' => $currentTimeSql]); |
502
|
|
|
|
503
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
504
|
|
|
|
505
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_column_defaults_current_time_and_date'); |
506
|
|
|
|
507
|
|
|
self::assertSame($currentTimestampSql, $onlineTable->getColumn('col_datetime')->getDefault()); |
508
|
|
|
self::assertSame($currentDateSql, $onlineTable->getColumn('col_date')->getDefault()); |
509
|
|
|
self::assertSame($currentTimeSql, $onlineTable->getColumn('col_time')->getDefault()); |
510
|
|
|
|
511
|
|
|
$comparator = new Comparator(); |
512
|
|
|
|
513
|
|
|
$diff = $comparator->diffTable($table, $onlineTable); |
514
|
|
|
self::assertFalse($diff, 'Tables should be identical with column defauts time and date.'); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
public function testEnsureTableOptionsAreReflectedInMetadata() : void |
518
|
|
|
{ |
519
|
|
|
$this->connection->query('DROP TABLE IF EXISTS test_table_metadata'); |
520
|
|
|
|
521
|
|
|
$sql = <<<'SQL' |
522
|
|
|
CREATE TABLE test_table_metadata( |
523
|
|
|
col1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY |
524
|
|
|
) |
525
|
|
|
COLLATE utf8_general_ci |
526
|
|
|
ENGINE InnoDB |
527
|
|
|
ROW_FORMAT COMPRESSED |
528
|
|
|
COMMENT 'This is a test' |
529
|
|
|
AUTO_INCREMENT=42 |
530
|
|
|
PARTITION BY HASH (col1) |
531
|
|
|
SQL; |
532
|
|
|
|
533
|
|
|
$this->connection->query($sql); |
534
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_table_metadata'); |
535
|
|
|
|
536
|
|
|
self::assertEquals('InnoDB', $onlineTable->getOption('engine')); |
537
|
|
|
self::assertEquals('utf8_general_ci', $onlineTable->getOption('collation')); |
538
|
|
|
self::assertEquals(42, $onlineTable->getOption('autoincrement')); |
539
|
|
|
self::assertEquals('This is a test', $onlineTable->getOption('comment')); |
540
|
|
|
self::assertEquals([ |
541
|
|
|
'row_format' => 'COMPRESSED', |
542
|
|
|
'partitioned' => true, |
543
|
|
|
], $onlineTable->getOption('create_options')); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
public function testEnsureTableWithoutOptionsAreReflectedInMetadata() : void |
547
|
|
|
{ |
548
|
|
|
$this->connection->query('DROP TABLE IF EXISTS test_table_empty_metadata'); |
549
|
|
|
|
550
|
|
|
$this->connection->query('CREATE TABLE test_table_empty_metadata(col1 INT NOT NULL)'); |
551
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_table_empty_metadata'); |
552
|
|
|
|
553
|
|
|
self::assertNotEmpty($onlineTable->getOption('engine')); |
554
|
|
|
// collation could be set to default or not set, information_schema indicate a possibly null value |
555
|
|
|
self::assertFalse($onlineTable->hasOption('autoincrement')); |
556
|
|
|
self::assertEquals('', $onlineTable->getOption('comment')); |
557
|
|
|
self::assertEquals([], $onlineTable->getOption('create_options')); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
public function testParseNullCreateOptions() : void |
561
|
|
|
{ |
562
|
|
|
$table = $this->schemaManager->listTableDetails('sys.processlist'); |
563
|
|
|
|
564
|
|
|
self::assertEquals([], $table->getOption('create_options')); |
565
|
|
|
} |
566
|
|
|
} |
567
|
|
|
|