2019-03-20 18:31:00 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2019-06-13 06:39:05 +02:00
|
|
|
* MigrateJournalNotes.php
|
2020-01-23 20:35:02 +01:00
|
|
|
* Copyright (c) 2020 james@firefly-iii.org
|
2019-03-20 18:31:00 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2019-03-20 18:31:00 +01: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-03-20 18:31:00 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2019-03-20 18:31:00 +01: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-03-20 18:31:00 +01: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-03-20 18:31:00 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Console\Commands\Upgrade;
|
|
|
|
|
2023-06-20 07:16:56 +02:00
|
|
|
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
|
2019-03-20 18:31:00 +01:00
|
|
|
use FireflyIII\Models\Note;
|
|
|
|
use FireflyIII\Models\TransactionJournalMeta;
|
|
|
|
use Illuminate\Console\Command;
|
2023-04-01 07:04:42 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-10-30 12:24:51 +01:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2019-03-20 18:31:00 +01:00
|
|
|
|
|
|
|
/**
|
2019-06-13 06:39:05 +02:00
|
|
|
* Class MigrateJournalNotes
|
2019-03-20 18:31:00 +01:00
|
|
|
*/
|
2019-06-13 06:39:05 +02:00
|
|
|
class MigrateJournalNotes extends Command
|
2019-03-20 18:31:00 +01:00
|
|
|
{
|
2023-06-20 07:16:56 +02:00
|
|
|
use ShowsFriendlyMessages;
|
|
|
|
|
2019-08-26 07:13:48 +02:00
|
|
|
public const CONFIG_NAME = '480_migrate_notes';
|
2019-03-20 18:31:00 +01:00
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-06-13 06:39:05 +02:00
|
|
|
protected $description = 'Migrate notes for transaction journals.';
|
2019-03-20 18:31:00 +01:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'firefly-iii:migrate-notes {--F|force : Force the execution of this command.}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return int
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
2019-03-20 18:31:00 +01:00
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
2019-03-23 18:58:06 +01:00
|
|
|
$start = microtime(true);
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2019-03-20 18:31:00 +01:00
|
|
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyInfo('This command has already been executed.');
|
2019-03-20 18:31:00 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2019-06-13 06:39:05 +02:00
|
|
|
$count = 0;
|
2021-09-18 10:26:12 +02:00
|
|
|
$set = TransactionJournalMeta::whereName('notes')->get();
|
2019-03-20 18:31:00 +01:00
|
|
|
/** @var TransactionJournalMeta $meta */
|
|
|
|
foreach ($set as $meta) {
|
|
|
|
$journal = $meta->transactionJournal;
|
|
|
|
$note = $journal->notes()->first();
|
|
|
|
if (null === $note) {
|
|
|
|
$note = new Note();
|
|
|
|
$note->noteable()->associate($journal);
|
|
|
|
}
|
|
|
|
|
|
|
|
$note->text = $meta->data;
|
|
|
|
$note->save();
|
|
|
|
Log::debug(sprintf('Migrated meta note #%d to Note #%d', $meta->id, $note->id));
|
2023-01-03 06:48:53 +01:00
|
|
|
$meta->delete();
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2019-06-13 06:39:05 +02:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 === $count) {
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyPositive('No notes to migrate.');
|
2019-03-20 18:31:00 +01:00
|
|
|
}
|
2019-06-13 06:39:05 +02:00
|
|
|
if (0 !== $count) {
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyInfo(sprintf('Migrated %d note(s).', $count));
|
2019-06-13 06:39:05 +02:00
|
|
|
}
|
|
|
|
|
2019-03-23 18:58:06 +01:00
|
|
|
$end = round(microtime(true) - $start, 2);
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyInfo(sprintf('Migrated notes in %s seconds.', $end));
|
2019-03-20 18:31:00 +01:00
|
|
|
$this->markAsExecuted();
|
2020-03-21 15:43:41 +01:00
|
|
|
|
2019-03-20 18:31:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
2022-10-30 12:24:51 +01:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
2019-03-20 18:31:00 +01: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-03-20 18:31:00 +01:00
|
|
|
}
|
|
|
|
|
2021-09-18 10:26:12 +02:00
|
|
|
return false;
|
2019-03-20 18:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function markAsExecuted(): void
|
|
|
|
{
|
|
|
|
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
|
|
|
}
|
2019-08-17 12:09:03 +02:00
|
|
|
}
|