librenms-librenms/database/migrations/2019_10_03_211702_serialize_config.php
Tony Murray be04388137 Store config data serialized (#10651)
* Store config data serialized
This way we can store null, booleans, and more reliably.

* Use model to get the mutated output.

* fix whitespace and unused function

* use json_encode/decode and casts
migration to transfer

* json_encode JSON_UNESCAPED_SLASHES

* Use JSON_UNESCAPED_SLASHES.  That is only relevant if you are printing into an HTML page.

* pre-encode the seed...

* filter other fields besides config_value
2019-10-06 21:51:22 +00:00

50 lines
1.4 KiB
PHP

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class SerializeConfig extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::table('config')->get()->each(function ($config) {
$value = $config->config_value;
if (filter_var($value, FILTER_VALIDATE_INT)) {
$value = (int)$value;
} elseif (filter_var($value, FILTER_VALIDATE_FLOAT)) {
$value = (float)$value;
} elseif (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null) {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
DB::table('config')
->where('config_id', $config->config_id)
->update(['config_value' => json_encode($value)]);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::table('config')->get()->each(function ($config) {
$value = json_decode($config->config_value);
$value = is_bool($value) ? var_export($value, true) : (string)$value;
DB::table('config')
->where('config_id', $config->config_id)
->update(['config_value' => $value]);
});
}
}