2019-08-26 07:12:46 +02:00
|
|
|
<?php
|
2020-06-30 19:05:35 +02:00
|
|
|
|
2019-08-26 07:12:46 +02:00
|
|
|
/**
|
|
|
|
* MigrateRecurrenceMeta.php
|
2020-01-23 20:35:02 +01:00
|
|
|
* Copyright (c) 2020 james@firefly-iii.org
|
2019-08-26 07:12:46 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2019-08-26 07:12:46 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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.
|
2019-08-26 07:12:46 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2019-08-26 07:12:46 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2019-08-26 07:12:46 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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-26 07:12:46 +02:00
|
|
|
*/
|
|
|
|
|
2020-06-30 19:05:35 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-26 07:12:46 +02:00
|
|
|
namespace FireflyIII\Console\Commands\Upgrade;
|
|
|
|
|
2023-06-20 07:16:56 +02:00
|
|
|
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
|
2023-01-03 06:48:53 +01:00
|
|
|
use FireflyIII\Models\Recurrence;
|
2019-08-26 07:12:46 +02:00
|
|
|
use FireflyIII\Models\RecurrenceMeta;
|
|
|
|
use FireflyIII\Models\RecurrenceTransactionMeta;
|
|
|
|
use Illuminate\Console\Command;
|
2021-09-18 10:20:19 +02:00
|
|
|
use JsonException;
|
2022-10-30 12:24:51 +01:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2019-08-26 07:12:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MigrateRecurrenceMeta
|
|
|
|
*/
|
|
|
|
class MigrateRecurrenceMeta extends Command
|
|
|
|
{
|
2023-06-20 07:16:56 +02:00
|
|
|
use ShowsFriendlyMessages;
|
|
|
|
|
2023-12-02 12:56:48 +01:00
|
|
|
public const string CONFIG_NAME = '481_migrate_recurrence_meta';
|
2023-11-05 09:54:53 +01:00
|
|
|
|
2019-08-26 07:12:46 +02:00
|
|
|
protected $description = 'Migrate recurrence meta data';
|
2023-11-05 09:54:53 +01:00
|
|
|
|
2019-08-26 07:12:46 +02:00
|
|
|
protected $signature = 'firefly-iii:migrate-recurrence-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 JsonException
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws NotFoundExceptionInterface
|
2019-08-26 07:12:46 +02:00
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
|
|
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyInfo('This command has already been executed.');
|
2019-08-26 07:12:46 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$count = $this->migrateMetaData();
|
|
|
|
|
|
|
|
if (0 === $count) {
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyPositive('No recurrence meta data migrated.');
|
2019-08-26 07:12:46 +02:00
|
|
|
}
|
|
|
|
if ($count > 0) {
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyInfo(sprintf('Migrated %d meta data entries', $count));
|
2019-08-26 07:12:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->markAsExecuted();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
2022-10-30 12:24:51 +01:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
2019-08-26 07:12:46 +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-08-26 07:12:46 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 10:26:12 +02:00
|
|
|
return false;
|
2019-08-26 07:12:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-21 12:34:58 +02:00
|
|
|
* @return int
|
|
|
|
* @throws JsonException
|
2019-08-26 07:12:46 +02:00
|
|
|
*/
|
2023-06-21 12:34:58 +02:00
|
|
|
private function migrateMetaData(): int
|
2019-08-26 07:12:46 +02:00
|
|
|
{
|
2023-06-21 12:34:58 +02:00
|
|
|
$count = 0;
|
|
|
|
// get all recurrence meta data:
|
|
|
|
$collection = RecurrenceMeta::with('recurrence')->get();
|
|
|
|
/** @var RecurrenceMeta $meta */
|
|
|
|
foreach ($collection as $meta) {
|
|
|
|
$count += $this->migrateEntry($meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
2019-08-26 07:12:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param RecurrenceMeta $meta
|
2019-08-26 07:12:46 +02:00
|
|
|
*
|
|
|
|
* @return int
|
2021-09-18 10:20:19 +02:00
|
|
|
* @throws JsonException
|
2019-08-26 07:12:46 +02:00
|
|
|
*/
|
|
|
|
private function migrateEntry(RecurrenceMeta $meta): int
|
|
|
|
{
|
2023-01-03 06:48:53 +01:00
|
|
|
/** @var Recurrence|null $recurrence */
|
2020-03-17 14:54:25 +01:00
|
|
|
$recurrence = $meta->recurrence;
|
2019-09-05 17:46:18 +02:00
|
|
|
if (null === $recurrence) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-26 07:12:46 +02:00
|
|
|
$firstTransaction = $recurrence->recurrenceTransactions()->first();
|
|
|
|
if (null === $firstTransaction) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-26 18:38:23 +02:00
|
|
|
$value = $meta->value;
|
|
|
|
|
|
|
|
if ('tags' === $meta->name) {
|
|
|
|
$array = explode(',', $meta->value);
|
2021-05-24 08:50:17 +02:00
|
|
|
$value = json_encode($array, JSON_THROW_ON_ERROR);
|
2019-08-26 18:38:23 +02:00
|
|
|
}
|
2019-08-26 07:12:46 +02:00
|
|
|
|
|
|
|
RecurrenceTransactionMeta::create(
|
|
|
|
[
|
|
|
|
'rt_id' => $firstTransaction->id,
|
|
|
|
'name' => $meta->name,
|
2019-08-26 18:38:23 +02:00
|
|
|
'value' => $value,
|
2019-08-26 07:12:46 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
$meta->forceDelete();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-21 12:34:58 +02:00
|
|
|
*
|
2019-08-26 07:12:46 +02:00
|
|
|
*/
|
2023-06-21 12:34:58 +02:00
|
|
|
private function markAsExecuted(): void
|
2019-08-26 07:12:46 +02:00
|
|
|
{
|
2023-06-21 12:34:58 +02:00
|
|
|
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
2019-08-26 07:12:46 +02:00
|
|
|
}
|
|
|
|
}
|