1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DBALException; |
6
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
7
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
8
|
|
|
use Doctrine\DBAL\Schema\Column; |
9
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
10
|
|
|
use Doctrine\DBAL\Schema\Index; |
11
|
|
|
use Doctrine\DBAL\Schema\SchemaException; |
12
|
|
|
use Doctrine\DBAL\Schema\Table; |
13
|
|
|
use Doctrine\DBAL\Types\Type; |
14
|
|
|
use Doctrine\Tests\DbalTestCase; |
15
|
|
|
use function array_shift; |
16
|
|
|
use function current; |
17
|
|
|
|
18
|
|
|
class TableTest extends DbalTestCase |
19
|
|
|
{ |
20
|
|
|
public function testCreateWithInvalidTableName() |
21
|
|
|
{ |
22
|
|
|
$this->expectException(DBALException::class); |
23
|
|
|
|
24
|
|
|
new Table(''); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testGetName() |
28
|
|
|
{ |
29
|
|
|
$table = new Table('foo', [], [], []); |
30
|
|
|
self::assertEquals('foo', $table->getName()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testColumns() |
34
|
|
|
{ |
35
|
|
|
$type = Type::getType('integer'); |
36
|
|
|
$columns = []; |
37
|
|
|
$columns[] = new Column('foo', $type); |
38
|
|
|
$columns[] = new Column('bar', $type); |
39
|
|
|
$table = new Table('foo', $columns, [], []); |
40
|
|
|
|
41
|
|
|
self::assertTrue($table->hasColumn('foo')); |
42
|
|
|
self::assertTrue($table->hasColumn('bar')); |
43
|
|
|
self::assertFalse($table->hasColumn('baz')); |
44
|
|
|
|
45
|
|
|
self::assertInstanceOf(Column::class, $table->getColumn('foo')); |
46
|
|
|
self::assertInstanceOf(Column::class, $table->getColumn('bar')); |
47
|
|
|
|
48
|
|
|
self::assertCount(2, $table->getColumns()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testColumnsCaseInsensitive() |
52
|
|
|
{ |
53
|
|
|
$table = new Table('foo'); |
54
|
|
|
$column = $table->addColumn('Foo', 'integer'); |
55
|
|
|
|
56
|
|
|
self::assertTrue($table->hasColumn('Foo')); |
57
|
|
|
self::assertTrue($table->hasColumn('foo')); |
58
|
|
|
self::assertTrue($table->hasColumn('FOO')); |
59
|
|
|
|
60
|
|
|
self::assertSame($column, $table->getColumn('Foo')); |
61
|
|
|
self::assertSame($column, $table->getColumn('foo')); |
62
|
|
|
self::assertSame($column, $table->getColumn('FOO')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testCreateColumn() |
66
|
|
|
{ |
67
|
|
|
$type = Type::getType('integer'); |
68
|
|
|
|
69
|
|
|
$table = new Table('foo'); |
70
|
|
|
|
71
|
|
|
self::assertFalse($table->hasColumn('bar')); |
72
|
|
|
$table->addColumn('bar', 'integer'); |
73
|
|
|
self::assertTrue($table->hasColumn('bar')); |
74
|
|
|
self::assertSame($type, $table->getColumn('bar')->getType()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testDropColumn() |
78
|
|
|
{ |
79
|
|
|
$type = Type::getType('integer'); |
80
|
|
|
$columns = []; |
81
|
|
|
$columns[] = new Column('foo', $type); |
82
|
|
|
$columns[] = new Column('bar', $type); |
83
|
|
|
$table = new Table('foo', $columns, [], []); |
84
|
|
|
|
85
|
|
|
self::assertTrue($table->hasColumn('foo')); |
86
|
|
|
self::assertTrue($table->hasColumn('bar')); |
87
|
|
|
|
88
|
|
|
$table->dropColumn('foo')->dropColumn('bar'); |
89
|
|
|
|
90
|
|
|
self::assertFalse($table->hasColumn('foo')); |
91
|
|
|
self::assertFalse($table->hasColumn('bar')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testGetUnknownColumnThrowsException() |
95
|
|
|
{ |
96
|
|
|
$this->expectException(SchemaException::class); |
97
|
|
|
|
98
|
|
|
$table = new Table('foo', [], [], []); |
99
|
|
|
$table->getColumn('unknown'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testAddColumnTwiceThrowsException() |
103
|
|
|
{ |
104
|
|
|
$this->expectException(SchemaException::class); |
105
|
|
|
|
106
|
|
|
$type = Type::getType('integer'); |
107
|
|
|
$columns = []; |
108
|
|
|
$columns[] = new Column('foo', $type); |
109
|
|
|
$columns[] = new Column('foo', $type); |
110
|
|
|
$table = new Table('foo', $columns, [], []); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testCreateIndex() |
114
|
|
|
{ |
115
|
|
|
$type = Type::getType('integer'); |
116
|
|
|
$columns = [new Column('foo', $type), new Column('bar', $type), new Column('baz', $type)]; |
117
|
|
|
$table = new Table('foo', $columns); |
118
|
|
|
|
119
|
|
|
$table->addIndex(['foo', 'bar'], 'foo_foo_bar_idx'); |
120
|
|
|
$table->addUniqueIndex(['bar', 'baz'], 'foo_bar_baz_uniq'); |
121
|
|
|
|
122
|
|
|
self::assertTrue($table->hasIndex('foo_foo_bar_idx')); |
123
|
|
|
self::assertTrue($table->hasIndex('foo_bar_baz_uniq')); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testIndexCaseInsensitive() |
127
|
|
|
{ |
128
|
|
|
$type = Type::getType('integer'); |
129
|
|
|
$columns = [ |
130
|
|
|
new Column('foo', $type), |
131
|
|
|
new Column('bar', $type), |
132
|
|
|
new Column('baz', $type), |
133
|
|
|
]; |
134
|
|
|
$table = new Table('foo', $columns); |
135
|
|
|
|
136
|
|
|
$table->addIndex(['foo', 'bar', 'baz'], 'Foo_Idx'); |
137
|
|
|
|
138
|
|
|
self::assertTrue($table->hasIndex('foo_idx')); |
139
|
|
|
self::assertTrue($table->hasIndex('Foo_Idx')); |
140
|
|
|
self::assertTrue($table->hasIndex('FOO_IDX')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testAddIndexes() |
144
|
|
|
{ |
145
|
|
|
$type = Type::getType('integer'); |
146
|
|
|
$columns = [ |
147
|
|
|
new Column('foo', $type), |
148
|
|
|
new Column('bar', $type), |
149
|
|
|
]; |
150
|
|
|
$indexes = [ |
151
|
|
|
new Index('the_primary', ['foo'], true, true), |
152
|
|
|
new Index('bar_idx', ['bar'], false, false), |
153
|
|
|
]; |
154
|
|
|
$table = new Table('foo', $columns, $indexes, []); |
155
|
|
|
|
156
|
|
|
self::assertTrue($table->hasIndex('the_primary')); |
157
|
|
|
self::assertTrue($table->hasIndex('bar_idx')); |
158
|
|
|
self::assertFalse($table->hasIndex('some_idx')); |
159
|
|
|
|
160
|
|
|
self::assertInstanceOf(Index::class, $table->getPrimaryKey()); |
161
|
|
|
self::assertInstanceOf(Index::class, $table->getIndex('the_primary')); |
162
|
|
|
self::assertInstanceOf(Index::class, $table->getIndex('bar_idx')); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testGetUnknownIndexThrowsException() |
166
|
|
|
{ |
167
|
|
|
$this->expectException(SchemaException::class); |
168
|
|
|
|
169
|
|
|
$table = new Table('foo', [], [], []); |
170
|
|
|
$table->getIndex('unknownIndex'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testAddTwoPrimaryThrowsException() |
174
|
|
|
{ |
175
|
|
|
$this->expectException(SchemaException::class); |
176
|
|
|
|
177
|
|
|
$type = Type::getType('integer'); |
178
|
|
|
$columns = [new Column('foo', $type), new Column('bar', $type)]; |
179
|
|
|
$indexes = [ |
180
|
|
|
new Index('the_primary', ['foo'], true, true), |
181
|
|
|
new Index('other_primary', ['bar'], true, true), |
182
|
|
|
]; |
183
|
|
|
$table = new Table('foo', $columns, $indexes, []); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testAddTwoIndexesWithSameNameThrowsException() |
187
|
|
|
{ |
188
|
|
|
$this->expectException(SchemaException::class); |
189
|
|
|
|
190
|
|
|
$type = Type::getType('integer'); |
191
|
|
|
$columns = [new Column('foo', $type), new Column('bar', $type)]; |
192
|
|
|
$indexes = [ |
193
|
|
|
new Index('an_idx', ['foo'], false, false), |
194
|
|
|
new Index('an_idx', ['bar'], false, false), |
195
|
|
|
]; |
196
|
|
|
$table = new Table('foo', $columns, $indexes, []); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testConstraints() |
200
|
|
|
{ |
201
|
|
|
$constraint = new ForeignKeyConstraint([], 'foo', []); |
202
|
|
|
|
203
|
|
|
$tableA = new Table('foo', [], [], [$constraint]); |
204
|
|
|
$constraints = $tableA->getForeignKeys(); |
205
|
|
|
|
206
|
|
|
self::assertCount(1, $constraints); |
207
|
|
|
self::assertSame($constraint, array_shift($constraints)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testOptions() |
211
|
|
|
{ |
212
|
|
|
$table = new Table('foo', [], [], [], false, ['foo' => 'bar']); |
|
|
|
|
213
|
|
|
|
214
|
|
|
self::assertTrue($table->hasOption('foo')); |
215
|
|
|
self::assertEquals('bar', $table->getOption('foo')); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testBuilderSetPrimaryKey() |
219
|
|
|
{ |
220
|
|
|
$table = new Table('foo'); |
221
|
|
|
|
222
|
|
|
$table->addColumn('bar', 'integer'); |
223
|
|
|
$table->setPrimaryKey(['bar']); |
224
|
|
|
|
225
|
|
|
self::assertTrue($table->hasIndex('primary')); |
226
|
|
|
self::assertInstanceOf(Index::class, $table->getPrimaryKey()); |
227
|
|
|
self::assertTrue($table->getIndex('primary')->isUnique()); |
228
|
|
|
self::assertTrue($table->getIndex('primary')->isPrimary()); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testBuilderAddUniqueIndex() |
232
|
|
|
{ |
233
|
|
|
$table = new Table('foo'); |
234
|
|
|
|
235
|
|
|
$table->addColumn('bar', 'integer'); |
236
|
|
|
$table->addUniqueIndex(['bar'], 'my_idx'); |
237
|
|
|
|
238
|
|
|
self::assertTrue($table->hasIndex('my_idx')); |
239
|
|
|
self::assertTrue($table->getIndex('my_idx')->isUnique()); |
240
|
|
|
self::assertFalse($table->getIndex('my_idx')->isPrimary()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testBuilderAddIndex() |
244
|
|
|
{ |
245
|
|
|
$table = new Table('foo'); |
246
|
|
|
|
247
|
|
|
$table->addColumn('bar', 'integer'); |
248
|
|
|
$table->addIndex(['bar'], 'my_idx'); |
249
|
|
|
|
250
|
|
|
self::assertTrue($table->hasIndex('my_idx')); |
251
|
|
|
self::assertFalse($table->getIndex('my_idx')->isUnique()); |
252
|
|
|
self::assertFalse($table->getIndex('my_idx')->isPrimary()); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testBuilderAddIndexWithInvalidNameThrowsException() |
256
|
|
|
{ |
257
|
|
|
$this->expectException(SchemaException::class); |
258
|
|
|
|
259
|
|
|
$table = new Table('foo'); |
260
|
|
|
$table->addColumn('bar', 'integer'); |
261
|
|
|
$table->addIndex(['bar'], 'invalid name %&/'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testBuilderAddIndexWithUnknownColumnThrowsException() |
265
|
|
|
{ |
266
|
|
|
$this->expectException(SchemaException::class); |
267
|
|
|
|
268
|
|
|
$table = new Table('foo'); |
269
|
|
|
$table->addIndex(['bar'], 'invalidName'); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testBuilderOptions() |
273
|
|
|
{ |
274
|
|
|
$table = new Table('foo'); |
275
|
|
|
$table->addOption('foo', 'bar'); |
276
|
|
|
self::assertTrue($table->hasOption('foo')); |
277
|
|
|
self::assertEquals('bar', $table->getOption('foo')); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function testAddForeignKeyConstraintUnknownLocalColumnThrowsException() |
281
|
|
|
{ |
282
|
|
|
$this->expectException(SchemaException::class); |
283
|
|
|
|
284
|
|
|
$table = new Table('foo'); |
285
|
|
|
$table->addColumn('id', 'integer'); |
286
|
|
|
|
287
|
|
|
$foreignTable = new Table('bar'); |
288
|
|
|
$foreignTable->addColumn('id', 'integer'); |
289
|
|
|
|
290
|
|
|
$table->addForeignKeyConstraint($foreignTable, ['foo'], ['id']); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function testAddForeignKeyConstraintUnknownForeignColumnThrowsException() |
294
|
|
|
{ |
295
|
|
|
$this->expectException(SchemaException::class); |
296
|
|
|
|
297
|
|
|
$table = new Table('foo'); |
298
|
|
|
$table->addColumn('id', 'integer'); |
299
|
|
|
|
300
|
|
|
$foreignTable = new Table('bar'); |
301
|
|
|
$foreignTable->addColumn('id', 'integer'); |
302
|
|
|
|
303
|
|
|
$table->addForeignKeyConstraint($foreignTable, ['id'], ['foo']); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function testAddForeignKeyConstraint() |
307
|
|
|
{ |
308
|
|
|
$table = new Table('foo'); |
309
|
|
|
$table->addColumn('id', 'integer'); |
310
|
|
|
|
311
|
|
|
$foreignTable = new Table('bar'); |
312
|
|
|
$foreignTable->addColumn('id', 'integer'); |
313
|
|
|
|
314
|
|
|
$table->addForeignKeyConstraint($foreignTable, ['id'], ['id'], ['foo' => 'bar']); |
315
|
|
|
|
316
|
|
|
$constraints = $table->getForeignKeys(); |
317
|
|
|
self::assertCount(1, $constraints); |
318
|
|
|
$constraint = current($constraints); |
319
|
|
|
|
320
|
|
|
self::assertInstanceOf(ForeignKeyConstraint::class, $constraint); |
321
|
|
|
|
322
|
|
|
self::assertTrue($constraint->hasOption('foo')); |
323
|
|
|
self::assertEquals('bar', $constraint->getOption('foo')); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function testAddIndexWithCaseSensitiveColumnProblem() |
327
|
|
|
{ |
328
|
|
|
$table = new Table('foo'); |
329
|
|
|
$table->addColumn('id', 'integer'); |
330
|
|
|
|
331
|
|
|
$table->addIndex(['ID'], 'my_idx'); |
332
|
|
|
|
333
|
|
|
self::assertTrue($table->hasIndex('my_idx')); |
334
|
|
|
self::assertEquals(['ID'], $table->getIndex('my_idx')->getColumns()); |
335
|
|
|
self::assertTrue($table->getIndex('my_idx')->spansColumns(['id'])); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function testAddPrimaryKeyColumnsAreExplicitlySetToNotNull() |
339
|
|
|
{ |
340
|
|
|
$table = new Table('foo'); |
341
|
|
|
$column = $table->addColumn('id', 'integer', ['notnull' => false]); |
342
|
|
|
|
343
|
|
|
self::assertFalse($column->getNotnull()); |
344
|
|
|
|
345
|
|
|
$table->setPrimaryKey(['id']); |
346
|
|
|
|
347
|
|
|
self::assertTrue($column->getNotnull()); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @group DDC-133 |
352
|
|
|
*/ |
353
|
|
|
public function testAllowImplicitSchemaTableInAutogeneratedIndexNames() |
354
|
|
|
{ |
355
|
|
|
$table = new Table('foo.bar'); |
356
|
|
|
$table->addColumn('baz', 'integer', []); |
357
|
|
|
$table->addIndex(['baz']); |
358
|
|
|
|
359
|
|
|
self::assertCount(1, $table->getIndexes()); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @group DBAL-50 |
364
|
|
|
*/ |
365
|
|
|
public function testAddForeignKeyIndexImplicitly() |
366
|
|
|
{ |
367
|
|
|
$table = new Table('foo'); |
368
|
|
|
$table->addColumn('id', 'integer'); |
369
|
|
|
|
370
|
|
|
$foreignTable = new Table('bar'); |
371
|
|
|
$foreignTable->addColumn('id', 'integer'); |
372
|
|
|
|
373
|
|
|
$table->addForeignKeyConstraint($foreignTable, ['id'], ['id'], ['foo' => 'bar']); |
374
|
|
|
|
375
|
|
|
$indexes = $table->getIndexes(); |
376
|
|
|
self::assertCount(1, $indexes); |
377
|
|
|
$index = current($indexes); |
378
|
|
|
|
379
|
|
|
self::assertTrue($table->hasIndex($index->getName())); |
380
|
|
|
self::assertEquals(['id'], $index->getColumns()); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @group DBAL-1063 |
385
|
|
|
*/ |
386
|
|
|
public function testAddForeignKeyDoesNotCreateDuplicateIndex() |
387
|
|
|
{ |
388
|
|
|
$table = new Table('foo'); |
389
|
|
|
$table->addColumn('bar', 'integer'); |
390
|
|
|
$table->addIndex(['bar'], 'bar_idx'); |
391
|
|
|
|
392
|
|
|
$foreignTable = new Table('bar'); |
393
|
|
|
$foreignTable->addColumn('foo', 'integer'); |
394
|
|
|
|
395
|
|
|
$table->addForeignKeyConstraint($foreignTable, ['bar'], ['foo']); |
396
|
|
|
|
397
|
|
|
self::assertCount(1, $table->getIndexes()); |
398
|
|
|
self::assertTrue($table->hasIndex('bar_idx')); |
399
|
|
|
self::assertSame(['bar'], $table->getIndex('bar_idx')->getColumns()); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @group DBAL-1063 |
404
|
|
|
*/ |
405
|
|
|
public function testAddForeignKeyAddsImplicitIndexIfIndexColumnsDoNotSpan() |
406
|
|
|
{ |
407
|
|
|
$table = new Table('foo'); |
408
|
|
|
$table->addColumn('bar', 'integer'); |
409
|
|
|
$table->addColumn('baz', 'string'); |
410
|
|
|
$table->addColumn('bloo', 'string'); |
411
|
|
|
$table->addIndex(['baz', 'bar'], 'composite_idx'); |
412
|
|
|
$table->addIndex(['bar', 'baz', 'bloo'], 'full_idx'); |
413
|
|
|
|
414
|
|
|
$foreignTable = new Table('bar'); |
415
|
|
|
$foreignTable->addColumn('foo', 'integer'); |
416
|
|
|
$foreignTable->addColumn('baz', 'string'); |
417
|
|
|
|
418
|
|
|
$table->addForeignKeyConstraint($foreignTable, ['bar', 'baz'], ['foo', 'baz']); |
419
|
|
|
|
420
|
|
|
self::assertCount(3, $table->getIndexes()); |
421
|
|
|
self::assertTrue($table->hasIndex('composite_idx')); |
422
|
|
|
self::assertTrue($table->hasIndex('full_idx')); |
423
|
|
|
self::assertTrue($table->hasIndex('idx_8c73652176ff8caa78240498')); |
424
|
|
|
self::assertSame(['baz', 'bar'], $table->getIndex('composite_idx')->getColumns()); |
425
|
|
|
self::assertSame(['bar', 'baz', 'bloo'], $table->getIndex('full_idx')->getColumns()); |
426
|
|
|
self::assertSame(['bar', 'baz'], $table->getIndex('idx_8c73652176ff8caa78240498')->getColumns()); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @group DBAL-50 |
431
|
|
|
* @group DBAL-1063 |
432
|
|
|
*/ |
433
|
|
|
public function testOverrulingIndexDoesNotDropOverruledIndex() |
434
|
|
|
{ |
435
|
|
|
$table = new Table('bar'); |
436
|
|
|
$table->addColumn('baz', 'integer', []); |
437
|
|
|
$table->addIndex(['baz']); |
438
|
|
|
|
439
|
|
|
$indexes = $table->getIndexes(); |
440
|
|
|
self::assertCount(1, $indexes); |
441
|
|
|
$index = current($indexes); |
442
|
|
|
|
443
|
|
|
$table->addUniqueIndex(['baz']); |
444
|
|
|
self::assertCount(2, $table->getIndexes()); |
445
|
|
|
self::assertTrue($table->hasIndex($index->getName())); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* @group DBAL-1063 |
450
|
|
|
*/ |
451
|
|
|
public function testAllowsAddingDuplicateIndexesBasedOnColumns() |
452
|
|
|
{ |
453
|
|
|
$table = new Table('foo'); |
454
|
|
|
$table->addColumn('bar', 'integer'); |
455
|
|
|
$table->addIndex(['bar'], 'bar_idx'); |
456
|
|
|
$table->addIndex(['bar'], 'duplicate_idx'); |
457
|
|
|
|
458
|
|
|
self::assertCount(2, $table->getIndexes()); |
459
|
|
|
self::assertTrue($table->hasIndex('bar_idx')); |
460
|
|
|
self::assertTrue($table->hasIndex('duplicate_idx')); |
461
|
|
|
self::assertSame(['bar'], $table->getIndex('bar_idx')->getColumns()); |
462
|
|
|
self::assertSame(['bar'], $table->getIndex('duplicate_idx')->getColumns()); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* @group DBAL-1063 |
467
|
|
|
*/ |
468
|
|
|
public function testAllowsAddingFulfillingIndexesBasedOnColumns() |
469
|
|
|
{ |
470
|
|
|
$table = new Table('foo'); |
471
|
|
|
$table->addColumn('bar', 'integer'); |
472
|
|
|
$table->addColumn('baz', 'string'); |
473
|
|
|
$table->addIndex(['bar'], 'bar_idx'); |
474
|
|
|
$table->addIndex(['bar', 'baz'], 'fulfilling_idx'); |
475
|
|
|
|
476
|
|
|
self::assertCount(2, $table->getIndexes()); |
477
|
|
|
self::assertTrue($table->hasIndex('bar_idx')); |
478
|
|
|
self::assertTrue($table->hasIndex('fulfilling_idx')); |
479
|
|
|
self::assertSame(['bar'], $table->getIndex('bar_idx')->getColumns()); |
480
|
|
|
self::assertSame(['bar', 'baz'], $table->getIndex('fulfilling_idx')->getColumns()); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* @group DBAL-50 |
485
|
|
|
* @group DBAL-1063 |
486
|
|
|
*/ |
487
|
|
|
public function testPrimaryKeyOverrulingUniqueIndexDoesNotDropUniqueIndex() |
488
|
|
|
{ |
489
|
|
|
$table = new Table('bar'); |
490
|
|
|
$table->addColumn('baz', 'integer', []); |
491
|
|
|
$table->addUniqueIndex(['baz'], 'idx_unique'); |
492
|
|
|
|
493
|
|
|
$table->setPrimaryKey(['baz']); |
494
|
|
|
|
495
|
|
|
$indexes = $table->getIndexes(); |
496
|
|
|
self::assertCount(2, $indexes, 'Table should only contain both the primary key table index and the unique one, even though it was overruled.'); |
497
|
|
|
|
498
|
|
|
self::assertTrue($table->hasPrimaryKey()); |
499
|
|
|
self::assertTrue($table->hasIndex('idx_unique')); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConstraintIndex() |
503
|
|
|
{ |
504
|
|
|
$foreignTable = new Table('foreign'); |
505
|
|
|
$foreignTable->addColumn('id', 'integer'); |
506
|
|
|
|
507
|
|
|
$localTable = new Table('local'); |
508
|
|
|
$localTable->addColumn('id', 'integer'); |
509
|
|
|
$localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']); |
510
|
|
|
|
511
|
|
|
self::assertCount(1, $localTable->getIndexes()); |
512
|
|
|
|
513
|
|
|
$localTable->addIndex(['id'], 'explicit_idx'); |
514
|
|
|
|
515
|
|
|
self::assertCount(1, $localTable->getIndexes()); |
516
|
|
|
self::assertTrue($localTable->hasIndex('explicit_idx')); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstraintIndex() |
520
|
|
|
{ |
521
|
|
|
$foreignTable = new Table('foreign'); |
522
|
|
|
$foreignTable->addColumn('id', 'integer'); |
523
|
|
|
|
524
|
|
|
$localTable = new Table('local'); |
525
|
|
|
$localTable->addColumn('id', 'integer'); |
526
|
|
|
$localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']); |
527
|
|
|
|
528
|
|
|
self::assertCount(1, $localTable->getIndexes()); |
529
|
|
|
|
530
|
|
|
$localTable->addUniqueIndex(['id'], 'explicit_idx'); |
531
|
|
|
|
532
|
|
|
self::assertCount(1, $localTable->getIndexes()); |
533
|
|
|
self::assertTrue($localTable->hasIndex('explicit_idx')); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
public function testAddingFulfillingPrimaryKeyOverridesImplicitForeignKeyConstraintIndex() |
537
|
|
|
{ |
538
|
|
|
$foreignTable = new Table('foreign'); |
539
|
|
|
$foreignTable->addColumn('id', 'integer'); |
540
|
|
|
|
541
|
|
|
$localTable = new Table('local'); |
542
|
|
|
$localTable->addColumn('id', 'integer'); |
543
|
|
|
$localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']); |
544
|
|
|
|
545
|
|
|
self::assertCount(1, $localTable->getIndexes()); |
546
|
|
|
|
547
|
|
|
$localTable->setPrimaryKey(['id'], 'explicit_idx'); |
548
|
|
|
|
549
|
|
|
self::assertCount(1, $localTable->getIndexes()); |
550
|
|
|
self::assertTrue($localTable->hasIndex('explicit_idx')); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
public function testAddingFulfillingExplicitIndexOverridingImplicitForeignKeyConstraintIndexWithSameNameDoesNotThrowException() |
554
|
|
|
{ |
555
|
|
|
$foreignTable = new Table('foreign'); |
556
|
|
|
$foreignTable->addColumn('id', 'integer'); |
557
|
|
|
|
558
|
|
|
$localTable = new Table('local'); |
559
|
|
|
$localTable->addColumn('id', 'integer'); |
560
|
|
|
$localTable->addForeignKeyConstraint($foreignTable, ['id'], ['id']); |
561
|
|
|
|
562
|
|
|
self::assertCount(1, $localTable->getIndexes()); |
563
|
|
|
self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750')); |
564
|
|
|
|
565
|
|
|
$implicitIndex = $localTable->getIndex('IDX_8BD688E8BF396750'); |
566
|
|
|
|
567
|
|
|
$localTable->addIndex(['id'], 'IDX_8BD688E8BF396750'); |
568
|
|
|
|
569
|
|
|
self::assertCount(1, $localTable->getIndexes()); |
570
|
|
|
self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750')); |
571
|
|
|
self::assertNotSame($implicitIndex, $localTable->getIndex('IDX_8BD688E8BF396750')); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* @group DBAL-64 |
576
|
|
|
*/ |
577
|
|
|
public function testQuotedTableName() |
578
|
|
|
{ |
579
|
|
|
$table = new Table('`bar`'); |
580
|
|
|
|
581
|
|
|
$mysqlPlatform = new MySqlPlatform(); |
582
|
|
|
$sqlitePlatform = new SqlitePlatform(); |
583
|
|
|
|
584
|
|
|
self::assertEquals('bar', $table->getName()); |
585
|
|
|
self::assertEquals('`bar`', $table->getQuotedName($mysqlPlatform)); |
586
|
|
|
self::assertEquals('"bar"', $table->getQuotedName($sqlitePlatform)); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @group DBAL-79 |
591
|
|
|
*/ |
592
|
|
|
public function testTableHasPrimaryKey() |
593
|
|
|
{ |
594
|
|
|
$table = new Table('test'); |
595
|
|
|
|
596
|
|
|
self::assertFalse($table->hasPrimaryKey()); |
597
|
|
|
|
598
|
|
|
$table->addColumn('foo', 'integer'); |
599
|
|
|
$table->setPrimaryKey(['foo']); |
600
|
|
|
|
601
|
|
|
self::assertTrue($table->hasPrimaryKey()); |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* @group DBAL-91 |
606
|
|
|
*/ |
607
|
|
|
public function testAddIndexWithQuotedColumns() |
608
|
|
|
{ |
609
|
|
|
$table = new Table('test'); |
610
|
|
|
$table->addColumn('"foo"', 'integer'); |
611
|
|
|
$table->addColumn('bar', 'integer'); |
612
|
|
|
$table->addIndex(['"foo"', '"bar"']); |
613
|
|
|
|
614
|
|
|
self::assertTrue($table->columnsAreIndexed(['"foo"', '"bar"'])); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* @group DBAL-91 |
619
|
|
|
*/ |
620
|
|
|
public function testAddForeignKeyWithQuotedColumnsAndTable() |
621
|
|
|
{ |
622
|
|
|
$table = new Table('test'); |
623
|
|
|
$table->addColumn('"foo"', 'integer'); |
624
|
|
|
$table->addColumn('bar', 'integer'); |
625
|
|
|
$table->addForeignKeyConstraint('"boing"', ['"foo"', '"bar"'], ['id']); |
626
|
|
|
|
627
|
|
|
self::assertCount(1, $table->getForeignKeys()); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* @group DBAL-177 |
632
|
|
|
*/ |
633
|
|
|
public function testQuoteSchemaPrefixed() |
634
|
|
|
{ |
635
|
|
|
$table = new Table('`test`.`test`'); |
636
|
|
|
self::assertEquals('test.test', $table->getName()); |
637
|
|
|
self::assertEquals('`test`.`test`', $table->getQuotedName(new MySqlPlatform())); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* @group DBAL-204 |
642
|
|
|
*/ |
643
|
|
|
public function testFullQualifiedTableName() |
644
|
|
|
{ |
645
|
|
|
$table = new Table('`test`.`test`'); |
646
|
|
|
self::assertEquals('test.test', $table->getFullQualifiedName('test')); |
647
|
|
|
self::assertEquals('test.test', $table->getFullQualifiedName('other')); |
648
|
|
|
|
649
|
|
|
$table = new Table('test'); |
650
|
|
|
self::assertEquals('test.test', $table->getFullQualifiedName('test')); |
651
|
|
|
self::assertEquals('other.test', $table->getFullQualifiedName('other')); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* @group DBAL-224 |
656
|
|
|
*/ |
657
|
|
|
public function testDropIndex() |
658
|
|
|
{ |
659
|
|
|
$table = new Table('test'); |
660
|
|
|
$table->addColumn('id', 'integer'); |
661
|
|
|
$table->addIndex(['id'], 'idx'); |
662
|
|
|
|
663
|
|
|
self::assertTrue($table->hasIndex('idx')); |
664
|
|
|
|
665
|
|
|
$table->dropIndex('idx'); |
666
|
|
|
self::assertFalse($table->hasIndex('idx')); |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* @group DBAL-224 |
671
|
|
|
*/ |
672
|
|
|
public function testDropPrimaryKey() |
673
|
|
|
{ |
674
|
|
|
$table = new Table('test'); |
675
|
|
|
$table->addColumn('id', 'integer'); |
676
|
|
|
$table->setPrimaryKey(['id']); |
677
|
|
|
|
678
|
|
|
self::assertTrue($table->hasPrimaryKey()); |
679
|
|
|
|
680
|
|
|
$table->dropPrimaryKey(); |
681
|
|
|
self::assertFalse($table->hasPrimaryKey()); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* @group DBAL-234 |
686
|
|
|
*/ |
687
|
|
|
public function testRenameIndex() |
688
|
|
|
{ |
689
|
|
|
$table = new Table('test'); |
690
|
|
|
$table->addColumn('id', 'integer'); |
691
|
|
|
$table->addColumn('foo', 'integer'); |
692
|
|
|
$table->addColumn('bar', 'integer'); |
693
|
|
|
$table->addColumn('baz', 'integer'); |
694
|
|
|
$table->setPrimaryKey(['id'], 'pk'); |
695
|
|
|
$table->addIndex(['foo'], 'idx', ['flag']); |
696
|
|
|
$table->addUniqueIndex(['bar', 'baz'], 'uniq'); |
697
|
|
|
|
698
|
|
|
// Rename to custom name. |
699
|
|
|
self::assertSame($table, $table->renameIndex('pk', 'pk_new')); |
700
|
|
|
self::assertSame($table, $table->renameIndex('idx', 'idx_new')); |
701
|
|
|
self::assertSame($table, $table->renameIndex('uniq', 'uniq_new')); |
702
|
|
|
|
703
|
|
|
self::assertTrue($table->hasPrimaryKey()); |
704
|
|
|
self::assertTrue($table->hasIndex('pk_new')); |
705
|
|
|
self::assertTrue($table->hasIndex('idx_new')); |
706
|
|
|
self::assertTrue($table->hasIndex('uniq_new')); |
707
|
|
|
|
708
|
|
|
self::assertFalse($table->hasIndex('pk')); |
709
|
|
|
self::assertFalse($table->hasIndex('idx')); |
710
|
|
|
self::assertFalse($table->hasIndex('uniq')); |
711
|
|
|
|
712
|
|
|
self::assertEquals(new Index('pk_new', ['id'], true, true), $table->getPrimaryKey()); |
713
|
|
|
self::assertEquals(new Index('pk_new', ['id'], true, true), $table->getIndex('pk_new')); |
714
|
|
|
self::assertEquals( |
715
|
|
|
new Index('idx_new', ['foo'], false, false, ['flag']), |
716
|
|
|
$table->getIndex('idx_new') |
717
|
|
|
); |
718
|
|
|
self::assertEquals(new Index('uniq_new', ['bar', 'baz'], true), $table->getIndex('uniq_new')); |
719
|
|
|
|
720
|
|
|
// Rename to auto-generated name. |
721
|
|
|
self::assertSame($table, $table->renameIndex('pk_new', null)); |
722
|
|
|
self::assertSame($table, $table->renameIndex('idx_new', null)); |
723
|
|
|
self::assertSame($table, $table->renameIndex('uniq_new', null)); |
724
|
|
|
|
725
|
|
|
self::assertTrue($table->hasPrimaryKey()); |
726
|
|
|
self::assertTrue($table->hasIndex('primary')); |
727
|
|
|
self::assertTrue($table->hasIndex('IDX_D87F7E0C8C736521')); |
728
|
|
|
self::assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498')); |
729
|
|
|
|
730
|
|
|
self::assertFalse($table->hasIndex('pk_new')); |
731
|
|
|
self::assertFalse($table->hasIndex('idx_new')); |
732
|
|
|
self::assertFalse($table->hasIndex('uniq_new')); |
733
|
|
|
|
734
|
|
|
self::assertEquals(new Index('primary', ['id'], true, true), $table->getPrimaryKey()); |
735
|
|
|
self::assertEquals(new Index('primary', ['id'], true, true), $table->getIndex('primary')); |
736
|
|
|
self::assertEquals( |
737
|
|
|
new Index('IDX_D87F7E0C8C736521', ['foo'], false, false, ['flag']), |
738
|
|
|
$table->getIndex('IDX_D87F7E0C8C736521') |
739
|
|
|
); |
740
|
|
|
self::assertEquals( |
741
|
|
|
new Index('UNIQ_D87F7E0C76FF8CAA78240498', ['bar', 'baz'], true), |
742
|
|
|
$table->getIndex('UNIQ_D87F7E0C76FF8CAA78240498') |
743
|
|
|
); |
744
|
|
|
|
745
|
|
|
// Rename to same name (changed case). |
746
|
|
|
self::assertSame($table, $table->renameIndex('primary', 'PRIMARY')); |
747
|
|
|
self::assertSame($table, $table->renameIndex('IDX_D87F7E0C8C736521', 'idx_D87F7E0C8C736521')); |
748
|
|
|
self::assertSame($table, $table->renameIndex('UNIQ_D87F7E0C76FF8CAA78240498', 'uniq_D87F7E0C76FF8CAA78240498')); |
749
|
|
|
|
750
|
|
|
self::assertTrue($table->hasPrimaryKey()); |
751
|
|
|
self::assertTrue($table->hasIndex('primary')); |
752
|
|
|
self::assertTrue($table->hasIndex('IDX_D87F7E0C8C736521')); |
753
|
|
|
self::assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498')); |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
/** |
757
|
|
|
* @group DBAL-2508 |
758
|
|
|
*/ |
759
|
|
|
public function testKeepsIndexOptionsOnRenamingRegularIndex() |
760
|
|
|
{ |
761
|
|
|
$table = new Table('foo'); |
762
|
|
|
$table->addColumn('id', 'integer'); |
763
|
|
|
$table->addIndex(['id'], 'idx_bar', [], ['where' => '1 = 1']); |
764
|
|
|
|
765
|
|
|
$table->renameIndex('idx_bar', 'idx_baz'); |
766
|
|
|
|
767
|
|
|
self::assertSame(['where' => '1 = 1'], $table->getIndex('idx_baz')->getOptions()); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
/** |
771
|
|
|
* @group DBAL-2508 |
772
|
|
|
*/ |
773
|
|
|
public function testKeepsIndexOptionsOnRenamingUniqueIndex() |
774
|
|
|
{ |
775
|
|
|
$table = new Table('foo'); |
776
|
|
|
$table->addColumn('id', 'integer'); |
777
|
|
|
$table->addUniqueIndex(['id'], 'idx_bar', ['where' => '1 = 1']); |
778
|
|
|
|
779
|
|
|
$table->renameIndex('idx_bar', 'idx_baz'); |
780
|
|
|
|
781
|
|
|
self::assertSame(['where' => '1 = 1'], $table->getIndex('idx_baz')->getOptions()); |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
/** |
785
|
|
|
* @group DBAL-234 |
786
|
|
|
* @expectedException \Doctrine\DBAL\Schema\SchemaException |
787
|
|
|
*/ |
788
|
|
|
public function testThrowsExceptionOnRenamingNonExistingIndex() |
789
|
|
|
{ |
790
|
|
|
$table = new Table('test'); |
791
|
|
|
$table->addColumn('id', 'integer'); |
792
|
|
|
$table->addIndex(['id'], 'idx'); |
793
|
|
|
|
794
|
|
|
$table->renameIndex('foo', 'bar'); |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
/** |
798
|
|
|
* @group DBAL-234 |
799
|
|
|
* @expectedException \Doctrine\DBAL\Schema\SchemaException |
800
|
|
|
*/ |
801
|
|
|
public function testThrowsExceptionOnRenamingToAlreadyExistingIndex() |
802
|
|
|
{ |
803
|
|
|
$table = new Table('test'); |
804
|
|
|
$table->addColumn('id', 'integer'); |
805
|
|
|
$table->addColumn('foo', 'integer'); |
806
|
|
|
$table->addIndex(['id'], 'idx_id'); |
807
|
|
|
$table->addIndex(['foo'], 'idx_foo'); |
808
|
|
|
|
809
|
|
|
$table->renameIndex('idx_id', 'idx_foo'); |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* @dataProvider getNormalizesAssetNames |
814
|
|
|
* @group DBAL-831 |
815
|
|
|
*/ |
816
|
|
|
public function testNormalizesColumnNames($assetName) |
817
|
|
|
{ |
818
|
|
|
$table = new Table('test'); |
819
|
|
|
|
820
|
|
|
$table->addColumn($assetName, 'integer'); |
821
|
|
|
$table->addIndex([$assetName], $assetName); |
822
|
|
|
$table->addForeignKeyConstraint('test', [$assetName], [$assetName], [], $assetName); |
823
|
|
|
|
824
|
|
|
self::assertTrue($table->hasColumn($assetName)); |
825
|
|
|
self::assertTrue($table->hasColumn('foo')); |
826
|
|
|
self::assertInstanceOf(Column::class, $table->getColumn($assetName)); |
827
|
|
|
self::assertInstanceOf(Column::class, $table->getColumn('foo')); |
828
|
|
|
|
829
|
|
|
self::assertTrue($table->hasIndex($assetName)); |
830
|
|
|
self::assertTrue($table->hasIndex('foo')); |
831
|
|
|
self::assertInstanceOf(Index::class, $table->getIndex($assetName)); |
832
|
|
|
self::assertInstanceOf(Index::class, $table->getIndex('foo')); |
833
|
|
|
|
834
|
|
|
self::assertTrue($table->hasForeignKey($assetName)); |
835
|
|
|
self::assertTrue($table->hasForeignKey('foo')); |
836
|
|
|
self::assertInstanceOf(ForeignKeyConstraint::class, $table->getForeignKey($assetName)); |
837
|
|
|
self::assertInstanceOf(ForeignKeyConstraint::class, $table->getForeignKey('foo')); |
838
|
|
|
|
839
|
|
|
$table->renameIndex($assetName, $assetName); |
840
|
|
|
self::assertTrue($table->hasIndex($assetName)); |
841
|
|
|
self::assertTrue($table->hasIndex('foo')); |
842
|
|
|
|
843
|
|
|
$table->renameIndex($assetName, 'foo'); |
844
|
|
|
self::assertTrue($table->hasIndex($assetName)); |
845
|
|
|
self::assertTrue($table->hasIndex('foo')); |
846
|
|
|
|
847
|
|
|
$table->renameIndex('foo', $assetName); |
848
|
|
|
self::assertTrue($table->hasIndex($assetName)); |
849
|
|
|
self::assertTrue($table->hasIndex('foo')); |
850
|
|
|
|
851
|
|
|
$table->renameIndex($assetName, 'bar'); |
852
|
|
|
self::assertFalse($table->hasIndex($assetName)); |
853
|
|
|
self::assertFalse($table->hasIndex('foo')); |
854
|
|
|
self::assertTrue($table->hasIndex('bar')); |
855
|
|
|
|
856
|
|
|
$table->renameIndex('bar', $assetName); |
857
|
|
|
|
858
|
|
|
$table->dropColumn($assetName); |
859
|
|
|
$table->dropIndex($assetName); |
860
|
|
|
$table->removeForeignKey($assetName); |
861
|
|
|
|
862
|
|
|
self::assertFalse($table->hasColumn($assetName)); |
863
|
|
|
self::assertFalse($table->hasColumn('foo')); |
864
|
|
|
self::assertFalse($table->hasIndex($assetName)); |
865
|
|
|
self::assertFalse($table->hasIndex('foo')); |
866
|
|
|
self::assertFalse($table->hasForeignKey($assetName)); |
867
|
|
|
self::assertFalse($table->hasForeignKey('foo')); |
868
|
|
|
} |
869
|
|
|
|
870
|
|
|
public function getNormalizesAssetNames() |
871
|
|
|
{ |
872
|
|
|
return [ |
873
|
|
|
['foo'], |
874
|
|
|
['FOO'], |
875
|
|
|
['`foo`'], |
876
|
|
|
['`FOO`'], |
877
|
|
|
['"foo"'], |
878
|
|
|
['"FOO"'], |
879
|
|
|
['"foo"'], |
880
|
|
|
['"FOO"'], |
881
|
|
|
]; |
882
|
|
|
} |
883
|
|
|
} |
884
|
|
|
|