|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional\Platform; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Comparator; |
|
6
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
|
7
|
|
|
use function in_array; |
|
8
|
|
|
|
|
9
|
|
|
final class NewPrimaryKeyWithNewAutoIncrementColumnTest extends DbalFunctionalTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritDoc} |
|
13
|
|
|
*/ |
|
14
|
|
|
protected function setUp() : void |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
|
|
18
|
|
|
if (in_array($this->getPlatform()->getName(), ['mysql'])) { |
|
19
|
|
|
return; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$this->markTestSkipped('Restricted to MySQL.'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Ensures that the primary key is created within the same "alter table" statement that an auto-increment column |
|
27
|
|
|
* is added to the table as part of the new primary key. |
|
28
|
|
|
* |
|
29
|
|
|
* Before the fix for this problem this resulted in a database error: (at least on mysql) |
|
30
|
|
|
* SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto |
|
31
|
|
|
* column and it must be defined as a key |
|
32
|
|
|
*/ |
|
33
|
|
|
public function testAlterPrimaryKeyToAutoIncrementColumn() |
|
34
|
|
|
{ |
|
35
|
|
|
$schemaManager = $this->connection->getSchemaManager(); |
|
36
|
|
|
$schema = $schemaManager->createSchema(); |
|
37
|
|
|
|
|
38
|
|
|
$table = $schema->createTable('dbal2807'); |
|
39
|
|
|
$table->addColumn('initial_id', 'integer'); |
|
40
|
|
|
$table->setPrimaryKey(['initial_id']); |
|
41
|
|
|
|
|
42
|
|
|
$schemaManager->dropAndCreateTable($table); |
|
43
|
|
|
|
|
44
|
|
|
$newSchema = clone $schema; |
|
45
|
|
|
$newTable = $newSchema->getTable($table->getName()); |
|
46
|
|
|
$newTable->addColumn('new_id', 'integer', ['autoincrement' => true]); |
|
47
|
|
|
$newTable->dropPrimaryKey(); |
|
48
|
|
|
$newTable->setPrimaryKey(['new_id']); |
|
49
|
|
|
|
|
50
|
|
|
$diff = (new Comparator())->compare($schema, $newSchema); |
|
51
|
|
|
|
|
52
|
|
|
foreach ($diff->toSql($this->getPlatform()) as $sql) { |
|
53
|
|
|
$this->connection->exec($sql); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$validationSchema = $schemaManager->createSchema(); |
|
57
|
|
|
$validationTable = $validationSchema->getTable($table->getName()); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertTrue($validationTable->hasColumn('new_id')); |
|
60
|
|
|
$this->assertTrue($validationTable->getColumn('new_id')->getAutoincrement()); |
|
61
|
|
|
$this->assertTrue($validationTable->hasPrimaryKey()); |
|
62
|
|
|
$this->assertSame(['new_id'], $validationTable->getPrimaryKeyColumns()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function getPlatform() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->connection->getDatabasePlatform(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|