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

DBAL2807Test   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 21
dl 0
loc 40
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A testAlterPrimaryKeyToAutoIncrementColumn() 0 31 2
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($table->getName());
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($table->getName());
43
44
        $this->assertTrue($validationTable->hasColumn('new_id'));
45
        $this->assertTrue($validationTable->getColumn('new_id')->getAutoincrement());
46
        $this->assertTrue($validationTable->hasPrimaryKey());
47
        $this->assertSame(['new_id'], $validationTable->getPrimaryKeyColumns());
48
    }
49
}
50