Completed
Pull Request — master (#3311)
by Arne
60:40
created

DBAL2807Test::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
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
     * {@inheritDoc}
12
     */
13
    public function setUp()
14
    {
15
        parent::setUp();
16
17
        if (!in_array($this->getPlatform()->getName(), ['mysql']))
18
        {
19
            $this->markTestSkipped('Restricted to MySQL.');
20
21
            return;
22
        }
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 column and it must be defined as a key
31
     */
32
    public function testAlterPrimaryKeyToAutoIncrementColumn()
33
    {
34
        $schemaManager = $this->connection->getSchemaManager();
35
        $schema = $schemaManager->createSchema();
36
37
        $table = $schema->createTable('dbal2807');
38
        $table->addColumn('initial_id', 'integer');
39
        $table->setPrimaryKey(['initial_id']);
40
41
        $schemaManager->dropAndCreateTable($table);
42
43
        $newSchema = clone $schema;
44
        $newTable = $newSchema->getTable($table->getName());
45
        $newTable->addColumn('new_id', 'integer', ['autoincrement' => true]);
46
        $newTable->dropPrimaryKey();
47
        $newTable->setPrimaryKey(['new_id']);
48
49
        $diff = (new Comparator())->compare($schema, $newSchema);
50
51
        foreach ($diff->toSql($this->getPlatform()) as $sql)
52
        {
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