Make settings index(name, module_id) unique (#4832)

This commit is contained in:
Yuriy Bakhtin 2021-02-04 13:22:26 +03:00 committed by GitHub
parent 9c9b3ef31f
commit d130ee7c54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -9,6 +9,7 @@ HumHub Changelog
- Fix #4569: Prevent double module registration - Fix #4569: Prevent double module registration
- Fix #4389: Require to check a checkbox if the profile field is required - Fix #4389: Require to check a checkbox if the profile field is required
- Fix #3687: Disable profile field type for existing record - Fix #3687: Disable profile field type for existing record
- Fix #4825: Make settings index(name, module_id) unique
1.7.2 (January 21, 2021) 1.7.2 (January 21, 2021)

View File

@ -0,0 +1,44 @@
<?php
use humhub\models\Setting;
use yii\db\Migration;
/**
* Class m210204_054203_fix_settings_unique_index
*/
class m210204_054203_fix_settings_unique_index extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
// Remove old duplicated settings, keep only the latest saved value of each duplicated setting
$duplicateSettings = Setting::find()
->select(['name', 'module_id', 'MAX(id) AS latest_id'])
->groupBy(['name', 'module_id'])
->having('COUNT(*) > 1')
->asArray()
->all();
foreach ($duplicateSettings as $duplicateSetting) {
$this->delete('setting', 'name=:name AND module_id=:module_id AND id!=:latest_id', [
':name' => $duplicateSetting['name'],
':module_id' => $duplicateSetting['module_id'],
':latest_id' => $duplicateSetting['latest_id'],
]);
}
// Convert the index(name, module_id) to unique
$this->dropIndex('unique-setting', 'setting');
$this->createIndex('unique-setting', 'setting', ['name', 'module_id'], true);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m210204_054203_fix_settings_unique_index cannot be reverted.\n";
return false;
}
}