Files
firefly-iii/app/Console/Commands/Upgrade/RenameAccountMeta.php

124 lines
3.4 KiB
PHP
Raw Normal View History

2019-06-21 19:07:11 +02:00
<?php
/**
* RenameAccountMeta.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-08-17 12:09:03 +02:00
declare(strict_types=1);
2019-06-21 19:07:11 +02:00
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException;
2019-06-21 19:07:11 +02:00
use FireflyIII\Models\AccountMeta;
use Illuminate\Console\Command;
2022-10-30 12:24:51 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2019-06-21 19:07:11 +02:00
/**
* Class RenameAccountMeta
*/
class RenameAccountMeta extends Command
{
use ShowsFriendlyMessages;
public const CONFIG_NAME = '480_rename_account_meta';
2019-06-21 19:07:11 +02:00
/**
* 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
2023-02-22 18:03:31 +01:00
* @throws ContainerExceptionInterface
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2023-02-22 18:03:31 +01:00
* @throws NotFoundExceptionInterface
2019-06-21 19:07:11 +02:00
*/
public function handle(): int
{
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
2019-06-21 19:07:11 +02:00
return 0;
}
2021-04-07 07:28:43 +02:00
2019-06-21 19:07:11 +02:00
$array = [
'accountRole' => 'account_role',
'ccType' => 'cc_type',
2019-06-23 05:53:01 +02:00
'accountNumber' => 'account_number',
2019-06-21 19:07:11 +02:00
'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]);
2019-06-23 05:53:01 +02:00
// delete empty entries while we're at it.
2020-03-15 08:16:16 +01:00
AccountMeta::where('name', $new)->where('data', '""')->delete();
2019-06-21 19:07:11 +02:00
}
$this->markAsExecuted();
if (0 === $count) {
$this->friendlyPositive('All account meta is OK.');
2019-06-21 19:07:11 +02:00
}
if (0 !== $count) {
$this->friendlyInfo(sprintf('Renamed %d account meta entries (entry).', $count));
2019-06-21 19:07:11 +02:00
}
return 0;
}
/**
* @return bool
2022-10-30 12:24:51 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2019-06-21 19:07:11 +02:00
*/
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
2022-10-30 12:24:51 +01:00
return (bool)$configVar->data;
2019-06-21 19:07:11 +02:00
}
return false;
2019-06-21 19:07:11 +02:00
}
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
}