From 0eea776b8be6bdc78cb794e2043006233f208114 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 9 Apr 2023 20:29:35 +0200 Subject: [PATCH] Add decimal command. --- app/Console/Commands/ForceDecimalSize.php | 85 +++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 app/Console/Commands/ForceDecimalSize.php diff --git a/app/Console/Commands/ForceDecimalSize.php b/app/Console/Commands/ForceDecimalSize.php new file mode 100644 index 0000000000..07eae60f15 --- /dev/null +++ b/app/Console/Commands/ForceDecimalSize.php @@ -0,0 +1,85 @@ +verifyAccessToken()) { + $this->error('Invalid access token.'); + + return 1; + } + + $this->error('Running this command is dangerous and can cause data loss.'); + $this->error('Please do not continue.'); + $question = $this->confirm('Do you want to continue?'); + if (true === $question) { + $user = $this->getUser(); + Log::channel('audit')->info(sprintf('User #%d ("%s") forced migrations.', $user->id, $user->email)); + $this->updateDecimals(); + return 0; + } + $this->line('Done!'); + return 0; + } + + private function updateDecimals(): void + { + $this->info('Going to force the size of DECIMAL columns. Please hold.'); + $tables = [ + 'accounts' => ['virtual_balance'], + 'auto_budgets' => ['amount'], + 'available_budgets' => ['amount'], + 'bills' => ['amount_min', 'amount_max'], + 'budget_limits' => ['amount'], + 'currency_exchange_rates' => ['rate', 'user_rate'], + 'limit_repetitions' => ['amount'], + 'piggy_bank_events' => ['amount'], + 'piggy_bank_repetitions' => ['currentamount'], + 'piggy_banks' => ['targetamount'], + 'recurrences_transactions' => ['amount', 'foreign_amount'], + 'transactions' => ['amount', 'foreign_amount'], + ]; + /** + * @var string $name + * @var array $fields + */ + foreach($tables as $name => $fields) { + /** @var string $field */ + foreach($fields as $field) { + $this->line(sprintf('Updating table "%s", field "%s"...', $name, $field)); + $query = sprintf('ALTER TABLE %s CHANGE COLUMN %s %s DECIMAL(32, 12);', $name, $field, $field); + DB::select($query); + sleep(1); + } + } + } +}