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

User: Fix and optimize user deletion logic - refs BT#22215 #6008

Merged
merged 1 commit into from
Dec 31, 2024
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
69 changes: 69 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20241230175100.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a single space around assignment operators


/* For licensing terms, see /license.txt */

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;

final class Version20241230175100 extends AbstractMigrationChamilo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing class doc comment

{
public function getDescription(): string
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function doc comment

{
return 'Fix cascading delete for c_lp_category_rel_user table foreign key dynamically';
}

public function up(Schema $schema): void
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function doc comment

{
// Find the foreign key name dynamically
$foreignKeyName = $this->getForeignKeyName('c_lp_category_rel_user', 'user_id');

if ($foreignKeyName) {
$this->addSql("ALTER TABLE c_lp_category_rel_user DROP FOREIGN KEY `$foreignKeyName`");
}

// Add the updated foreign key
$this->addSql('
ALTER TABLE c_lp_category_rel_user
ADD CONSTRAINT FK_83D35829A76ED395
FOREIGN KEY (user_id)
REFERENCES user(id)
ON DELETE SET NULL
ON UPDATE CASCADE
');
}

public function down(Schema $schema): void
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function doc comment

{
$this->addSql('ALTER TABLE c_lp_category_rel_user DROP FOREIGN KEY FK_83D35829A76ED395');

$this->addSql('
ALTER TABLE c_lp_category_rel_user
ADD CONSTRAINT c_lp_category_rel_user_ibfk_1
FOREIGN KEY (user_id)
REFERENCES user(id)
ON DELETE SET NULL
');
}

private function getForeignKeyName(string $tableName, string $columnName): ?string
{
$query = "
SELECT CONSTRAINT_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_NAME = :tableName
AND COLUMN_NAME = :columnName
AND TABLE_SCHEMA = DATABASE()
";

$result = $this->connection->fetchOne($query, [
'tableName' => $tableName,
'columnName' => $columnName,
]);

return $result ?: null;
}
}
Loading
Loading