Passed
Pull Request — master (#3311)
by Arne
06:17
created

setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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()
15
    {
16
        parent::setUp();
17
18
        if (! in_array($this->getPlatform()->getName(), ['mysql'])) {
19
            $this->markTestSkipped('Restricted to MySQL.');
20
        }
21
    }
22
23
    /**
24
     * Ensures that the primary key is created within the same "alter table" statement that an auto-increment column
25
     * is added to the table as part of the new primary key.
26
     *
27
     * Before the fix for this problem this resulted in a database error: (at least on mysql)
28
     * 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
29
     */
30
    public function testAlterPrimaryKeyToAutoIncrementColumn()
31
    {
32
        $schemaManager = $this->connection->getSchemaManager();
33
        $schema        = $schemaManager->createSchema();
34
35
        $table = $schema->createTable('dbal2807');
36
        $table->addColumn('initial_id', 'integer');
37
        $table->setPrimaryKey(['initial_id']);
38
39
        $schemaManager->dropAndCreateTable($table);
40
41
        $newSchema = clone $schema;
42
        $newTable  = $newSchema->getTable($table->getName());
43
        $newTable->addColumn('new_id', 'integer', ['autoincrement' => true]);
44
        $newTable->dropPrimaryKey();
45
        $newTable->setPrimaryKey(['new_id']);
46
47
        $diff = (new Comparator())->compare($schema, $newSchema);
48
49
        foreach ($diff->toSql($this->getPlatform()) as $sql) {
50
            $this->connection->exec($sql);
51
        }
52
53
        $validationSchema = $schemaManager->createSchema();
54
        $validationTable  = $validationSchema->getTable($table->getName());
55
56
        $this->assertTrue($validationTable->hasColumn('new_id'));
57
        $this->assertTrue($validationTable->getColumn('new_id')->getAutoincrement());
58
        $this->assertTrue($validationTable->hasPrimaryKey());
59
        $this->assertSame(['new_id'], $validationTable->getPrimaryKeyColumns());
60
    }
61
62
    private function getPlatform()
63
    {
64
        return $this->connection->getDatabasePlatform();
65
    }
66
}
67