Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "bigint" type introspection for PK column with AI for SQLite #6411

Open
wants to merge 1 commit into
base: 3.9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Schema/SqliteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ protected function _getPortableTableColumnDefinition($tableColumn)
$default = null;
}

// https://github.com/doctrine/dbal/blob/3.8.4/src/Platforms/SqlitePlatform.php#L271-L274
// https://dbfiddle.uk/yyXvHV-6
if (strtolower($type) === 'integer' && ($tableColumn['pk'] !== 0 && $tableColumn['pk'] !== '0')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why $tableColumn['pk'] !== 0 && $tableColumn['pk'] !== '0' should mean this is an auto increment column. Couldn't it be an integer column that is part of the primary key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I coded the "auto increment" detection based on existing https://github.com/doctrine/dbal/blob/b6a65d9ef8/src/Schema/SqliteSchemaManager.php#L295-L319. Is that right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also noticed that code block, but I don't know why it is right (if it is).

$type = 'bigint';
}

if ($default !== null) {
// SQLite returns the default value as a literal expression, so we need to parse it
if (preg_match('/^\'(.*)\'$/s', $default, $matches) === 1) {
Expand Down
40 changes: 39 additions & 1 deletion tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Doctrine\DBAL\Schema\View;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\ArrayType;
use Doctrine\DBAL\Types\BigIntType;
use Doctrine\DBAL\Types\BinaryType;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\DateIntervalType;
Expand Down Expand Up @@ -298,7 +299,12 @@ public function testListTableColumns(): void
self::assertArrayHasKey('id', $columns);
self::assertEquals(0, array_search('id', $columnsKeys, true));
self::assertEquals('id', strtolower($columns['id']->getName()));
self::assertInstanceOf(IntegerType::class, $columns['id']->getType());
self::assertInstanceOf(
$this->connection->getDatabasePlatform() instanceof SqlitePlatform
? BigIntType::class
: IntegerType::class,
$columns['id']->getType(),
);
self::assertEquals(false, $columns['id']->getUnsigned());
self::assertEquals(true, $columns['id']->getNotnull());
self::assertEquals(null, $columns['id']->getDefault());
Expand Down Expand Up @@ -363,6 +369,33 @@ public function testListTableColumns(): void
self::assertIsArray($columns['baz3']->getPlatformOptions());
}

public function testListTableColumnsWithBigintColumnsAndAutoincrement(): void
{
if (! $this->connection->getDatabasePlatform()->supportsIdentityColumns()) {
self::markTestSkipped('This test is only supported on platforms that have autoincrement');
}

$tableName = 'test_list_table_bigint';

$table = new Table($tableName);
$table->addColumn('id_with_ai', Types::BIGINT, ['autoincrement' => true]);
$table->setPrimaryKey(['id_with_ai']);
$table->addColumn('foo', Types::BIGINT);
$table->addColumn('bar', Types::INTEGER);

$this->schemaManager->createTable($table);

$columns = $this->schemaManager->listTableColumns($tableName);

self::assertCount(3, $columns);
self::assertArrayHasKey('id_with_ai', $columns);
self::assertInstanceOf(BigIntType::class, $columns['id_with_ai']->getType());
self::assertArrayHasKey('foo', $columns);
self::assertInstanceOf(BigIntType::class, $columns['foo']->getType());
self::assertArrayHasKey('bar', $columns);
self::assertInstanceOf(IntegerType::class, $columns['bar']->getType());
}

public function testListTableColumnsWithFixedStringColumn(): void
{
$tableName = 'test_list_table_fixed_string';
Expand Down Expand Up @@ -457,6 +490,11 @@ public function testDiffListTableColumns(callable $comparatorFactory): void
}

$offlineTable = $this->createListTableColumns();

if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
$offlineTable->getColumn('id')->setType(Type::getType(Types::BIGINT));
}

$this->dropAndCreateTable($offlineTable);
$onlineTable = $this->schemaManager->introspectTable('list_table_columns');

Expand Down