Skip to content

Commit

Permalink
Merge pull request #6226 from greg0ire/index-other-schema-sqlite
Browse files Browse the repository at this point in the history
Move schema part to the index
  • Loading branch information
greg0ire authored Nov 24, 2023
2 parents dc42058 + bc7afc1 commit ef0576b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
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
45 changes: 45 additions & 0 deletions src/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\DBAL\Types;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\Deprecations\Deprecation;
use InvalidArgumentException;

use function array_combine;
use function array_keys;
Expand All @@ -26,12 +27,14 @@
use function array_unique;
use function array_values;
use function count;
use function explode;
use function implode;
use function is_numeric;
use function sprintf;
use function sqrt;
use function str_replace;
use function strlen;
use function strpos;
use function strtolower;
use function trim;

Expand Down Expand Up @@ -918,6 +921,48 @@ public function getCreateTablesSQL(array $tables): array
return $sql;
}

/**
* {@inheritDoc}
*/
public function getCreateIndexSQL(Index $index, $table)
{
if ($table instanceof Table) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4798',
'Passing $table as a Table object to %s is deprecated. Pass it as a quoted name instead.',
__METHOD__,
);

$table = $table->getQuotedName($this);
}

$name = $index->getQuotedName($this);
$columns = $index->getColumns();

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

if (count($columns) === 0) {
throw new InvalidArgumentException(sprintf(

Check warning on line 949 in src/Platforms/SqlitePlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/SqlitePlatform.php#L949

Added line #L949 was not covered by tests
'Incomplete or invalid index definition %s on table %s',
$name,
$table,

Check warning on line 952 in src/Platforms/SqlitePlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/SqlitePlatform.php#L952

Added line #L952 was not covered by tests
));
}

if ($index->isPrimary()) {
return $this->getCreatePrimaryKeySQL($index, $table);

Check warning on line 957 in src/Platforms/SqlitePlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/SqlitePlatform.php#L957

Added line #L957 was not covered by tests
}

Check warning on line 959 in src/Platforms/SqlitePlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/SqlitePlatform.php#L959

Added line #L959 was not covered by tests
$query = 'CREATE ' . $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')' . $this->getPartialIndexSQL($index);

Check warning on line 962 in src/Platforms/SqlitePlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/SqlitePlatform.php#L962

Added line #L962 was not covered by tests
return $query;
}

/**
* {@inheritDoc}
*/
Expand Down
37 changes: 37 additions & 0 deletions tests/Functional/Platform/OtherSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Doctrine\DBAL\Tests\Functional\Platform;

use Doctrine\DBAL\DriverManager;
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'));
$connection = DriverManager::getConnection(
['url' => 'sqlite:////tmp/test_other_schema.sqlite'],
);
$onlineTable = $connection->createSchemaManager()->introspectTable('test_other_schema');
self::assertCount(1, $onlineTable->getIndexes());
}
}

0 comments on commit ef0576b

Please sign in to comment.