From a49e20c2aa9027513223d69b0dcc253a4583a730 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 26 Aug 2019 07:12:46 +0200 Subject: [PATCH] New command to migrate recurrence meta data. --- .../Upgrade/MigrateRecurrenceMeta.php | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 app/Console/Commands/Upgrade/MigrateRecurrenceMeta.php diff --git a/app/Console/Commands/Upgrade/MigrateRecurrenceMeta.php b/app/Console/Commands/Upgrade/MigrateRecurrenceMeta.php new file mode 100644 index 0000000000..99c4c70fb6 --- /dev/null +++ b/app/Console/Commands/Upgrade/MigrateRecurrenceMeta.php @@ -0,0 +1,141 @@ +. + */ + +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; + } + $this->warn('Congrats, you found the skeleton command. Boo!'); + + $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)); + + return 0; + } + + /** + * @return bool + */ + private function isExecuted(): bool + { + $configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false); + if (null !== $configVar) { + return (bool)$configVar->data; + } + + 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 + { + $recurrence = $meta->recurrence; + $firstTransaction = $recurrence->recurrenceTransactions()->first(); + if (null === $firstTransaction) { + return 0; + } + + RecurrenceTransactionMeta::create( + [ + 'rt_id' => $firstTransaction->id, + 'name' => $meta->name, + 'value' => $meta->value, + ] + ); + $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; + } +}