Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce AbstractSchemaManager::getCurrentSchemaName() #6746

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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 UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ awareness about deprecated code.

# Upgrade to 4.3

## Deprecated `PostgreSQLSchemaManager` methods related to the current schema

The following `PostgreSQLSchemaManager` methods have been deprecated:
- `getCurrentSchema()` - use `getCurrentSchemaName()` instead
- `determineCurrentSchema()` - use `determineCurrentSchemaName()` instead

## Deprecated using `Schema` as `AbstractAsset`

Relying on the `Schema` class extending `AbstractAsset` is deprecated. Use only the methods declared immediately in
Expand Down
52 changes: 52 additions & 0 deletions src/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@
*/
abstract class AbstractSchemaManager
{
/**
* The current schema name determined from the connection. The <code>null</code> value means that there is no
* schema currently selected within the connection.
*
* The property should be accessed only when {@link $currentSchemaDetermined} is set to <code>true</code>. If the
* currently used database platform doesn't support schemas, the property will remain uninitialized.
*
* The property is initialized only once. If the underlying connection switches to a different schema, a new schema
* manager instance will have to be created to reflect this change.
*/
private ?string $currentSchemaName;

/**
* Indicates whether the current schema has been determined.
*/
private bool $currentSchemaDetermined = false;

/** @param T $platform */
public function __construct(protected Connection $connection, protected AbstractPlatform $platform)
{
Expand Down Expand Up @@ -235,6 +252,40 @@
return $tables;
}

/**
* Returns the current schema name used by the schema manager connection.
*
* The <code>null</code> value means that there is no schema currently selected within the connection or the
* corresponding database platform doesn't support schemas.
*
* @throws Exception
*/
final protected function getCurrentSchemaName(): ?string
{
if (! $this->platform->supportsSchemas()) {
return null;
}

if (! $this->currentSchemaDetermined) {
$this->currentSchemaName = $this->determineCurrentSchemaName();
$this->currentSchemaDetermined = true;
}

return $this->currentSchemaName;
}

/**
* Determines the name of the current schema.
*
* If the corresponding database platform supports schemas, the schema manager must implement this method.
*
* @throws Exception
*/
protected function determineCurrentSchemaName(): ?string

Check warning on line 284 in src/Schema/AbstractSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchemaManager.php#L284

Added line #L284 was not covered by tests
{
throw NotSupported::new(__METHOD__);

Check warning on line 286 in src/Schema/AbstractSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/AbstractSchemaManager.php#L286

Added line #L286 was not covered by tests
}

/**
* An extension point for those platforms where case sensitivity of the object name depends on whether it's quoted.
*
Expand Down Expand Up @@ -829,6 +880,7 @@
{
$schemaConfig = new SchemaConfig();
$schemaConfig->setMaxIdentifierLength($this->platform->getMaxIdentifierLength());
$schemaConfig->setName($this->getCurrentSchemaName());

$params = $this->connection->getParams();
if (! isset($params['defaultTableOptions'])) {
Expand Down
22 changes: 10 additions & 12 deletions src/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
*/
class PostgreSQLSchemaManager extends AbstractSchemaManager
{
private ?string $currentSchema = null;

/**
* {@inheritDoc}
*/
Expand All @@ -52,28 +50,23 @@ public function listSchemaNames(): array
);
}

public function createSchemaConfig(): SchemaConfig
{
$config = parent::createSchemaConfig();

$config->setName($this->getCurrentSchema());

return $config;
}

/**
* Returns the name of the current schema.
*
* @deprecated Use {@link getCurrentSchemaName()} instead
*
* @throws Exception
*/
protected function getCurrentSchema(): ?string
{
return $this->currentSchema ??= $this->determineCurrentSchema();
return $this->getCurrentSchemaName();
}

/**
* Determines the name of the current schema.
*
* @deprecated Use {@link determineCurrentSchemaName()} instead
*
* @throws Exception
*/
protected function determineCurrentSchema(): string
Expand All @@ -84,6 +77,11 @@ protected function determineCurrentSchema(): string
return $currentSchema;
}

protected function determineCurrentSchemaName(): ?string
{
return $this->determineCurrentSchema();
}

/**
* {@inheritDoc}
*/
Expand Down
14 changes: 2 additions & 12 deletions src/Schema/SQLServerSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey)
*/
protected function _getPortableTableDefinition(array $table): string
{
if ($table['schema_name'] !== 'dbo') {
if ($table['schema_name'] !== $this->getCurrentSchemaName()) {
return $table['schema_name'] . '.' . $table['table_name'];
}

Expand Down Expand Up @@ -273,15 +273,6 @@ public function createComparator(/* ComparatorConfig $config = new ComparatorCon
);
}

public function createSchemaConfig(): SchemaConfig
{
$config = parent::createSchemaConfig();

$config->setName($this->getCurrentSchemaName());

return $config;
}

/** @throws Exception */
private function getDatabaseCollation(): string
{
Expand All @@ -300,8 +291,7 @@ private function getDatabaseCollation(): string
return $this->databaseCollation;
}

/** @throws Exception */
private function getCurrentSchemaName(): ?string
protected function determineCurrentSchemaName(): ?string
{
$schemaName = $this->connection->fetchOne('SELECT SCHEMA_NAME()');
assert($schemaName !== false);
Expand Down
5 changes: 5 additions & 0 deletions tests/Functional/Schema/Db2SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function testListDatabases(): void

$this->schemaManager->listDatabases();
}

public function getExpectedDefaultSchemaName(): ?string
{
return null;
}
}
5 changes: 5 additions & 0 deletions tests/Functional/Schema/MySQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,4 +615,9 @@ public function testSchemaDiffWithCustomColumnTypeWhileDatabaseTypeDiffers(): vo
' The column should be changed from VARCAHR TO INT',
);
}

public function getExpectedDefaultSchemaName(): ?string
{
return null;
}
}
5 changes: 5 additions & 0 deletions tests/Functional/Schema/OracleSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,9 @@ public function testQuotedTableNameRemainsQuotedInSchema(): void
$columns = $schemaManager->listTableColumns('"tester"');
self::assertCount(1, $columns);
}

public function getExpectedDefaultSchemaName(): ?string
{
return null;
}
}
6 changes: 6 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,12 @@ public function testPartitionTable(): void
));
self::assertSame(1, $partitionsCount);
}

/** @link https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PUBLIC */
public function getExpectedDefaultSchemaName(): string
{
return 'public';
}
}

class MoneyType extends Type
Expand Down
6 changes: 6 additions & 0 deletions tests/Functional/Schema/SQLServerSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,10 @@ public function testNvarcharMaxIsLengthMinus1(): void
self::assertSame(-1, $table->getColumn('col_nvarchar_max')->getLength());
self::assertSame(128, $table->getColumn('col_nvarchar')->getLength());
}

/** @link https://learn.microsoft.com/en-us/sql/relational-databases/security/authentication-access/ownership-and-user-schema-separation?view=sql-server-ver16#the-dbo-schema */
public function getExpectedDefaultSchemaName(): string
{
return 'dbo';
}
}
5 changes: 5 additions & 0 deletions tests/Functional/Schema/SQLiteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,9 @@ public function testCommentInQuotedTable(): void
$table = $this->schemaManager->introspectTable('table_with_comment');
self::assertSame('This is a comment', $table->getComment());
}

public function getExpectedDefaultSchemaName(): ?string
{
return null;
}
}
17 changes: 9 additions & 8 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,14 +1340,15 @@ protected function findTableByName(array $tables, string $name): ?Table

return null;
}
}

interface ListTableColumnsDispatchEventListener
{
public function onSchemaColumnDefinition(): void;
}
/** @throws Exception */
public function testDefaultSchemaName(): void
{
self::assertSame(
$this->getExpectedDefaultSchemaName(),
$this->schemaManager->createSchemaConfig()->getName(),
);
}

interface ListTableIndexesDispatchEventListener
{
public function onSchemaIndexDefinition(): void;
abstract public function getExpectedDefaultSchemaName(): ?string;
}