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

Merge 3.9.x up into 3.10.x #6757

Merged
merged 4 commits into from
Feb 2, 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
9 changes: 8 additions & 1 deletion src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use function preg_match;
use function preg_match_all;
use function sprintf;
use function str_contains;
use function str_ends_with;
use function str_replace;
use function str_starts_with;
Expand Down Expand Up @@ -1777,11 +1778,17 @@ private function generateIdentifierName($identifier): string

protected function getCommentOnTableSQL(string $tableName, ?string $comment): string
{
if (str_contains($tableName, '.')) {
[$schemaName, $tableName] = explode('.', $tableName);
} else {
$schemaName = 'dbo';
}

return $this->getAddExtendedPropertySQL(
'MS_Description',
$comment,
'SCHEMA',
$this->quoteStringLiteral('dbo'),
$this->quoteStringLiteral($schemaName),
'TABLE',
$this->quoteStringLiteral($this->unquoteSingleIdentifier($tableName)),
);
Expand Down
27 changes: 20 additions & 7 deletions src/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,16 @@ protected function _getPortableTableIndexesList($tableIndexes, $tableName = null
foreach ($tableIndexes as $row) {
$colNumbers = array_map('intval', explode(' ', $row['indkey']));
$columnNameSql = sprintf(
'SELECT attnum, attname FROM pg_attribute WHERE attrelid=%d AND attnum IN (%s) ORDER BY attnum ASC',
<<<'SQL'
SELECT attnum,
quote_ident(attname) AS attname
FROM pg_attribute
WHERE attrelid = %d
AND attnum IN (%s)
ORDER BY attnum
SQL,
$row['indrelid'],
implode(' ,', $colNumbers),
implode(', ', $colNumbers),
);

$indexColumns = $this->_conn->fetchAllAssociative($columnNameSql);
Expand Down Expand Up @@ -612,7 +619,7 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
$sql = 'SELECT';

if ($tableName === null) {
$sql .= ' c.relname AS table_name, n.nspname AS schema_name,';
$sql .= ' quote_ident(c.relname) AS table_name, quote_ident(n.nspname) AS schema_name,';
}

$sql .= sprintf(<<<'SQL'
Expand Down Expand Up @@ -664,7 +671,7 @@ protected function selectIndexColumns(string $databaseName, ?string $tableName =
$sql = 'SELECT';

if ($tableName === null) {
$sql .= ' tc.relname AS table_name, tn.nspname AS schema_name,';
$sql .= ' quote_ident(tc.relname) AS table_name, quote_ident(tn.nspname) AS schema_name,';
}

$sql .= <<<'SQL'
Expand Down Expand Up @@ -698,7 +705,7 @@ protected function selectForeignKeyColumns(string $databaseName, ?string $tableN
$sql = 'SELECT';

if ($tableName === null) {
$sql .= ' tc.relname AS table_name, tn.nspname AS schema_name,';
$sql .= ' quote_ident(tc.relname) AS table_name, quote_ident(tn.nspname) AS schema_name,';
}

$sql .= <<<'SQL'
Expand Down Expand Up @@ -726,7 +733,8 @@ protected function selectForeignKeyColumns(string $databaseName, ?string $tableN
protected function fetchTableOptionsByTable(string $databaseName, ?string $tableName = null): array
{
$sql = <<<'SQL'
SELECT c.relname,
SELECT n.nspname AS schema_name,
c.relname AS table_name,
CASE c.relpersistence WHEN 'u' THEN true ELSE false END as unlogged,
obj_description(c.oid, 'pg_class') AS comment
FROM pg_class c
Expand All @@ -738,7 +746,12 @@ protected function fetchTableOptionsByTable(string $databaseName, ?string $table

$sql .= ' WHERE ' . implode(' AND ', $conditions);

return $this->_conn->fetchAllAssociativeIndexed($sql);
$tableOptions = [];
foreach ($this->_conn->iterateAssociative($sql) as $row) {
$tableOptions[$this->_getPortableTableDefinition($row)] = $row;
}

return $tableOptions;
}

/**
Expand Down
26 changes: 15 additions & 11 deletions src/Schema/SQLServerSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,15 @@ protected function _getPortableTableForeignKeysList($tableForeignKeys)
$name = $tableForeignKey['ForeignKey'];

if (! isset($foreignKeys[$name])) {
$referencedTableName = $tableForeignKey['ReferenceTableName'];

if ($tableForeignKey['ReferenceSchemaName'] !== 'dbo') {
$referencedTableName = $tableForeignKey['ReferenceSchemaName'] . '.' . $referencedTableName;
}

$foreignKeys[$name] = [
'local_columns' => [$tableForeignKey['ColumnName']],
'foreign_table' => $tableForeignKey['ReferenceTableName'],
'foreign_table' => $referencedTableName,
'foreign_columns' => [$tableForeignKey['ReferenceColumnName']],
'name' => $name,
'options' => [
Expand Down Expand Up @@ -556,31 +562,29 @@ protected function fetchTableOptionsByTable(string $databaseName, ?string $table
{
$sql = <<<'SQL'
SELECT
tbl.name,
scm.name AS schema_name,
tbl.name AS table_name,
p.value AS [table_comment]
FROM
sys.tables AS tbl
JOIN sys.schemas AS scm
ON tbl.schema_id = scm.schema_id
INNER JOIN sys.extended_properties AS p ON p.major_id=tbl.object_id AND p.minor_id=0 AND p.class=1
SQL;

$conditions = ["SCHEMA_NAME(tbl.schema_id) = N'dbo'", "p.name = N'MS_Description'"];
$params = [];
$conditions = ["p.name = N'MS_Description'"];

if ($tableName !== null) {
$conditions[] = "tbl.name = N'" . $tableName . "'";
$conditions[] = $this->getTableWhereClause($tableName, 'scm.name', 'tbl.name');
}

$sql .= ' WHERE ' . implode(' AND ', $conditions);

/** @var array<string,array<string,mixed>> $metadata */
$metadata = $this->_conn->executeQuery($sql, $params)
->fetchAllAssociativeIndexed();

$tableOptions = [];
foreach ($metadata as $table => $data) {
foreach ($this->_conn->iterateAssociative($sql) as $data) {
$data = array_change_key_case($data, CASE_LOWER);

$tableOptions[$table] = [
$tableOptions[$this->_getPortableTableDefinition($data)] = [
'comment' => $data['table_comment'],
];
}
Expand Down
116 changes: 0 additions & 116 deletions tests/Functional/Schema/OracleSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,122 +71,6 @@ public function testAlterTableColumnNotNull(callable $comparatorFactory): void
self::assertTrue($columns['bar']->getNotnull());
}

public function testListTableDetailsWithDifferentIdentifierQuotingRequirements(): void
{
$primaryTableName = '"Primary_Table"';
$offlinePrimaryTable = new Table($primaryTableName);
$offlinePrimaryTable->addColumn(
'"Id"',
Types::INTEGER,
['autoincrement' => true, 'comment' => 'Explicit casing.'],
);
$offlinePrimaryTable->addColumn('select', Types::INTEGER, ['comment' => 'Reserved keyword.']);
$offlinePrimaryTable->addColumn('foo', Types::INTEGER, ['comment' => 'Implicit uppercasing.']);
$offlinePrimaryTable->addColumn('BAR', Types::INTEGER);
$offlinePrimaryTable->addColumn('"BAZ"', Types::INTEGER);
$offlinePrimaryTable->addIndex(['select'], 'from');
$offlinePrimaryTable->addIndex(['foo'], 'foo_index');
$offlinePrimaryTable->addIndex(['BAR'], 'BAR_INDEX');
$offlinePrimaryTable->addIndex(['"BAZ"'], 'BAZ_INDEX');
$offlinePrimaryTable->setPrimaryKey(['"Id"']);

$foreignTableName = 'foreign';
$offlineForeignTable = new Table($foreignTableName);
$offlineForeignTable->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
$offlineForeignTable->addColumn('"Fk"', Types::INTEGER);
$offlineForeignTable->addIndex(['"Fk"'], '"Fk_index"');
$offlineForeignTable->addForeignKeyConstraint(
$primaryTableName,
['"Fk"'],
['"Id"'],
[],
'"Primary_Table_Fk"',
);
$offlineForeignTable->setPrimaryKey(['id']);

$this->dropTableIfExists($foreignTableName);
$this->dropTableIfExists($primaryTableName);

$this->schemaManager->createTable($offlinePrimaryTable);
$this->schemaManager->createTable($offlineForeignTable);

$onlinePrimaryTable = $this->schemaManager->introspectTable($primaryTableName);
$onlineForeignTable = $this->schemaManager->introspectTable($foreignTableName);

$platform = $this->connection->getDatabasePlatform();

// Primary table assertions
self::assertSame($primaryTableName, $onlinePrimaryTable->getQuotedName($platform));

self::assertTrue($onlinePrimaryTable->hasColumn('"Id"'));
self::assertSame('"Id"', $onlinePrimaryTable->getColumn('"Id"')->getQuotedName($platform));
self::assertTrue($onlinePrimaryTable->hasPrimaryKey());

$primaryKey = $onlinePrimaryTable->getPrimaryKey();

self::assertNotNull($primaryKey);
self::assertSame(['"Id"'], $primaryKey->getQuotedColumns($platform));

self::assertTrue($onlinePrimaryTable->hasColumn('select'));
self::assertSame('"select"', $onlinePrimaryTable->getColumn('select')->getQuotedName($platform));

self::assertTrue($onlinePrimaryTable->hasColumn('foo'));
self::assertSame('FOO', $onlinePrimaryTable->getColumn('foo')->getQuotedName($platform));

self::assertTrue($onlinePrimaryTable->hasColumn('BAR'));
self::assertSame('BAR', $onlinePrimaryTable->getColumn('BAR')->getQuotedName($platform));

self::assertTrue($onlinePrimaryTable->hasColumn('"BAZ"'));
self::assertSame('BAZ', $onlinePrimaryTable->getColumn('"BAZ"')->getQuotedName($platform));

self::assertTrue($onlinePrimaryTable->hasIndex('from'));
self::assertTrue($onlinePrimaryTable->getIndex('from')->hasColumnAtPosition('"select"'));
self::assertSame(['"select"'], $onlinePrimaryTable->getIndex('from')->getQuotedColumns($platform));

self::assertTrue($onlinePrimaryTable->hasIndex('foo_index'));
self::assertTrue($onlinePrimaryTable->getIndex('foo_index')->hasColumnAtPosition('foo'));
self::assertSame(['FOO'], $onlinePrimaryTable->getIndex('foo_index')->getQuotedColumns($platform));

self::assertTrue($onlinePrimaryTable->hasIndex('BAR_INDEX'));
self::assertTrue($onlinePrimaryTable->getIndex('BAR_INDEX')->hasColumnAtPosition('BAR'));
self::assertSame(['BAR'], $onlinePrimaryTable->getIndex('BAR_INDEX')->getQuotedColumns($platform));

self::assertTrue($onlinePrimaryTable->hasIndex('BAZ_INDEX'));
self::assertTrue($onlinePrimaryTable->getIndex('BAZ_INDEX')->hasColumnAtPosition('"BAZ"'));
self::assertSame(['BAZ'], $onlinePrimaryTable->getIndex('BAZ_INDEX')->getQuotedColumns($platform));

// Foreign table assertions
self::assertTrue($onlineForeignTable->hasColumn('id'));
self::assertSame('ID', $onlineForeignTable->getColumn('id')->getQuotedName($platform));
self::assertTrue($onlineForeignTable->hasPrimaryKey());

$primaryKey = $onlineForeignTable->getPrimaryKey();

self::assertNotNull($primaryKey);
self::assertSame(['ID'], $primaryKey->getQuotedColumns($platform));

self::assertTrue($onlineForeignTable->hasColumn('"Fk"'));
self::assertSame('"Fk"', $onlineForeignTable->getColumn('"Fk"')->getQuotedName($platform));

self::assertTrue($onlineForeignTable->hasIndex('"Fk_index"'));
self::assertTrue($onlineForeignTable->getIndex('"Fk_index"')->hasColumnAtPosition('"Fk"'));
self::assertSame(['"Fk"'], $onlineForeignTable->getIndex('"Fk_index"')->getQuotedColumns($platform));

self::assertTrue($onlineForeignTable->hasForeignKey('"Primary_Table_Fk"'));
self::assertSame(
$primaryTableName,
$onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignTableName($platform),
);
self::assertSame(
['"Fk"'],
$onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedLocalColumns($platform),
);
self::assertSame(
['"Id"'],
$onlineForeignTable->getForeignKey('"Primary_Table_Fk"')->getQuotedForeignColumns($platform),
);
}

public function testListTableColumnsSameTableNamesInDifferentSchemas(): void
{
$table = $this->createListTableColumns();
Expand Down
39 changes: 0 additions & 39 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

use function array_map;
use function array_merge;
use function array_pop;
use function array_unshift;
use function assert;
use function count;
Expand Down Expand Up @@ -163,44 +162,6 @@ public function testAlterTableAutoIncrementDrop(callable $comparatorFactory): vo
self::assertFalse($tableFinal->getColumn('id')->getAutoincrement());
}

public function testTableWithSchema(): void
{
$this->connection->executeStatement('CREATE SCHEMA nested');

$nestedRelatedTable = new Table('nested.schemarelated');
$column = $nestedRelatedTable->addColumn('id', Types::INTEGER);
$column->setAutoincrement(true);
$nestedRelatedTable->setPrimaryKey(['id']);

$nestedSchemaTable = new Table('nested.schematable');
$column = $nestedSchemaTable->addColumn('id', Types::INTEGER);
$column->setAutoincrement(true);
$nestedSchemaTable->setPrimaryKey(['id']);
$nestedSchemaTable->addForeignKeyConstraint($nestedRelatedTable, ['id'], ['id']);

$this->schemaManager->createTable($nestedRelatedTable);
$this->schemaManager->createTable($nestedSchemaTable);

$tableNames = $this->schemaManager->listTableNames();
self::assertContains('nested.schematable', $tableNames);

$tables = $this->schemaManager->listTables();
self::assertNotNull($this->findTableByName($tables, 'nested.schematable'));

$nestedSchemaTable = $this->schemaManager->introspectTable('nested.schematable');
self::assertTrue($nestedSchemaTable->hasColumn('id'));

$primaryKey = $nestedSchemaTable->getPrimaryKey();

self::assertNotNull($primaryKey);
self::assertEquals(['id'], $primaryKey->getColumns());

$relatedFks = $nestedSchemaTable->getForeignKeys();
self::assertCount(1, $relatedFks);
$relatedFk = array_pop($relatedFks);
self::assertEquals('nested.schemarelated', $relatedFk->getForeignTableName());
}

public function testListSameTableNameColumnsWithDifferentSchema(): void
{
$this->connection->executeStatement('CREATE SCHEMA another');
Expand Down
Loading