[ticket/17507] Fix PostgreSQL errors

PHPBB-17507
This commit is contained in:
rxu 2025-07-09 11:46:31 +07:00
parent c943ffaf0e
commit 69a4990b2b
No known key found for this signature in database
GPG key ID: 8117904FEDEFDD17
2 changed files with 21 additions and 46 deletions

View file

@ -14,8 +14,7 @@
namespace phpbb\db\doctrine;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
@ -32,7 +31,7 @@ use Doctrine\DBAL\Types\Type;
* to stay compatible with the existing DB we have to change its
* naming and not ours.
*/
class postgresql_platform extends PostgreSQL94Platform
class postgresql_platform extends PostgreSQLPlatform
{
/**
* {@inheritdoc}
@ -188,31 +187,19 @@ class postgresql_platform extends PostgreSQL94Platform
{
// If we have a primary or a unique index, we need to drop the constraint
// instead of the index itself or postgreSQL will reject the query.
if ($index instanceof Index)
if (is_string($index) && $table !== null && $index === $this->tableName($table) . '_pkey')
{
if ($index->isPrimary())
{
if ($table instanceof Table)
{
$table = $table->getQuotedName($this);
}
else if (!is_string($table))
{
throw new \InvalidArgumentException(
__METHOD__ . '() expects $table parameter to be string or ' . Table::class . '.'
);
}
return 'ALTER TABLE '.$table.' DROP CONSTRAINT '.$index->getQuotedName($this);
}
}
else if (! is_string($index))
{
throw new \InvalidArgumentException(
__METHOD__ . '() expects $index parameter to be string or ' . Index::class . '.'
);
return $this->getDropConstraintSQL($index, $this->tableName($table));
}
return parent::getDropIndexSQL($index, $table);
}
/**
* {@inheritDoc}
*/
private function tableName($table)
{
return $table instanceof Table ? $table->getName() : (string) $table;
}
}

View file

@ -96,7 +96,7 @@ class doctrine implements tools_interface
*/
protected function get_schema(): Schema
{
return $this->get_schema_manager()->createSchema();
return $this->get_schema_manager()->introspectSchema();
}
/**
@ -380,7 +380,7 @@ class doctrine implements tools_interface
{
try
{
$this->connection->executeQuery($this->get_schema_manager()->getDatabasePlatform()->getTruncateTableSQL($table_name));
$this->connection->executeQuery($this->connection->getDatabasePlatform()->getTruncateTableSQL($table_name));
}
catch (Exception $e)
{
@ -485,7 +485,7 @@ class doctrine implements tools_interface
$comparator = new comparator();
$schemaDiff = $comparator->compareSchemas($current_schema, $new_schema);
$queries = $schemaDiff->toSql($this->get_schema_manager()->getDatabasePlatform());
$queries = $schemaDiff->toSql($this->connection->getDatabasePlatform());
if ($this->return_statements)
{
@ -497,7 +497,6 @@ class doctrine implements tools_interface
// executeQuery() must be used here because $query might return a result set, for instance REPAIR does
$this->connection->executeQuery($query);
}
return true;
}
@ -629,7 +628,7 @@ class doctrine implements tools_interface
$table = $schema->createTable($table_name);
$short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix));
$dbms_name = $this->get_schema_manager()->getDatabasePlatform()->getName();
$dbms_name = $this->connection->getDatabasePlatform()->getName();
foreach ($table_data['COLUMNS'] as $column_name => $column_data)
{
@ -646,7 +645,7 @@ class doctrine implements tools_interface
? [$table_data['PRIMARY_KEY']]
: $table_data['PRIMARY_KEY'];
$table->setPrimaryKey($table_data['PRIMARY_KEY']);
$table->setPrimaryKey($table_data['PRIMARY_KEY'], false);
}
if (array_key_exists('KEYS', $table_data))
@ -728,7 +727,7 @@ class doctrine implements tools_interface
return false;
}
$dbms_name = $this->get_schema_manager()->getDatabasePlatform()->getName();
$dbms_name = $this->connection->getDatabasePlatform()->getName();
list($type, $options) = table_helper::convert_column_data($column_data, $dbms_name);
$table->addColumn($column_name, $type, $options);
@ -760,7 +759,7 @@ class doctrine implements tools_interface
return;
}
$dbms_name = $this->get_schema_manager()->getDatabasePlatform()->getName();
$dbms_name = $this->connection->getDatabasePlatform()->getName();
list($type, $options) = table_helper::convert_column_data($column_data, $dbms_name);
$options['type'] = Type::getType($type);
@ -805,13 +804,10 @@ class doctrine implements tools_interface
*/
protected function schema_column_remove(Schema $schema, string $table_name, string $column_name, bool $safe_check = false): void
{
// $this->schema_drop_primary_key($table_name, $column_name);
// $this->schema_drop_unique_key($table_name, $column_name);
$this->alter_table(
$schema,
$table_name,
function (Table $table) use ($schema, $table_name, $column_name, $safe_check): void
function (Table $table) use (&$schema, $table_name, $column_name, $safe_check): void
{
if ($safe_check && !$table->hasColumn($column_name))
{
@ -991,15 +987,7 @@ class doctrine implements tools_interface
{
if ($index->isPrimary())
{
// For PostgreSQL, drop primary index first to avoid "Dependent objects still exist" error
if (stripos($this->connection->getDatabasePlatform()->getname(), 'postgresql') !== false)
{
$this->get_schema_manager()->dropUniqueConstraint($table->getPrimaryKey()->getName(), $table->getName());
}
else
{
$table->dropPrimaryKey();
}
$table->dropPrimaryKey();
}
else
{