mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-16 15:08:54 +00:00
[ticket/17507] Fix column removal issues
PHPBB-17507
This commit is contained in:
parent
d2202b7747
commit
c943ffaf0e
1 changed files with 38 additions and 79 deletions
|
@ -300,12 +300,6 @@ class doctrine implements tools_interface
|
|||
*/
|
||||
public function sql_column_remove(string $table_name, string $column_name)
|
||||
{
|
||||
$return = $this->schema_drop_primary_key($table_name, $column_name);
|
||||
if ($return !== true)
|
||||
{
|
||||
return $return;
|
||||
}
|
||||
|
||||
return $this->alter_schema(
|
||||
function (Schema $schema) use ($table_name, $column_name): void
|
||||
{
|
||||
|
@ -811,7 +805,8 @@ 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_primary_key($table_name, $column_name);
|
||||
// $this->schema_drop_unique_key($table_name, $column_name);
|
||||
|
||||
$this->alter_table(
|
||||
$schema,
|
||||
|
@ -983,47 +978,6 @@ class doctrine implements tools_interface
|
|||
$table->setPrimaryKey($columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes primary key from a table
|
||||
*
|
||||
* @param string $table_name
|
||||
* @param string $column_name
|
||||
*
|
||||
* @return bool|string[]
|
||||
*
|
||||
* @throws SchemaException
|
||||
*/
|
||||
protected function schema_drop_primary_key(string $table_name, string $column_name): array|bool
|
||||
{
|
||||
// Check if this column is part of a primary key. If yes, remove the primary key.
|
||||
$primary_key_indexes = $this->get_filtered_index_list($table_name, false);
|
||||
|
||||
$primary_key_indexes = array_filter($primary_key_indexes, function($index) use ($column_name) {
|
||||
$index_columns = array_map('strtolower', $index->getUnquotedColumns());
|
||||
return in_array($column_name, $index_columns, true) && $index->isPrimary();
|
||||
});
|
||||
|
||||
$return = true;
|
||||
if (count($primary_key_indexes))
|
||||
{
|
||||
// For PostgreSQL, drop primary index first to avoid "Dependent objects still exist" error
|
||||
if (stripos($this->get_schema_manager()->getDatabasePlatform()->getname(), 'postgresql') !== false)
|
||||
{
|
||||
$this->get_schema_manager()->dropIndex('"primary"', $table_name);
|
||||
}
|
||||
|
||||
$return = $this->alter_schema(
|
||||
function (Schema $schema) use ($table_name, $column_name): void
|
||||
{
|
||||
$table = $schema->getTable($table_name);
|
||||
$table->dropPrimaryKey();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recreate an index of a table
|
||||
*
|
||||
|
@ -1035,43 +989,48 @@ class doctrine implements tools_interface
|
|||
*/
|
||||
protected function recreate_index(Table $table, Index $index, array $new_columns): void
|
||||
{
|
||||
if (!empty($new_columns))
|
||||
if ($index->isPrimary())
|
||||
{
|
||||
if ($table->hasPrimaryKey() && $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();
|
||||
}
|
||||
else if ($table->hasIndex($index->getName()))
|
||||
{
|
||||
$table->dropIndex($index->getName());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$table->dropIndex($index->getName());
|
||||
}
|
||||
|
||||
if (count($new_columns) > 0)
|
||||
if (count($new_columns) > 0)
|
||||
{
|
||||
if ($index->isPrimary())
|
||||
{
|
||||
if ($index->isPrimary())
|
||||
{
|
||||
$table->setPrimaryKey(
|
||||
$new_columns,
|
||||
$index->getName(),
|
||||
);
|
||||
}
|
||||
else if ($index->isUnique())
|
||||
{
|
||||
$table->addUniqueIndex(
|
||||
$new_columns,
|
||||
$index->getName(),
|
||||
$index->getOptions(),
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$table->addIndex(
|
||||
$new_columns,
|
||||
$index->getName(),
|
||||
$index->getFlags(),
|
||||
$index->getOptions(),
|
||||
);
|
||||
}
|
||||
$table->setPrimaryKey(
|
||||
$new_columns,
|
||||
$index->getName(),
|
||||
);
|
||||
}
|
||||
else if ($index->isUnique())
|
||||
{
|
||||
$table->addUniqueIndex(
|
||||
$new_columns,
|
||||
$index->getName(),
|
||||
$index->getOptions(),
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$table->addIndex(
|
||||
$new_columns,
|
||||
$index->getName(),
|
||||
$index->getFlags(),
|
||||
$index->getOptions(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue