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