-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create unique index for the table
like
- Loading branch information
1 parent
2329da4
commit 8d9e8da
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
protected/humhub/modules/like/migrations/m240425_144905_unique_index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use humhub\components\Migration; | ||
use humhub\modules\like\models\Like; | ||
|
||
|
||
/** | ||
* Class m240425_144905_unique_index | ||
*/ | ||
class m240425_144905_unique_index extends Migration | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function safeUp() | ||
{ | ||
$likes = Like::find() | ||
->groupBy(['object_model', 'object_id', 'created_by']) | ||
->having('COUNT(*) > 1'); | ||
|
||
foreach ($likes->each() as $like) { | ||
/* @var Like $like */ | ||
Like::deleteAll(['AND', | ||
['object_model' => $like->object_model], | ||
['object_id' => $like->object_id], | ||
['created_by' => $like->created_by], | ||
['!=', 'id', $like->id], | ||
]); | ||
|
||
$like->flushCache(); | ||
} | ||
|
||
$this->safeCreateIndex('unique-object-user', 'like', ['object_model', 'object_id', 'created_by'], true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function safeDown() | ||
{ | ||
echo "m240425_144905_unique_index cannot be reverted.\n"; | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters