Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
8 changed files
with
140 additions
and
192 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
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
49 changes: 49 additions & 0 deletions
49
database/migrations/2019_10_03_211702_serialize_config.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,49 @@ | ||
<?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]); | ||
}); | ||
} | ||
} |
Oops, something went wrong.