Skip to content

Commit 9352375

Browse files
committed
Fix "bigint" type for PK with AI introspection for SQLite
1 parent 9cf787c commit 9352375

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Schema/SqliteSchemaManager.php

+6
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,12 @@ protected function _getPortableTableColumnDefinition($tableColumn)
379379
$default = null;
380380
}
381381

382+
// https://github.com/doctrine/dbal/blob/3.8.4/src/Platforms/SqlitePlatform.php#L271-L274
383+
// https://dbfiddle.uk/yyXvHV-6
384+
if (strtolower($type) === 'integer' && ($tableColumn['pk'] !== 0 && $tableColumn['pk'] !== '0')) {
385+
$type = 'bigint';
386+
}
387+
382388
if ($default !== null) {
383389
// SQLite returns the default value as a literal expression, so we need to parse it
384390
if (preg_match('/^\'(.*)\'$/s', $default, $matches) === 1) {

tests/Functional/Schema/SchemaManagerFunctionalTestCase.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Doctrine\DBAL\Schema\View;
2626
use Doctrine\DBAL\Tests\FunctionalTestCase;
2727
use Doctrine\DBAL\Types\ArrayType;
28+
use Doctrine\DBAL\Types\BigIntType;
2829
use Doctrine\DBAL\Types\BinaryType;
2930
use Doctrine\DBAL\Types\BlobType;
3031
use Doctrine\DBAL\Types\DateIntervalType;
@@ -298,7 +299,12 @@ public function testListTableColumns(): void
298299
self::assertArrayHasKey('id', $columns);
299300
self::assertEquals(0, array_search('id', $columnsKeys, true));
300301
self::assertEquals('id', strtolower($columns['id']->getName()));
301-
self::assertInstanceOf(IntegerType::class, $columns['id']->getType());
302+
self::assertInstanceOf(
303+
$this->connection->getDatabasePlatform() instanceof SqlitePlatform
304+
? BigIntType::class
305+
: IntegerType::class,
306+
$columns['id']->getType(),
307+
);
302308
self::assertEquals(false, $columns['id']->getUnsigned());
303309
self::assertEquals(true, $columns['id']->getNotnull());
304310
self::assertEquals(null, $columns['id']->getDefault());
@@ -363,6 +369,29 @@ public function testListTableColumns(): void
363369
self::assertIsArray($columns['baz3']->getPlatformOptions());
364370
}
365371

372+
public function testListTableColumnsWithBigintColumns(): void
373+
{
374+
$tableName = 'test_list_table_bigint';
375+
376+
$table = new Table($tableName);
377+
$table->addColumn('id_with_ai', Types::BIGINT);
378+
$table->setPrimaryKey(['id_with_ai']);
379+
$table->addColumn('foo', Types::BIGINT);
380+
$table->addColumn('bar', Types::INTEGER);
381+
382+
$this->schemaManager->createTable($table);
383+
384+
$columns = $this->schemaManager->listTableColumns($tableName);
385+
386+
self::assertCount(3, $columns);
387+
self::assertArrayHasKey('id_with_ai', $columns);
388+
self::assertInstanceOf(BigIntType::class, $columns['id_with_ai']->getType());
389+
self::assertArrayHasKey('foo', $columns);
390+
self::assertInstanceOf(BigIntType::class, $columns['foo']->getType());
391+
self::assertArrayHasKey('bar', $columns);
392+
self::assertInstanceOf(IntegerType::class, $columns['bar']->getType());
393+
}
394+
366395
public function testListTableColumnsWithFixedStringColumn(): void
367396
{
368397
$tableName = 'test_list_table_fixed_string';
@@ -457,6 +486,11 @@ public function testDiffListTableColumns(callable $comparatorFactory): void
457486
}
458487

459488
$offlineTable = $this->createListTableColumns();
489+
490+
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
491+
$offlineTable->getColumn('id')->setType(Type::getType(Types::BIGINT));
492+
}
493+
460494
$this->dropAndCreateTable($offlineTable);
461495
$onlineTable = $this->schemaManager->introspectTable('list_table_columns');
462496

0 commit comments

Comments
 (0)