Skip to content

Commit

Permalink
Merge pull request #7 from mvorisek/merge_4
Browse files Browse the repository at this point in the history
Fix 3.8 -> 4.0 merge
  • Loading branch information
greg0ire committed May 4, 2024
2 parents 8c57598 + 5351005 commit e13e34c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Schema/SQLiteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function listTableForeignKeys(string $table): array
{
$table = $this->normalizeName($table);

$columns = $this->selectForeignKeyColumns($database ?? 'main', $table)
$columns = $this->selectForeignKeyColumns('main', $table)
->fetchAllAssociative();

if (count($columns) > 0) {
Expand Down
27 changes: 14 additions & 13 deletions tests/Functional/Schema/SqliteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;

use function array_keys;
use function array_shift;

class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
Expand Down Expand Up @@ -207,24 +210,22 @@ public function testNonSimpleAlterTableCreatedFromDDL(): void
self::assertSame(['name'], $index->getColumns());
}

public function testGeneratesAlterTableRenameColumnSQLWithSchema(): void
public function testAlterTableWithSchema(): void
{
$this->platform->disableSchemaEmulation();
$this->dropTableIfExists('t');

$table = new Table('main.t');
$table->addColumn('a', Types::INTEGER);
$this->schemaManager->createTable($table);

$tableDiff = new TableDiff('t');
$tableDiff->fromTable = $table;
$tableDiff->renamedColumns['a'] = new Column('b', Type::getType(Types::INTEGER));

self::assertSame([
'CREATE TEMPORARY TABLE __temp__t AS SELECT a FROM main.t',
'DROP TABLE main.t',
'CREATE TABLE main.t (b INTEGER NOT NULL)',
'INSERT INTO main.t (b) SELECT a FROM __temp__t',
'DROP TABLE __temp__t',
], $this->platform->getAlterTableSQL($tableDiff));
self::assertSame(['a'], array_keys($this->schemaManager->listTableColumns('t')));

$tableDiff = new TableDiff($table, [], [], [], [
'a' => new Column('b', Type::getType(Types::INTEGER)),
], [], [], [], [], [], [], []);
$this->schemaManager->alterTable($tableDiff);

self::assertSame(['b'], array_keys($this->schemaManager->listTableColumns('t')));
}

public function testIntrospectMultipleAnonymousForeignKeyConstraints(): void
Expand Down
6 changes: 3 additions & 3 deletions tests/Platforms/SQLitePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,9 @@ public function testGeneratesAlterTableRenameColumnSQLWithSchema(): void
$table = new Table('main.t');
$table->addColumn('a', Types::INTEGER);

$tableDiff = new TableDiff('t');
$tableDiff->fromTable = $table;
$tableDiff->renamedColumns['a'] = new Column('b', Type::getType(Types::INTEGER));
$tableDiff = new TableDiff($table, [], [], [], [
'a' => new Column('b', Type::getType(Types::INTEGER)),
], [], [], [], [], [], [], []);

self::assertSame([
'CREATE TEMPORARY TABLE __temp__t AS SELECT a FROM main.t',
Expand Down
24 changes: 24 additions & 0 deletions tests/Schema/SQLiteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\SQLiteSchemaManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -202,4 +203,27 @@ public static function getDataColumnComment(): iterable
],
];
}

/**
* TODO move to functional test once SqliteSchemaManager::selectForeignKeyColumns can honor database/schema name
* https://github.com/doctrine/dbal/blob/3.8.3/src/Schema/SqliteSchemaManager.php#L740
*/
public function testListTableForeignKeysDefaultDatabasePassing(): void
{
$conn = $this->createMock(Connection::class);

$manager = new class ($conn, new SQLitePlatform()) extends SQLiteSchemaManager {
public static string $passedDatabaseName;

protected function selectForeignKeyColumns(string $databaseName, ?string $tableName = null): Result
{
self::$passedDatabaseName = $databaseName;

return parent::selectForeignKeyColumns($databaseName, $tableName);
}
};

$manager->listTableForeignKeys('t');
self::assertSame('main', $manager::$passedDatabaseName);
}
}

0 comments on commit e13e34c

Please sign in to comment.