-
Notifications
You must be signed in to change notification settings - Fork 492
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing class doc comment |
||
{ | ||
public function getDescription(): string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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