Passed
Push — phpstan-tests ( 6a2b78...974220 )
by Michael
61:35 queued 23s
created

testAlterPrimaryKeyToAutoIncrementColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 30
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional\Platform;
6
7
use Doctrine\DBAL\Schema\Comparator;
8
use Doctrine\Tests\DbalFunctionalTestCase;
9
use function in_array;
10
11
final class NewPrimaryKeyWithNewAutoIncrementColumnTest extends DbalFunctionalTestCase
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16
    protected function setUp() : void
17
    {
18
        parent::setUp();
19
20
        if (in_array($this->getPlatform()->getName(), ['mysql'])) {
21
            return;
22
        }
23
24
        $this->markTestSkipped('Restricted to MySQL.');
25
    }
26
27
    /**
28
     * Ensures that the primary key is created within the same "alter table" statement that an auto-increment column
29
     * is added to the table as part of the new primary key.
30
     *
31
     * Before the fix for this problem this resulted in a database error: (at least on mysql)
32
     * SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto
33
     * column and it must be defined as a key
34
     */
35
    public function testAlterPrimaryKeyToAutoIncrementColumn()
36
    {
37
        $schemaManager = $this->connection->getSchemaManager();
38
        $schema        = $schemaManager->createSchema();
39
40
        $table = $schema->createTable('dbal2807');
41
        $table->addColumn('initial_id', 'integer');
42
        $table->setPrimaryKey(['initial_id']);
43
44
        $schemaManager->dropAndCreateTable($table);
45
46
        $newSchema = clone $schema;
47
        $newTable  = $newSchema->getTable($table->getName());
48
        $newTable->addColumn('new_id', 'integer', ['autoincrement' => true]);
49
        $newTable->dropPrimaryKey();
50
        $newTable->setPrimaryKey(['new_id']);
51
52
        $diff = (new Comparator())->compare($schema, $newSchema);
53
54
        foreach ($diff->toSql($this->getPlatform()) as $sql) {
55
            $this->connection->exec($sql);
56
        }
57
58
        $validationSchema = $schemaManager->createSchema();
59
        $validationTable  = $validationSchema->getTable($table->getName());
60
61
        $this->assertTrue($validationTable->hasColumn('new_id'));
62
        $this->assertTrue($validationTable->getColumn('new_id')->getAutoincrement());
63
        $this->assertTrue($validationTable->hasPrimaryKey());
64
        $this->assertSame(['new_id'], $validationTable->getPrimaryKeyColumns());
65
    }
66
67
    private function getPlatform()
68
    {
69
        return $this->connection->getDatabasePlatform();
70
    }
71
}
72