Skip to content

Commit

Permalink
Move schema part to the index
Browse files Browse the repository at this point in the history
In SQLite, creating an index in another schema is done by prepending the
index name with the schema, while the table name must be unprefixed.
  • Loading branch information
greg0ire committed Nov 21, 2023
1 parent 0ac3c27 commit 41e155c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
<referencedMethod name="Doctrine\DBAL\Platforms\SQLServerPlatform::getListTableForeignKeysSQL"/>
<referencedMethod name="Doctrine\DBAL\Platforms\SQLServerPlatform::getListTableIndexesSQL"/>
<referencedMethod name="Doctrine\DBAL\Platforms\SQLServerPlatform::getListTableMetadataSQL"/>
<referencedMethod name="Doctrine\DBAL\Platforms\SqlitePlatform::disableSchemaEmulation"/>
<referencedMethod name="Doctrine\DBAL\Platforms\SqlitePlatform::getListTableColumnsSQL"/>
<referencedMethod name="Doctrine\DBAL\Platforms\SqlitePlatform::getListTableForeignKeysSQL"/>
<referencedMethod name="Doctrine\DBAL\Platforms\SqlitePlatform::getListTableIndexesSQL"/>
Expand Down
5 changes: 5 additions & 0 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,11 @@ public function getCreateIndexSQL(Index $index, $table)
$name = $index->getQuotedName($this);
$columns = $index->getColumns();

if ($this instanceof SqlitePlatform && strpos($table, '.') !== false) {
[$schema, $table] = explode('.', $table);
$name = $schema . '.' . $name;
}

if (count($columns) === 0) {
throw new InvalidArgumentException(sprintf(
'Incomplete or invalid index definition %s on table %s',
Expand Down
31 changes: 31 additions & 0 deletions tests/Functional/Platform/OtherSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Doctrine\DBAL\Tests\Functional\Platform;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;

class OtherSchemaTest extends FunctionalTestCase
{
public function testATableCanBeCreatedInAnotherSchema(): void
{
$databasePlatform = $this->connection->getDatabasePlatform();
if (! ($databasePlatform instanceof SqlitePlatform)) {
self::markTestSkipped('This test requires SQLite');
}

$this->connection->executeStatement("ATTACH DATABASE '/tmp/test_other_schema.sqlite' AS other");
$databasePlatform->disableSchemaEmulation();

$table = new Table('other.test_other_schema');
$table->addColumn('id', Types::INTEGER);
$table->addIndex(['id']);

$this->dropAndCreateTable($table);
$this->connection->insert('other.test_other_schema', ['id' => 1]);

self::assertEquals(1, $this->connection->fetchOne('SELECT COUNT(*) FROM other.test_other_schema'));
}
}
20 changes: 19 additions & 1 deletion tests/Platforms/SqlitePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
{
public function createPlatform(): AbstractPlatform
{
return new SqlitePlatform();
$platform = new SqlitePlatform();
$platform->disableSchemaEmulation();

return $platform;
}

public function getGenerateTableSql(): string
Expand Down Expand Up @@ -768,4 +771,19 @@ public function testGetCreateTableSQLWithColumnCollation(): void
$this->platform->getCreateTableSQL($table),
);
}

public function testCreateTableInOtherSchema(): void
{
$table = new Table('other_schema.foo');
$table->addColumn('id', Types::INTEGER);
$table->addIndex(['id']);

self::assertSame(
[
'CREATE TABLE other_schema.foo (id INTEGER NOT NULL)',
'CREATE INDEX other_schema.IDX_D9A8AD0DBF396750 ON foo (id)',
],
$this->platform->getCreateTableSQL($table),
);
}
}

0 comments on commit 41e155c

Please sign in to comment.