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