Wrapped in a transaction

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-03-07 15:04:08 +00:00
parent fe08f39900
commit d64ee42ec3

View file

@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Models\Asset;
use App\Models\CustomField;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ToggleCustomfieldEncryption extends Command
{
@ -47,22 +48,26 @@ class ToggleCustomfieldEncryption extends Command
// If the field is not encrypted, make it encrypted and encrypt the data in the assets table for the
// corresponding field.
if ($field->field_encrypted == 0) {
$assets = Asset::whereNotNull($field->db_column)->get();
DB::transaction(function () use ($field) {
foreach ($assets as $asset) {
$asset->{$field->db_column} = encrypt($asset->{$field->db_column});
$asset->save();
if ($field->field_encrypted == 0) {
$assets = Asset::whereNotNull($field->db_column)->get();
foreach ($assets as $asset) {
$asset->{$field->db_column} = encrypt($asset->{$field->db_column});
$asset->save();
}
$field->field_encrypted = 1;
$field->save();
// This field is already encrypted. Do nothing.
} else {
$this->error('The custom field ' . $field->db_column.' is already encrypted. No action was taken.');
}
});
$field->field_encrypted = 1;
$field->save();
// This field is already encrypted. Do nothing.
} else {
$this->error('The custom field ' . $fieldname.' is already encrypted. No action was taken.');
}
// No matching column name found
} else {
$this->error('No matching results for unencrypted custom fields with db_column name: ' . $fieldname.'. Please check the fieldname.');
}