. */ declare(strict_types=1); namespace FireflyIII\Console\Commands\Upgrade; use FireflyIII\Console\Commands\ShowsFriendlyMessages; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\AccountMeta; use Illuminate\Console\Command; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; /** * Class RenameAccountMeta */ class RenameAccountMeta extends Command { use ShowsFriendlyMessages; public const CONFIG_NAME = '480_rename_account_meta'; /** * The console command description. * * @var string */ protected $description = 'Rename account meta-data to new format.'; /** * The name and signature of the console command. * * @var string */ protected $signature = 'firefly-iii:rename-account-meta {--F|force : Force the execution of this command.}'; /** * Execute the console command. * * @return int * @throws ContainerExceptionInterface * @throws FireflyException * @throws NotFoundExceptionInterface */ public function handle(): int { if ($this->isExecuted() && true !== $this->option('force')) { $this->friendlyInfo('This command has already been executed.'); return 0; } $array = [ 'accountRole' => 'account_role', 'ccType' => 'cc_type', 'accountNumber' => 'account_number', 'ccMonthlyPaymentDate' => 'cc_monthly_payment_date', ]; $count = 0; /** * @var string $old * @var string $new */ foreach ($array as $old => $new) { $count += AccountMeta::where('name', $old)->update(['name' => $new]); // delete empty entries while we're at it. AccountMeta::where('name', $new)->where('data', '""')->delete(); } $this->markAsExecuted(); if (0 === $count) { $this->friendlyPositive('All account meta is OK.'); } if (0 !== $count) { $this->friendlyInfo(sprintf('Renamed %d account meta entries (entry).', $count)); } return 0; } /** * @return bool * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ private function isExecuted(): bool { $configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false); if (null !== $configVar) { return (bool)$configVar->data; } return false; } /** * */ private function markAsExecuted(): void { app('fireflyconfig')->set(self::CONFIG_NAME, true); } }