Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/Platforms/SQLitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,9 @@ private function getIndexesInAlteredTable(TableDiff $diff): array
$changed = false;
$indexColumns = [];
foreach ($index->getColumns() as $columnName) {
// the column names of an introspected index are quoted while the name map is keyed
// by the unquoted column names, so unquote before the lookup
$columnName = (new Identifier($columnName))->getName();
Comment thread
fballiano marked this conversation as resolved.
Outdated
$normalizedColumnName = strtolower($columnName);
if (! isset($nameMap[$normalizedColumnName])) {
unset($indexes[$key]);
Expand Down Expand Up @@ -904,6 +907,9 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
$changed = false;
$localColumns = [];
foreach ($constraint->getLocalColumns() as $columnName) {
// the referencing column names of an introspected foreign key constraint are quoted
// while the name map is keyed by the unquoted column names, so unquote before the lookup
$columnName = (new Identifier($columnName))->getName();
$normalizedColumnName = strtolower($columnName);
if (! isset($nameMap[$normalizedColumnName])) {
unset($foreignKeys[$key]);
Expand Down
63 changes: 63 additions & 0 deletions tests/Functional/Schema/SQLiteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\ColumnEditor;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Index\IndexedColumn;
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\SQLiteSchemaManager;
Expand All @@ -26,6 +28,7 @@
use function array_shift;
use function array_values;
use function assert;
use function ksort;

class SQLiteSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
Expand Down Expand Up @@ -316,6 +319,66 @@ public function testNonSimpleAlterTableCreatedFromDDL(): void
self::assertSame(['name'], $index->getColumns());
}

public function testAlterIntrospectedTablePreservesIndexesAndForeignKeys(): void
Comment thread
fballiano marked this conversation as resolved.
Outdated
{
$this->dropTableIfExists('tree_nodes');

$ddl = <<<'DDL'
CREATE TABLE tree_nodes (
id INTEGER NOT NULL,
parent_id INTEGER,
name TEXT,
weight INTEGER,
PRIMARY KEY (id),
FOREIGN KEY (parent_id) REFERENCES tree_nodes (id)
)
DDL;

$this->connection->executeStatement($ddl);
$this->connection->executeStatement('CREATE UNIQUE INDEX idx_tree_name ON tree_nodes (name)');
$this->connection->executeStatement('CREATE INDEX idx_tree_parent ON tree_nodes (parent_id)');

$schemaManager = $this->connection->createSchemaManager();

$oldTable = $schemaManager->introspectTableByUnquotedName('tree_nodes');
$newTable = $oldTable->edit()
->modifyColumnByUnquotedName(
'weight',
static function (ColumnEditor $editor): void {
$editor->setTypeName(Types::STRING)
->setLength(32);
},
)
->create();

$diff = $schemaManager->createComparator()->compareTables($oldTable, $newTable);
$schemaManager->alterTable($diff);

$table = $schemaManager->introspectTableByUnquotedName('tree_nodes');

self::assertCount(1, $table->getForeignKeys());

$indexes = [];
foreach ($table->getIndexes() as $index) {
$indexName = $index->getObjectName()->getIdentifier()->getValue();
if ($indexName === 'primary') {
continue;
}

$indexes[$indexName] = array_map(
static fn (IndexedColumn $indexedColumn): string => $indexedColumn
->getColumnName()
->getIdentifier()
->getValue(),
$index->getIndexedColumns(),
);
}

ksort($indexes);

self::assertSame(['idx_tree_name' => ['name'], 'idx_tree_parent' => ['parent_id']], $indexes);
}

public function testAlterTableWithSchema(): void
{
$this->dropTableIfExists('t');
Expand Down