mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Prevent duplicate plugin table entries (#14120)
* Prevent duplicate plugin table entries Some sort of race condition. Add a unique index, this will cause the create query to fail when it tries to add a new entry for an existing plugin. * Add index
This commit is contained in:
@@ -189,7 +189,11 @@ class PluginManager
|
|||||||
|
|
||||||
if (! $plugin) {
|
if (! $plugin) {
|
||||||
try {
|
try {
|
||||||
$plugin = Plugin::create([
|
// plugin should not exist, but check for safety
|
||||||
|
$plugin = Plugin::firstOrCreate([
|
||||||
|
'version' => 2,
|
||||||
|
'plugin_name' => $name,
|
||||||
|
], [
|
||||||
'plugin_name' => $name,
|
'plugin_name' => $name,
|
||||||
'plugin_active' => $name !== 'ExamplePlugin',
|
'plugin_active' => $name !== 'ExamplePlugin',
|
||||||
'version' => 2,
|
'version' => 2,
|
||||||
|
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class PluginsUniqueIndex extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
// cleanup duplicates
|
||||||
|
$plugins = DB::table('plugins')->groupBy(['version', 'plugin_name'])->select(['version', 'plugin_name'])->get();
|
||||||
|
$valid_plugins = [];
|
||||||
|
foreach ($plugins as $plugin) {
|
||||||
|
// find the newest id with settings
|
||||||
|
$valid_plugins[] = DB::table('plugins')
|
||||||
|
->where(['version' => $plugin->version, 'plugin_name' => $plugin->plugin_name])
|
||||||
|
->orderBy('settings', 'DESC')
|
||||||
|
->orderBy('plugin_id', 'DESC')
|
||||||
|
->value('plugin_id');
|
||||||
|
}
|
||||||
|
DB::table('plugins')->whereNotIn('plugin_id', $valid_plugins)->delete();
|
||||||
|
|
||||||
|
Schema::table('plugins', function (Blueprint $table) {
|
||||||
|
$table->unique(['version', 'plugin_name']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('plugins', function (Blueprint $table) {
|
||||||
|
$table->dropUnique('plugins_version_plugin_name_unique');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@@ -1356,6 +1356,7 @@ plugins:
|
|||||||
- { Field: settings, Type: longtext, 'Null': true, Extra: '' }
|
- { Field: settings, Type: longtext, 'Null': true, Extra: '' }
|
||||||
Indexes:
|
Indexes:
|
||||||
PRIMARY: { Name: PRIMARY, Columns: [plugin_id], Unique: true, Type: BTREE }
|
PRIMARY: { Name: PRIMARY, Columns: [plugin_id], Unique: true, Type: BTREE }
|
||||||
|
plugins_version_plugin_name_unique: { Name: plugins_version_plugin_name_unique, Columns: [version, plugin_name], Unique: true, Type: BTREE }
|
||||||
pollers:
|
pollers:
|
||||||
Columns:
|
Columns:
|
||||||
- { Field: id, Type: 'int unsigned', 'Null': false, Extra: auto_increment }
|
- { Field: id, Type: 'int unsigned', 'Null': false, Extra: auto_increment }
|
||||||
|
Reference in New Issue
Block a user