Skip to content

Commit

Permalink
Create unique index for the table like
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Apr 25, 2024
1 parent 2329da4 commit 8d9e8da
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ HumHub Changelog
- Fix #6919: Fix saving of user profile country field value and enable a searching by country title
- Fix #6919: Migration to revert user profile country names to Iso 3166 codes
- Fix #6966: Make "Invite new people" always possible for Admins
- Fix #6969: Create unique index for the table `like`

1.15.4 (March 20, 2024)
-----------------------
Expand Down
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;
}
}
9 changes: 7 additions & 2 deletions protected/humhub/modules/like/models/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function() use ($objectModel, $objectId) {
*/
public function afterSave($insert, $changedAttributes)
{
Yii::$app->cache->delete('likes_' . $this->object_model . "_" . $this->object_id);
$this->flushCache();

if ($insert) {
\humhub\modules\like\activities\Liked::instance()->about($this)->save();
Expand All @@ -119,8 +119,13 @@ public function afterSave($insert, $changedAttributes)
*/
public function beforeDelete()
{
Yii::$app->cache->delete('likes_' . $this->object_model . "_" . $this->object_id);
$this->flushCache();
return parent::beforeDelete();
}

public function flushCache()
{
Yii::$app->cache->delete('likes_' . $this->object_model . '_' . $this->object_id);
}

}

0 comments on commit 8d9e8da

Please sign in to comment.