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