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

NewPrimaryKeyWithNewAutoIncrementColumn   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 25
dl 0
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlatform() 0 3 1
A testAlterPrimaryKeyToAutoIncrementColumn() 0 30 2
A setUp() 0 8 2
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 NewPrimaryKeyWithNewAutoIncrementColumn extends DbalFunctionalTestCase
10
{
11
    /**
12
     * {@inheritDoc}
13
     */
14
    public function setUp()
15
    {
16
        parent::setUp();
17
18
        if (! in_array($this->getPlatform()->getName(), ['mysql'])) {
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
            $this->connection->exec($sql);
53
        }
54
55
        $validationSchema = $schemaManager->createSchema();
56
        $validationTable  = $validationSchema->getTable($table->getName());
57
58
        $this->assertTrue($validationTable->hasColumn('new_id'));
59
        $this->assertTrue($validationTable->getColumn('new_id')->getAutoincrement());
60
        $this->assertTrue($validationTable->hasPrimaryKey());
61
        $this->assertSame(['new_id'], $validationTable->getPrimaryKeyColumns());
62
    }
63
64
    private function getPlatform()
65
    {
66
        return $this->connection->getDatabasePlatform();
67
    }
68
}
69