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
|
|
|
/** |
111
|
|
|
* @group DBAL-400 |
112
|
|
|
*/ |
113
|
|
|
public function testAlterTableAddPrimaryKey() |
114
|
|
|
{ |
115
|
|
|
$table = new Table('alter_table_add_pk'); |
116
|
|
|
$table->addColumn('id', 'integer'); |
117
|
|
|
$table->addColumn('foo', 'integer'); |
118
|
|
|
$table->addIndex(['id'], 'idx_id'); |
119
|
|
|
|
120
|
|
|
$this->schemaManager->createTable($table); |
121
|
|
|
|
122
|
|
|
$comparator = new Comparator(); |
123
|
|
|
$diffTable = clone $table; |
124
|
|
|
|
125
|
|
|
$diffTable->dropIndex('idx_id'); |
126
|
|
|
$diffTable->setPrimaryKey(['id']); |
127
|
|
|
|
128
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($table, $diffTable)); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$table = $this->schemaManager->listTableDetails('alter_table_add_pk'); |
131
|
|
|
|
132
|
|
|
self::assertFalse($table->hasIndex('idx_id')); |
133
|
|
|
self::assertTrue($table->hasPrimaryKey()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @group DBAL-464 |
138
|
|
|
*/ |
139
|
|
|
public function testDropPrimaryKeyWithAutoincrementColumn() |
140
|
|
|
{ |
141
|
|
|
$table = new Table('drop_primary_key'); |
142
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]); |
143
|
|
|
$table->addColumn('foo', 'integer'); |
144
|
|
|
$table->setPrimaryKey(['id', 'foo']); |
145
|
|
|
|
146
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
147
|
|
|
|
148
|
|
|
$diffTable = clone $table; |
149
|
|
|
|
150
|
|
|
$diffTable->dropPrimaryKey(); |
151
|
|
|
|
152
|
|
|
$comparator = new Comparator(); |
153
|
|
|
|
154
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($table, $diffTable)); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$table = $this->schemaManager->listTableDetails('drop_primary_key'); |
157
|
|
|
|
158
|
|
|
self::assertFalse($table->hasPrimaryKey()); |
159
|
|
|
self::assertFalse($table->getColumn('id')->getAutoincrement()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @group DBAL-789 |
164
|
|
|
*/ |
165
|
|
|
public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes() |
166
|
|
|
{ |
167
|
|
|
if ($this->schemaManager->getDatabasePlatform() instanceof MariaDb1027Platform) { |
168
|
|
|
$this->markTestSkipped( |
169
|
|
|
'MariaDb102Platform supports default values for BLOB and TEXT columns and will propagate values' |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$table = new Table('text_blob_default_value'); |
174
|
|
|
$table->addColumn('def_text', 'text', ['default' => 'def']); |
175
|
|
|
$table->addColumn('def_text_null', 'text', ['notnull' => false, 'default' => 'def']); |
176
|
|
|
$table->addColumn('def_blob', 'blob', ['default' => 'def']); |
177
|
|
|
$table->addColumn('def_blob_null', 'blob', ['notnull' => false, 'default' => 'def']); |
178
|
|
|
|
179
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
180
|
|
|
|
181
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('text_blob_default_value'); |
182
|
|
|
|
183
|
|
|
self::assertNull($onlineTable->getColumn('def_text')->getDefault()); |
184
|
|
|
self::assertNull($onlineTable->getColumn('def_text_null')->getDefault()); |
185
|
|
|
self::assertFalse($onlineTable->getColumn('def_text_null')->getNotnull()); |
186
|
|
|
self::assertNull($onlineTable->getColumn('def_blob')->getDefault()); |
187
|
|
|
self::assertNull($onlineTable->getColumn('def_blob_null')->getDefault()); |
188
|
|
|
self::assertFalse($onlineTable->getColumn('def_blob_null')->getNotnull()); |
189
|
|
|
|
190
|
|
|
$comparator = new Comparator(); |
191
|
|
|
|
192
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($table, $onlineTable)); |
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
|
|
|
|
204
|
|
|
public function testColumnCharset() |
205
|
|
|
{ |
206
|
|
|
$table = new Table('test_column_charset'); |
207
|
|
|
$table->addColumn('id', 'integer'); |
208
|
|
|
$table->addColumn('no_charset', 'text'); |
209
|
|
|
$table->addColumn('foo', 'text')->setPlatformOption('charset', 'ascii'); |
210
|
|
|
$table->addColumn('bar', 'text')->setPlatformOption('charset', 'latin1'); |
211
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
212
|
|
|
|
213
|
|
|
$columns = $this->schemaManager->listTableColumns('test_column_charset'); |
214
|
|
|
|
215
|
|
|
self::assertFalse($columns['id']->hasPlatformOption('charset')); |
216
|
|
|
self::assertEquals('utf8', $columns['no_charset']->getPlatformOption('charset')); |
217
|
|
|
self::assertEquals('ascii', $columns['foo']->getPlatformOption('charset')); |
218
|
|
|
self::assertEquals('latin1', $columns['bar']->getPlatformOption('charset')); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testAlterColumnCharset() |
222
|
|
|
{ |
223
|
|
|
$tableName = 'test_alter_column_charset'; |
224
|
|
|
|
225
|
|
|
$table = new Table($tableName); |
226
|
|
|
$table->addColumn('col_text', 'text')->setPlatformOption('charset', 'utf8'); |
227
|
|
|
|
228
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
229
|
|
|
|
230
|
|
|
$diffTable = clone $table; |
231
|
|
|
$diffTable->getColumn('col_text')->setPlatformOption('charset', 'ascii'); |
232
|
|
|
|
233
|
|
|
$comparator = new Comparator(); |
234
|
|
|
|
235
|
|
|
$this->schemaManager->alterTable($comparator->diffTable($table, $diffTable)); |
|
|
|
|
236
|
|
|
|
237
|
|
|
$table = $this->schemaManager->listTableDetails($tableName); |
238
|
|
|
|
239
|
|
|
self::assertEquals('ascii', $table->getColumn('col_text')->getPlatformOption('charset')); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testColumnCharsetChange() |
243
|
|
|
{ |
244
|
|
|
$table = new Table('test_column_charset_change'); |
245
|
|
|
$table->addColumn('col_string', 'string')->setLength(100)->setNotnull(true)->setPlatformOption('charset', 'utf8'); |
246
|
|
|
|
247
|
|
|
$diffTable = clone $table; |
248
|
|
|
$diffTable->getColumn('col_string')->setPlatformOption('charset', 'ascii'); |
249
|
|
|
|
250
|
|
|
$fromSchema = new Schema([$table]); |
251
|
|
|
$toSchema = new Schema([$diffTable]); |
252
|
|
|
|
253
|
|
|
$diff = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform()); |
254
|
|
|
self::assertContains('ALTER TABLE test_column_charset_change CHANGE col_string col_string VARCHAR(100) CHARACTER SET ascii NOT NULL', $diff); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function testColumnCollation() |
258
|
|
|
{ |
259
|
|
|
$table = new Table('test_collation'); |
260
|
|
|
$table->addOption('collate', $collation = 'latin1_swedish_ci'); |
261
|
|
|
$table->addOption('charset', 'latin1'); |
262
|
|
|
$table->addColumn('id', 'integer'); |
263
|
|
|
$table->addColumn('text', 'text'); |
264
|
|
|
$table->addColumn('foo', 'text')->setPlatformOption('collation', 'latin1_swedish_ci'); |
265
|
|
|
$table->addColumn('bar', 'text')->setPlatformOption('collation', 'utf8_general_ci'); |
266
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
267
|
|
|
|
268
|
|
|
$columns = $this->schemaManager->listTableColumns('test_collation'); |
269
|
|
|
|
270
|
|
|
self::assertArrayNotHasKey('collation', $columns['id']->getPlatformOptions()); |
271
|
|
|
self::assertEquals('latin1_swedish_ci', $columns['text']->getPlatformOption('collation')); |
272
|
|
|
self::assertEquals('latin1_swedish_ci', $columns['foo']->getPlatformOption('collation')); |
273
|
|
|
self::assertEquals('utf8_general_ci', $columns['bar']->getPlatformOption('collation')); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @group DBAL-843 |
278
|
|
|
*/ |
279
|
|
|
public function testListLobTypeColumns() |
280
|
|
|
{ |
281
|
|
|
$tableName = 'lob_type_columns'; |
282
|
|
|
$table = new Table($tableName); |
283
|
|
|
|
284
|
|
|
$table->addColumn('col_tinytext', 'text', ['length' => MySqlPlatform::LENGTH_LIMIT_TINYTEXT]); |
285
|
|
|
$table->addColumn('col_text', 'text', ['length' => MySqlPlatform::LENGTH_LIMIT_TEXT]); |
286
|
|
|
$table->addColumn('col_mediumtext', 'text', ['length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT]); |
287
|
|
|
$table->addColumn('col_longtext', 'text'); |
288
|
|
|
|
289
|
|
|
$table->addColumn('col_tinyblob', 'text', ['length' => MySqlPlatform::LENGTH_LIMIT_TINYBLOB]); |
290
|
|
|
$table->addColumn('col_blob', 'blob', ['length' => MySqlPlatform::LENGTH_LIMIT_BLOB]); |
291
|
|
|
$table->addColumn('col_mediumblob', 'blob', ['length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB]); |
292
|
|
|
$table->addColumn('col_longblob', 'blob'); |
293
|
|
|
|
294
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
295
|
|
|
|
296
|
|
|
$platform = $this->schemaManager->getDatabasePlatform(); |
297
|
|
|
$offlineColumns = $table->getColumns(); |
298
|
|
|
$onlineColumns = $this->schemaManager->listTableColumns($tableName); |
299
|
|
|
|
300
|
|
|
self::assertSame( |
301
|
|
|
$platform->getClobTypeDeclarationSQL($offlineColumns['col_tinytext']->toArray()), |
302
|
|
|
$platform->getClobTypeDeclarationSQL($onlineColumns['col_tinytext']->toArray()) |
303
|
|
|
); |
304
|
|
|
self::assertSame( |
305
|
|
|
$platform->getClobTypeDeclarationSQL($offlineColumns['col_text']->toArray()), |
306
|
|
|
$platform->getClobTypeDeclarationSQL($onlineColumns['col_text']->toArray()) |
307
|
|
|
); |
308
|
|
|
self::assertSame( |
309
|
|
|
$platform->getClobTypeDeclarationSQL($offlineColumns['col_mediumtext']->toArray()), |
310
|
|
|
$platform->getClobTypeDeclarationSQL($onlineColumns['col_mediumtext']->toArray()) |
311
|
|
|
); |
312
|
|
|
self::assertSame( |
313
|
|
|
$platform->getClobTypeDeclarationSQL($offlineColumns['col_longtext']->toArray()), |
314
|
|
|
$platform->getClobTypeDeclarationSQL($onlineColumns['col_longtext']->toArray()) |
315
|
|
|
); |
316
|
|
|
|
317
|
|
|
self::assertSame( |
318
|
|
|
$platform->getBlobTypeDeclarationSQL($offlineColumns['col_tinyblob']->toArray()), |
319
|
|
|
$platform->getBlobTypeDeclarationSQL($onlineColumns['col_tinyblob']->toArray()) |
320
|
|
|
); |
321
|
|
|
self::assertSame( |
322
|
|
|
$platform->getBlobTypeDeclarationSQL($offlineColumns['col_blob']->toArray()), |
323
|
|
|
$platform->getBlobTypeDeclarationSQL($onlineColumns['col_blob']->toArray()) |
324
|
|
|
); |
325
|
|
|
self::assertSame( |
326
|
|
|
$platform->getBlobTypeDeclarationSQL($offlineColumns['col_mediumblob']->toArray()), |
327
|
|
|
$platform->getBlobTypeDeclarationSQL($onlineColumns['col_mediumblob']->toArray()) |
328
|
|
|
); |
329
|
|
|
self::assertSame( |
330
|
|
|
$platform->getBlobTypeDeclarationSQL($offlineColumns['col_longblob']->toArray()), |
331
|
|
|
$platform->getBlobTypeDeclarationSQL($onlineColumns['col_longblob']->toArray()) |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @group DBAL-423 |
337
|
|
|
*/ |
338
|
|
|
public function testDiffListGuidTableColumn() |
339
|
|
|
{ |
340
|
|
|
$offlineTable = new Table('list_guid_table_column'); |
341
|
|
|
$offlineTable->addColumn('col_guid', 'guid'); |
342
|
|
|
|
343
|
|
|
$this->schemaManager->dropAndCreateTable($offlineTable); |
344
|
|
|
|
345
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('list_guid_table_column'); |
346
|
|
|
|
347
|
|
|
$comparator = new Comparator(); |
348
|
|
|
|
349
|
|
|
self::assertFalse( |
350
|
|
|
$comparator->diffTable($offlineTable, $onlineTable), |
351
|
|
|
'No differences should be detected with the offline vs online schema.' |
352
|
|
|
); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @group DBAL-1082 |
357
|
|
|
*/ |
358
|
|
|
public function testListDecimalTypeColumns() |
359
|
|
|
{ |
360
|
|
|
$tableName = 'test_list_decimal_columns'; |
361
|
|
|
$table = new Table($tableName); |
362
|
|
|
|
363
|
|
|
$table->addColumn('col', 'decimal'); |
364
|
|
|
$table->addColumn('col_unsigned', 'decimal', ['unsigned' => true]); |
365
|
|
|
|
366
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
367
|
|
|
|
368
|
|
|
$columns = $this->schemaManager->listTableColumns($tableName); |
369
|
|
|
|
370
|
|
|
self::assertArrayHasKey('col', $columns); |
371
|
|
|
self::assertArrayHasKey('col_unsigned', $columns); |
372
|
|
|
self::assertFalse($columns['col']->getUnsigned()); |
373
|
|
|
self::assertTrue($columns['col_unsigned']->getUnsigned()); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @group DBAL-1082 |
378
|
|
|
*/ |
379
|
|
|
public function testListFloatTypeColumns() |
380
|
|
|
{ |
381
|
|
|
$tableName = 'test_list_float_columns'; |
382
|
|
|
$table = new Table($tableName); |
383
|
|
|
|
384
|
|
|
$table->addColumn('col', 'float'); |
385
|
|
|
$table->addColumn('col_unsigned', 'float', ['unsigned' => true]); |
386
|
|
|
|
387
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
388
|
|
|
|
389
|
|
|
$columns = $this->schemaManager->listTableColumns($tableName); |
390
|
|
|
|
391
|
|
|
self::assertArrayHasKey('col', $columns); |
392
|
|
|
self::assertArrayHasKey('col_unsigned', $columns); |
393
|
|
|
self::assertFalse($columns['col']->getUnsigned()); |
394
|
|
|
self::assertTrue($columns['col_unsigned']->getUnsigned()); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
public function testJsonColumnType() : void |
398
|
|
|
{ |
399
|
|
|
$table = new Table('test_mysql_json'); |
400
|
|
|
$table->addColumn('col_json', 'json'); |
401
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
402
|
|
|
|
403
|
|
|
$columns = $this->schemaManager->listTableColumns('test_mysql_json'); |
404
|
|
|
|
405
|
|
|
self::assertSame(Type::JSON, $columns['col_json']->getType()->getName()); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
public function testColumnDefaultCurrentTimestamp() : void |
409
|
|
|
{ |
410
|
|
|
$platform = $this->schemaManager->getDatabasePlatform(); |
411
|
|
|
|
412
|
|
|
$table = new Table('test_column_defaults_current_timestamp'); |
413
|
|
|
|
414
|
|
|
$currentTimeStampSql = $platform->getCurrentTimestampSQL(); |
415
|
|
|
|
416
|
|
|
$table->addColumn('col_datetime', 'datetime', ['notnull' => true, 'default' => $currentTimeStampSql]); |
417
|
|
|
$table->addColumn('col_datetime_nullable', 'datetime', ['default' => $currentTimeStampSql]); |
418
|
|
|
|
419
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
420
|
|
|
|
421
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_column_defaults_current_timestamp'); |
422
|
|
|
self::assertSame($currentTimeStampSql, $onlineTable->getColumn('col_datetime')->getDefault()); |
423
|
|
|
self::assertSame($currentTimeStampSql, $onlineTable->getColumn('col_datetime_nullable')->getDefault()); |
424
|
|
|
|
425
|
|
|
$comparator = new Comparator(); |
426
|
|
|
|
427
|
|
|
$diff = $comparator->diffTable($table, $onlineTable); |
428
|
|
|
self::assertFalse($diff, 'Tables should be identical with column defaults.'); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
public function testColumnDefaultsAreValid() |
432
|
|
|
{ |
433
|
|
|
$table = new Table('test_column_defaults_are_valid'); |
434
|
|
|
|
435
|
|
|
$currentTimeStampSql = $this->schemaManager->getDatabasePlatform()->getCurrentTimestampSQL(); |
436
|
|
|
$table->addColumn('col_datetime', 'datetime', ['default' => $currentTimeStampSql]); |
437
|
|
|
$table->addColumn('col_datetime_null', 'datetime', ['notnull' => false, 'default' => null]); |
438
|
|
|
$table->addColumn('col_int', 'integer', ['default' => 1]); |
439
|
|
|
$table->addColumn('col_neg_int', 'integer', ['default' => -1]); |
440
|
|
|
$table->addColumn('col_string', 'string', ['default' => 'A']); |
441
|
|
|
$table->addColumn('col_decimal', 'decimal', ['scale' => 3, 'precision' => 6, 'default' => -2.3]); |
442
|
|
|
$table->addColumn('col_date', 'date', ['default' => '2012-12-12']); |
443
|
|
|
|
444
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
445
|
|
|
|
446
|
|
|
$this->connection->executeUpdate( |
447
|
|
|
'INSERT INTO test_column_defaults_are_valid () VALUES()' |
448
|
|
|
); |
449
|
|
|
|
450
|
|
|
$row = $this->connection->fetchAssoc( |
451
|
|
|
'SELECT *, DATEDIFF(CURRENT_TIMESTAMP(), col_datetime) as diff_seconds FROM test_column_defaults_are_valid' |
452
|
|
|
); |
453
|
|
|
|
454
|
|
|
self::assertInstanceOf(DateTime::class, DateTime::createFromFormat('Y-m-d H:i:s', $row['col_datetime'])); |
455
|
|
|
self::assertNull($row['col_datetime_null']); |
456
|
|
|
self::assertSame('2012-12-12', $row['col_date']); |
457
|
|
|
self::assertSame('A', $row['col_string']); |
458
|
|
|
self::assertEquals(1, $row['col_int']); |
459
|
|
|
self::assertEquals(-1, $row['col_neg_int']); |
460
|
|
|
self::assertEquals('-2.300', $row['col_decimal']); |
461
|
|
|
self::assertLessThan(5, $row['diff_seconds']); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* MariaDB 10.2+ does support CURRENT_TIME and CURRENT_DATE as |
466
|
|
|
* column default values for time and date columns. |
467
|
|
|
* (Not supported on Mysql as of 5.7.19) |
468
|
|
|
* |
469
|
|
|
* Note that MariaDB 10.2+, when storing default in information_schema, |
470
|
|
|
* silently change CURRENT_TIMESTAMP as 'current_timestamp()', |
471
|
|
|
* CURRENT_TIME as 'currtime()' and CURRENT_DATE as 'currdate()'. |
472
|
|
|
* This test also ensure proper aliasing to not trigger a table diff. |
473
|
|
|
*/ |
474
|
|
|
public function testColumnDefaultValuesCurrentTimeAndDate() : void |
475
|
|
|
{ |
476
|
|
|
if (! $this->schemaManager->getDatabasePlatform() instanceof MariaDb1027Platform) { |
477
|
|
|
$this->markTestSkipped('Only relevant for MariaDb102Platform.'); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
$platform = $this->schemaManager->getDatabasePlatform(); |
481
|
|
|
|
482
|
|
|
$table = new Table('test_column_defaults_current_time_and_date'); |
483
|
|
|
|
484
|
|
|
$currentTimestampSql = $platform->getCurrentTimestampSQL(); |
485
|
|
|
$currentTimeSql = $platform->getCurrentTimeSQL(); |
486
|
|
|
$currentDateSql = $platform->getCurrentDateSQL(); |
487
|
|
|
|
488
|
|
|
$table->addColumn('col_datetime', 'datetime', ['default' => $currentTimestampSql]); |
489
|
|
|
$table->addColumn('col_date', 'date', ['default' => $currentDateSql]); |
490
|
|
|
$table->addColumn('col_time', 'time', ['default' => $currentTimeSql]); |
491
|
|
|
|
492
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
493
|
|
|
|
494
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_column_defaults_current_time_and_date'); |
495
|
|
|
|
496
|
|
|
self::assertSame($currentTimestampSql, $onlineTable->getColumn('col_datetime')->getDefault()); |
497
|
|
|
self::assertSame($currentDateSql, $onlineTable->getColumn('col_date')->getDefault()); |
498
|
|
|
self::assertSame($currentTimeSql, $onlineTable->getColumn('col_time')->getDefault()); |
499
|
|
|
|
500
|
|
|
$comparator = new Comparator(); |
501
|
|
|
|
502
|
|
|
$diff = $comparator->diffTable($table, $onlineTable); |
503
|
|
|
self::assertFalse($diff, 'Tables should be identical with column defauts time and date.'); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Ensure default values (un-)escaping is properly done by mysql platforms. |
508
|
|
|
* The test is voluntarily relying on schema introspection due to current |
509
|
|
|
* doctrine limitations. Once #2850 is landed, this test can be removed. |
510
|
|
|
* |
511
|
|
|
* @see https://dev.mysql.com/doc/refman/5.7/en/string-literals.html |
512
|
|
|
*/ |
513
|
|
|
public function testEnsureDefaultsAreUnescapedFromSchemaIntrospection() : void |
514
|
|
|
{ |
515
|
|
|
$platform = $this->schemaManager->getDatabasePlatform(); |
516
|
|
|
$this->connection->query('DROP TABLE IF EXISTS test_column_defaults_with_create'); |
517
|
|
|
|
518
|
|
|
$escapeSequences = [ |
519
|
|
|
"\\0", // An ASCII NUL (X'00') character |
520
|
|
|
"\\'", |
521
|
|
|
"''", // Single quote |
522
|
|
|
'\\"', |
523
|
|
|
'""', // Double quote |
524
|
|
|
'\\b', // A backspace character |
525
|
|
|
'\\n', // A new-line character |
526
|
|
|
'\\r', // A carriage return character |
527
|
|
|
'\\t', // A tab character |
528
|
|
|
'\\Z', // ASCII 26 (Control+Z) |
529
|
|
|
'\\\\', // A backslash (\) character |
530
|
|
|
'\\%', // A percent (%) character |
531
|
|
|
'\\_', // An underscore (_) character |
532
|
|
|
]; |
533
|
|
|
|
534
|
|
|
$default = implode('+', $escapeSequences); |
535
|
|
|
|
536
|
|
|
$sql = sprintf( |
537
|
|
|
'CREATE TABLE test_column_defaults_with_create(col1 VARCHAR(255) NULL DEFAULT %s)', |
538
|
|
|
$platform->quoteStringLiteral($default) |
539
|
|
|
); |
540
|
|
|
$this->connection->query($sql); |
541
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_column_defaults_with_create'); |
542
|
|
|
self::assertSame($default, $onlineTable->getColumn('col1')->getDefault()); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
public function testEnsureTableOptionsAreReflectedInMetadata() : void |
546
|
|
|
{ |
547
|
|
|
$this->connection->query('DROP TABLE IF EXISTS test_table_metadata'); |
548
|
|
|
|
549
|
|
|
$sql = <<<'SQL' |
550
|
|
|
CREATE TABLE test_table_metadata( |
551
|
|
|
col1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY |
552
|
|
|
) |
553
|
|
|
COLLATE utf8_general_ci |
554
|
|
|
ENGINE InnoDB |
555
|
|
|
ROW_FORMAT COMPRESSED |
556
|
|
|
COMMENT 'This is a test' |
557
|
|
|
AUTO_INCREMENT=42 |
558
|
|
|
PARTITION BY HASH (col1) |
559
|
|
|
SQL; |
560
|
|
|
|
561
|
|
|
$this->connection->query($sql); |
562
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_table_metadata'); |
563
|
|
|
|
564
|
|
|
self::assertEquals('InnoDB', $onlineTable->getOption('engine')); |
565
|
|
|
self::assertEquals('utf8_general_ci', $onlineTable->getOption('collation')); |
566
|
|
|
self::assertEquals(42, $onlineTable->getOption('autoincrement')); |
567
|
|
|
self::assertEquals('This is a test', $onlineTable->getOption('comment')); |
568
|
|
|
self::assertEquals([ |
569
|
|
|
'row_format' => 'COMPRESSED', |
570
|
|
|
'partitioned' => true, |
571
|
|
|
], $onlineTable->getOption('create_options')); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
public function testEnsureTableWithoutOptionsAreReflectedInMetadata() : void |
575
|
|
|
{ |
576
|
|
|
$this->connection->query('DROP TABLE IF EXISTS test_table_empty_metadata'); |
577
|
|
|
|
578
|
|
|
$this->connection->query('CREATE TABLE test_table_empty_metadata(col1 INT NOT NULL)'); |
579
|
|
|
$onlineTable = $this->schemaManager->listTableDetails('test_table_empty_metadata'); |
580
|
|
|
|
581
|
|
|
self::assertNotEmpty($onlineTable->getOption('engine')); |
582
|
|
|
// collation could be set to default or not set, information_schema indicate a possibly null value |
583
|
|
|
self::assertFalse($onlineTable->hasOption('autoincrement')); |
584
|
|
|
self::assertEquals('', $onlineTable->getOption('comment')); |
585
|
|
|
self::assertEquals([], $onlineTable->getOption('create_options')); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
public function testParseNullCreateOptions() : void |
589
|
|
|
{ |
590
|
|
|
$table = $this->schemaManager->listTableDetails('sys.processlist'); |
591
|
|
|
|
592
|
|
|
self::assertEquals([], $table->getOption('create_options')); |
593
|
|
|
} |
594
|
|
|
} |
595
|
|
|
|