mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-16 01:06:46 +00:00
Add migration routine for https://github.com/firefly-iii/firefly-iii/pull/8650
This commit is contained in:
187
app/Console/Commands/Upgrade/MigrateRuleActions.php
Normal file
187
app/Console/Commands/Upgrade/MigrateRuleActions.php
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
/*
|
||||||
|
* MigrateRuleActions.php
|
||||||
|
* Copyright (c) 2024 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/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FireflyIII\Console\Commands\Upgrade;
|
||||||
|
|
||||||
|
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
|
||||||
|
use FireflyIII\Models\RuleAction;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class MigrateRuleActions extends Command
|
||||||
|
{
|
||||||
|
use ShowsFriendlyMessages;
|
||||||
|
|
||||||
|
public const string CONFIG_NAME = '610_migrate_rule_actions';
|
||||||
|
|
||||||
|
protected $description = 'Migrate rule actions away from expression engine';
|
||||||
|
|
||||||
|
protected $signature = 'firefly-iii:migrate-rule-actions {--F|force : Force the execution of this command.}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*/
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
||||||
|
$this->friendlyInfo('This command has already been executed.');
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (false === config('firefly.feature_flags.expression_engine')) {
|
||||||
|
$this->friendlyInfo('Expression engine is not enabled. Nothing to do.');
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
$this->replaceEqualSign();
|
||||||
|
$this->replaceObsoleteActions();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isExecuted(): bool
|
||||||
|
{
|
||||||
|
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
|
||||||
|
if (null !== $configVar) {
|
||||||
|
return (bool)$configVar->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function markAsExecuted(): void
|
||||||
|
{
|
||||||
|
app('fireflyconfig')->set(self::CONFIG_NAME, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function replaceEqualSign(): void
|
||||||
|
{
|
||||||
|
$count = 0;
|
||||||
|
$actions = RuleAction::get();
|
||||||
|
|
||||||
|
/** @var RuleAction $action */
|
||||||
|
foreach ($actions as $action) {
|
||||||
|
if (str_starts_with($action->action_value, '=')) {
|
||||||
|
$action->action_value = sprintf('%s%s', '\=', substr($action->action_value, 1));
|
||||||
|
$action->save();
|
||||||
|
++$count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($count > 0) {
|
||||||
|
$this->friendlyInfo(sprintf('Upgrading %d rule action(s) for the new expression engine.', $count));
|
||||||
|
}
|
||||||
|
if (0 === $count) {
|
||||||
|
$this->friendlyInfo('All rule actions are up to date.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function replaceObsoleteActions(): void
|
||||||
|
{
|
||||||
|
$obsolete = [
|
||||||
|
'append_description',
|
||||||
|
'prepend_description',
|
||||||
|
'append_notes',
|
||||||
|
'prepend_notes',
|
||||||
|
'append_descr_to_notes',
|
||||||
|
'append_notes_to_descr',
|
||||||
|
'move_descr_to_notes',
|
||||||
|
'move_notes_to_descr',
|
||||||
|
];
|
||||||
|
$actions = RuleAction::whereIn('action_type', $obsolete)->get();
|
||||||
|
|
||||||
|
/** @var RuleAction $action */
|
||||||
|
foreach ($actions as $action) {
|
||||||
|
$oldType = $action->action_type;
|
||||||
|
|
||||||
|
switch ($action->action_type) {
|
||||||
|
default:
|
||||||
|
$this->friendlyError(sprintf('Cannot deal with action type "%s", skip it.', $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'append_description':
|
||||||
|
$action->action_type = 'set_description';
|
||||||
|
$action->action_value = sprintf('=description~"%s"', str_replace('"', '\"', $action->action_value));
|
||||||
|
$action->save();
|
||||||
|
$this->friendlyInfo(sprintf('Upgraded action #%d from "%s" to "%s".', $action->id, $oldType, $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'prepend_description':
|
||||||
|
$action->action_type = 'set_description';
|
||||||
|
$action->action_value = sprintf('="%s"~description', str_replace('"', '\"', $action->action_value));
|
||||||
|
$action->save();
|
||||||
|
$this->friendlyInfo(sprintf('Upgraded action #%d from "%s" to "%s".', $action->id, $oldType, $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'append_notes':
|
||||||
|
$action->action_type = 'set_notes';
|
||||||
|
$action->action_value = sprintf('=notes~"%s"', str_replace('"', '\"', $action->action_value));
|
||||||
|
$action->save();
|
||||||
|
$this->friendlyInfo(sprintf('Upgraded action #%d from "%s" to "%s".', $action->id, $oldType, $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'prepend_notes':
|
||||||
|
$action->action_type = 'set_notes';
|
||||||
|
$action->action_value = sprintf('="%s"~notes', str_replace('"', '\"', $action->action_value));
|
||||||
|
$action->save();
|
||||||
|
$this->friendlyInfo(sprintf('Upgraded action #%d from "%s" to "%s".', $action->id, $oldType, $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'append_descr_to_notes':
|
||||||
|
$action->action_type = 'set_notes';
|
||||||
|
$action->action_value = '=notes~" "~description';
|
||||||
|
$action->save();
|
||||||
|
$this->friendlyInfo(sprintf('Upgraded action #%d from "%s" to "%s".', $action->id, $oldType, $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'append_notes_to_descr':
|
||||||
|
$action->action_type = 'set_description';
|
||||||
|
$action->action_value = '=description~" "~notes';
|
||||||
|
$action->save();
|
||||||
|
$this->friendlyInfo(sprintf('Upgraded action #%d from "%s" to "%s".', $action->id, $oldType, $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'move_descr_to_notes':
|
||||||
|
$action->action_type = 'set_notes';
|
||||||
|
$action->action_value = '=description';
|
||||||
|
$action->save();
|
||||||
|
$this->friendlyInfo(sprintf('Upgraded action #%d from "%s" to "%s".', $action->id, $oldType, $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'move_notes_to_descr':
|
||||||
|
$action->action_type = 'set_description';
|
||||||
|
$action->action_value = '=notes';
|
||||||
|
$action->save();
|
||||||
|
$this->friendlyInfo(sprintf('Upgraded action #%d from "%s" to "%s".', $action->id, $oldType, $action->action_type));
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -63,6 +63,7 @@ class UpgradeDatabase extends Command
|
|||||||
'firefly-iii:upgrade-liabilities',
|
'firefly-iii:upgrade-liabilities',
|
||||||
'firefly-iii:liabilities-600',
|
'firefly-iii:liabilities-600',
|
||||||
'firefly-iii:budget-limit-periods',
|
'firefly-iii:budget-limit-periods',
|
||||||
|
'firefly-iii:migrate-rule-actions',
|
||||||
'firefly-iii:restore-oauth-keys',
|
'firefly-iii:restore-oauth-keys',
|
||||||
// also just in case, some integrity commands:
|
// also just in case, some integrity commands:
|
||||||
'firefly-iii:create-group-memberships',
|
'firefly-iii:create-group-memberships',
|
||||||
|
@@ -26,11 +26,11 @@ class UpgradeSkeleton extends Command
|
|||||||
{
|
{
|
||||||
$start = microtime(true);
|
$start = microtime(true);
|
||||||
if ($this->isExecuted() && true !== $this->option('force')) {
|
if ($this->isExecuted() && true !== $this->option('force')) {
|
||||||
$this->info('FRIENDLY This command has already been executed.');
|
$this->friendlyInfo('This command has already been executed.');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
$this->warn('Congrats, you found the skeleton command. Boo!');
|
$this->friendlyWarning('Congrats, you found the skeleton command. Boo!');
|
||||||
|
|
||||||
//$this->markAsExecuted();
|
//$this->markAsExecuted();
|
||||||
|
|
||||||
|
@@ -85,6 +85,10 @@ class RuleAction extends Model
|
|||||||
|
|
||||||
return (string)$this->action_value;
|
return (string)$this->action_value;
|
||||||
}
|
}
|
||||||
|
if (true === config('firefly.feature_flags.expression_engine') && str_starts_with($this->action_value, '\=')) {
|
||||||
|
// return literal string.
|
||||||
|
return substr($this->action_value, 1);
|
||||||
|
}
|
||||||
$expr = new ActionExpression($this->action_value);
|
$expr = new ActionExpression($this->action_value);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@@ -53,6 +53,9 @@ class SetDescription implements ActionInterface
|
|||||||
$before = $object->description;
|
$before = $object->description;
|
||||||
$after = $this->action->getValue($journal);
|
$after = $this->action->getValue($journal);
|
||||||
|
|
||||||
|
// replace newlines.
|
||||||
|
$after = str_replace(["\r", "\n", "\t", "\036", "\025"], '', $after);
|
||||||
|
|
||||||
\DB::table('transaction_journals')
|
\DB::table('transaction_journals')
|
||||||
->where('id', '=', $journal['transaction_journal_id'])
|
->where('id', '=', $journal['transaction_journal_id'])
|
||||||
->update(['description' => $after])
|
->update(['description' => $after])
|
||||||
|
Reference in New Issue
Block a user