Completed
Pull Request — master (#3311)
by Arne
62:09
created

testAlterPrimaryKeyToAutoIncrementColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 31
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Ticket;
4
5
use Doctrine\DBAL\Schema\Comparator;
6
use Doctrine\Tests\DbalFunctionalTestCase;
7
8
final class DBAL2807Test extends DbalFunctionalTestCase
9
{
10
    /**
11
     * Ensures that the primary key is created within the same "alter table" statement that an auto-increment column
12
     * is added to the table as part of the new primary key.
13
     *
14
     * Before the fix for this problem this resulted in a database error: (at least on mysql)
15
     * SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
16
     */
17
    public function testAlterPrimaryKeyToAutoIncrementColumn()
18
    {
19
        $schemaManager = $this->connection->getSchemaManager();
20
        $schema = $schemaManager->createSchema();
21
22
        $table = $schema->createTable('dbal2807');
23
        $table->addColumn('initial_id', 'integer');
24
        $table->setPrimaryKey(['initial_id']);
25
26
        $schemaManager->dropAndCreateTable($table);
27
28
        $newSchema = clone $schema;
29
        $newTable = $newSchema->getTable('dbal2807');
30
        $newTable->addColumn('new_id', 'integer', ['autoincrement' => true]);
31
        $newTable->dropPrimaryKey();
32
        $newTable->setPrimaryKey(['new_id']);
33
34
        $diff = (new Comparator())->compare($schema, $newSchema);
35
36
        foreach ($diff->toSql($this->connection->getDatabasePlatform()) as $sql)
37
        {
38
            $this->connection->exec($sql);
39
        }
40
41
        $validationSchema = $schemaManager->createSchema();
42
        $validationTable = $validationSchema->getTable('dbal2807');
43
44
        $this->assertTrue($validationTable->hasPrimaryKey());
45
        $this->assertTrue($validationTable->hasColumn('new_id'));
46
        $this->assertSame(['new_id'], $validationTable->getPrimaryKeyColumns());
47
        $this->assertTrue($validationTable->getColumn('new_id')->getAutoincrement());
48
    }
49
}
50