2019-08-26 00:12:46 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MigrateRecurrenceMeta.php
|
2020-01-23 13:35:02 -06:00
|
|
|
* Copyright (c) 2020 james@firefly-iii.org
|
2019-08-26 00:12:46 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2019-08-26 00:12:46 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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 00:12:46 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2019-08-26 00:12:46 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2019-08-26 00:12:46 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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 00:12:46 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace FireflyIII\Console\Commands\Upgrade;
|
|
|
|
|
|
|
|
use FireflyIII\Models\RecurrenceMeta;
|
|
|
|
use FireflyIII\Models\RecurrenceTransactionMeta;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MigrateRecurrenceMeta
|
|
|
|
*/
|
|
|
|
class MigrateRecurrenceMeta extends Command
|
|
|
|
{
|
|
|
|
public const CONFIG_NAME = '481_migrate_recurrence_meta';
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Migrate recurrence meta data';
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'firefly-iii:migrate-recurrence-meta {--F|force : Force the execution of this command.}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
|
|
|
$start = microtime(true);
|
|
|
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
|
|
|
$this->warn('This command has already been executed.');
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$count = $this->migrateMetaData();
|
|
|
|
|
|
|
|
if (0 === $count) {
|
|
|
|
$this->line('No recurrence meta data migrated.');
|
|
|
|
}
|
|
|
|
if ($count > 0) {
|
|
|
|
$this->line(sprintf('Migrated %d meta data entries', $count));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->markAsExecuted();
|
|
|
|
|
|
|
|
$end = round(microtime(true) - $start, 2);
|
|
|
|
$this->info(sprintf('Migrated recurrence meta data in %s seconds.', $end));
|
2020-03-21 09:42:37 -05:00
|
|
|
// app('telemetry')->feature('executed-command', $this->signature);
|
2019-08-26 00:12:46 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isExecuted(): bool
|
|
|
|
{
|
|
|
|
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
|
|
|
|
if (null !== $configVar) {
|
2020-03-17 08:54:25 -05:00
|
|
|
return (bool) $configVar->data;
|
2019-08-26 00:12:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false; // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function markAsExecuted(): void
|
|
|
|
{
|
|
|
|
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param RecurrenceMeta $meta
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function migrateEntry(RecurrenceMeta $meta): int
|
|
|
|
{
|
2020-03-17 08:54:25 -05:00
|
|
|
$recurrence = $meta->recurrence;
|
2019-09-05 10:46:18 -05:00
|
|
|
if (null === $recurrence) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-26 00:12:46 -05:00
|
|
|
$firstTransaction = $recurrence->recurrenceTransactions()->first();
|
|
|
|
if (null === $firstTransaction) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-26 11:38:23 -05:00
|
|
|
$value = $meta->value;
|
|
|
|
|
|
|
|
if ('tags' === $meta->name) {
|
|
|
|
$array = explode(',', $meta->value);
|
2020-03-15 02:16:16 -05:00
|
|
|
$value = json_encode($array, JSON_THROW_ON_ERROR, 512);
|
2019-08-26 11:38:23 -05:00
|
|
|
}
|
2019-08-26 00:12:46 -05:00
|
|
|
|
|
|
|
RecurrenceTransactionMeta::create(
|
|
|
|
[
|
|
|
|
'rt_id' => $firstTransaction->id,
|
|
|
|
'name' => $meta->name,
|
2019-08-26 11:38:23 -05:00
|
|
|
'value' => $value,
|
2019-08-26 00:12:46 -05:00
|
|
|
]
|
|
|
|
);
|
|
|
|
$meta->forceDelete();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function migrateMetaData(): int
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|