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(); $this->markAsExecuted(); 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 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; } } } private function markAsExecuted(): void { app('fireflyconfig')->set(self::CONFIG_NAME, true); } }