Completed
Pull Request — master (#3311)
by Arne
64:06
created

NewPrimaryKeyWithNewAutoIncrementColumnTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 24
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 2
A testAlterPrimaryKeyToAutoIncrementColumn() 0 30 2
A getPlatform() 0 3 1
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
29
     * column and it must be defined as a key
30
     */
31
    public function testAlterPrimaryKeyToAutoIncrementColumn()
32
    {
33
        $schemaManager = $this->connection->getSchemaManager();
34
        $schema        = $schemaManager->createSchema();
35
36
        $table = $schema->createTable('dbal2807');
37
        $table->addColumn('initial_id', 'integer');
38
        $table->setPrimaryKey(['initial_id']);
39
40
        $schemaManager->dropAndCreateTable($table);
41
42
        $newSchema = clone $schema;
43
        $newTable  = $newSchema->getTable($table->getName());
44
        $newTable->addColumn('new_id', 'integer', ['autoincrement' => true]);
45
        $newTable->dropPrimaryKey();
46
        $newTable->setPrimaryKey(['new_id']);
47
48
        $diff = (new Comparator())->compare($schema, $newSchema);
49
50
        foreach ($diff->toSql($this->getPlatform()) as $sql) {
51
            $this->connection->exec($sql);
52
        }
53
54
        $validationSchema = $schemaManager->createSchema();
55
        $validationTable  = $validationSchema->getTable($table->getName());
56
57
        $this->assertTrue($validationTable->hasColumn('new_id'));
58
        $this->assertTrue($validationTable->getColumn('new_id')->getAutoincrement());
59
        $this->assertTrue($validationTable->hasPrimaryKey());
60
        $this->assertSame(['new_id'], $validationTable->getPrimaryKeyColumns());
61
    }
62
63
    private function getPlatform()
64
    {
65
        return $this->connection->getDatabasePlatform();
66
    }
67
}
68