mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Code cleanup.
This commit is contained in:
@@ -58,17 +58,17 @@ class ForceDecimalSize extends Command
|
||||
private string $cast;
|
||||
private array $classes
|
||||
= [
|
||||
'accounts' => Account::class,
|
||||
'auto_budgets' => AutoBudget::class,
|
||||
'available_budgets' => AvailableBudget::class,
|
||||
'bills' => Bill::class,
|
||||
'budget_limits' => BudgetLimit::class,
|
||||
'piggy_bank_events' => PiggyBankEvent::class,
|
||||
'piggy_bank_repetitions' => PiggyBankRepetition::class,
|
||||
'piggy_banks' => PiggyBank::class,
|
||||
'recurrences_transactions' => RecurrenceTransaction::class,
|
||||
'transactions' => Transaction::class,
|
||||
];
|
||||
'accounts' => Account::class,
|
||||
'auto_budgets' => AutoBudget::class,
|
||||
'available_budgets' => AvailableBudget::class,
|
||||
'bills' => Bill::class,
|
||||
'budget_limits' => BudgetLimit::class,
|
||||
'piggy_bank_events' => PiggyBankEvent::class,
|
||||
'piggy_bank_repetitions' => PiggyBankRepetition::class,
|
||||
'piggy_banks' => PiggyBank::class,
|
||||
'recurrences_transactions' => RecurrenceTransaction::class,
|
||||
'transactions' => Transaction::class,
|
||||
];
|
||||
|
||||
private string $operator;
|
||||
private string $regularExpression;
|
||||
@@ -90,7 +90,6 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
@@ -108,9 +107,6 @@ class ForceDecimalSize extends Command
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function determineDatabaseType(): void
|
||||
{
|
||||
// switch stuff based on database connection:
|
||||
@@ -129,8 +125,6 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/**
|
||||
* This method checks if a basic check can be done or if it needs to be complicated.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function correctAmounts(): void
|
||||
{
|
||||
@@ -155,13 +149,13 @@ class ForceDecimalSize extends Command
|
||||
/**
|
||||
* This method loops all enabled currencies and then calls the method that will fix all objects in this currency.
|
||||
*
|
||||
* @return void
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function correctAmountsByCurrency(): void
|
||||
{
|
||||
/** @var Collection $enabled */
|
||||
$enabled = TransactionCurrency::whereEnabled(1)->get();
|
||||
|
||||
/** @var TransactionCurrency $currency */
|
||||
foreach ($enabled as $currency) {
|
||||
$this->correctByCurrency($currency);
|
||||
@@ -171,9 +165,6 @@ class ForceDecimalSize extends Command
|
||||
/**
|
||||
* This method loops the available tables that may need fixing, and calls for the right method that can fix them.
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return void
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function correctByCurrency(TransactionCurrency $currency): void
|
||||
@@ -187,32 +178,46 @@ class ForceDecimalSize extends Command
|
||||
default:
|
||||
$message = sprintf('Cannot handle table "%s"', $name);
|
||||
$this->friendlyError($message);
|
||||
|
||||
throw new FireflyException($message);
|
||||
|
||||
case 'accounts':
|
||||
$this->correctAccountAmounts($currency, $fields);
|
||||
|
||||
break;
|
||||
|
||||
case 'auto_budgets':
|
||||
case 'available_budgets':
|
||||
case 'bills':
|
||||
case 'budget_limits':
|
||||
case 'recurrences_transactions':
|
||||
$this->correctGeneric($currency, $name);
|
||||
|
||||
break;
|
||||
|
||||
case 'currency_exchange_rates':
|
||||
case 'limit_repetitions':
|
||||
// do nothing
|
||||
break;
|
||||
|
||||
case 'piggy_bank_events':
|
||||
$this->correctPiggyEventAmounts($currency, $fields);
|
||||
|
||||
break;
|
||||
|
||||
case 'piggy_bank_repetitions':
|
||||
$this->correctPiggyRepetitionAmounts($currency, $fields);
|
||||
|
||||
break;
|
||||
|
||||
case 'piggy_banks':
|
||||
$this->correctPiggyAmounts($currency, $fields);
|
||||
|
||||
break;
|
||||
|
||||
case 'transactions':
|
||||
$this->correctTransactionAmounts($currency);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -220,11 +225,6 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/**
|
||||
* This method loops over all accounts and validates the amounts.
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
* @param array $fields
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function correctAccountAmounts(TransactionCurrency $currency, array $fields): void
|
||||
{
|
||||
@@ -234,8 +234,9 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/** @var Builder $query */
|
||||
$query = Account::leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('account_meta.name', 'currency_id')
|
||||
->where('account_meta.data', json_encode((string)$currency->id));
|
||||
->where('account_meta.name', 'currency_id')
|
||||
->where('account_meta.data', json_encode((string)$currency->id))
|
||||
;
|
||||
$query->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||
foreach ($fields as $field) {
|
||||
$q->orWhere(
|
||||
@@ -251,11 +252,12 @@ class ForceDecimalSize extends Command
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var Account $account */
|
||||
foreach ($result as $account) {
|
||||
/** @var string $field */
|
||||
foreach ($fields as $field) {
|
||||
$value = $account->$field;
|
||||
$value = $account->{$field};
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
@@ -270,11 +272,6 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/**
|
||||
* This method fixes all auto budgets in currency $currency.
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
* @param string $table
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function correctGeneric(TransactionCurrency $currency, string $table): void
|
||||
{
|
||||
@@ -304,11 +301,12 @@ class ForceDecimalSize extends Command
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var Model $item */
|
||||
foreach ($result as $item) {
|
||||
/** @var string $field */
|
||||
foreach ($fields as $field) {
|
||||
$value = $item->$field;
|
||||
$value = $item->{$field};
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
@@ -323,11 +321,6 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/**
|
||||
* This method fixes all piggy bank events in currency $currency.
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
* @param array $fields
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function correctPiggyEventAmounts(TransactionCurrency $currency, array $fields): void
|
||||
{
|
||||
@@ -337,19 +330,20 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/** @var Builder $query */
|
||||
$query = PiggyBankEvent::leftJoin('piggy_banks', 'piggy_bank_events.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('account_meta.name', 'currency_id')
|
||||
->where('account_meta.data', json_encode((string)$currency->id))
|
||||
->where(static function (Builder $q) use ($fields, $currency, $cast, $operator, $regularExpression) {
|
||||
foreach ($fields as $field) {
|
||||
$q->orWhere(
|
||||
DB::raw(sprintf('CAST(piggy_bank_events.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
|
||||
$operator,
|
||||
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||
);
|
||||
}
|
||||
});
|
||||
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('account_meta.name', 'currency_id')
|
||||
->where('account_meta.data', json_encode((string)$currency->id))
|
||||
->where(static function (Builder $q) use ($fields, $currency, $cast, $operator, $regularExpression) {
|
||||
foreach ($fields as $field) {
|
||||
$q->orWhere(
|
||||
DB::raw(sprintf('CAST(piggy_bank_events.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
|
||||
$operator,
|
||||
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||
);
|
||||
}
|
||||
})
|
||||
;
|
||||
|
||||
$result = $query->get(['piggy_bank_events.*']);
|
||||
if (0 === $result->count()) {
|
||||
@@ -357,11 +351,12 @@ class ForceDecimalSize extends Command
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var PiggyBankEvent $item */
|
||||
foreach ($result as $item) {
|
||||
/** @var string $field */
|
||||
foreach ($fields as $field) {
|
||||
$value = $item->$field;
|
||||
$value = $item->{$field};
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
@@ -378,33 +373,30 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/**
|
||||
* This method fixes all piggy bank repetitions in currency $currency.
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
* @param array $fields
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function correctPiggyRepetitionAmounts(TransactionCurrency $currency, array $fields)
|
||||
{
|
||||
$operator = $this->operator;
|
||||
$cast = $this->cast;
|
||||
$regularExpression = $this->regularExpression;
|
||||
|
||||
// select all piggy bank repetitions with this currency and issue.
|
||||
/** @var Builder $query */
|
||||
$query = PiggyBankRepetition::leftJoin('piggy_banks', 'piggy_bank_repetitions.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('account_meta.name', 'currency_id')
|
||||
->where('account_meta.data', json_encode((string)$currency->id))
|
||||
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||
foreach ($fields as $field) {
|
||||
$q->orWhere(
|
||||
DB::raw(sprintf('CAST(piggy_bank_repetitions.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
|
||||
$operator,
|
||||
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||
);
|
||||
}
|
||||
});
|
||||
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('account_meta.name', 'currency_id')
|
||||
->where('account_meta.data', json_encode((string)$currency->id))
|
||||
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||
foreach ($fields as $field) {
|
||||
$q->orWhere(
|
||||
DB::raw(sprintf('CAST(piggy_bank_repetitions.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
|
||||
$operator,
|
||||
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||
);
|
||||
}
|
||||
})
|
||||
;
|
||||
|
||||
$result = $query->get(['piggy_bank_repetitions.*']);
|
||||
if (0 === $result->count()) {
|
||||
@@ -412,11 +404,12 @@ class ForceDecimalSize extends Command
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var PiggyBankRepetition $item */
|
||||
foreach ($result as $item) {
|
||||
/** @var string $field */
|
||||
foreach ($fields as $field) {
|
||||
$value = $item->$field;
|
||||
$value = $item->{$field};
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
@@ -433,11 +426,6 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/**
|
||||
* This method fixes all piggy banks in currency $currency.
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
* @param array $fields
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function correctPiggyAmounts(TransactionCurrency $currency, array $fields): void
|
||||
{
|
||||
@@ -447,18 +435,19 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/** @var Builder $query */
|
||||
$query = PiggyBank::leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('account_meta.name', 'currency_id')
|
||||
->where('account_meta.data', json_encode((string)$currency->id))
|
||||
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||
foreach ($fields as $field) {
|
||||
$q->orWhere(
|
||||
DB::raw(sprintf('CAST(piggy_banks.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
|
||||
$operator,
|
||||
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||
);
|
||||
}
|
||||
});
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->where('account_meta.name', 'currency_id')
|
||||
->where('account_meta.data', json_encode((string)$currency->id))
|
||||
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||
foreach ($fields as $field) {
|
||||
$q->orWhere(
|
||||
DB::raw(sprintf('CAST(piggy_banks.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
|
||||
$operator,
|
||||
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||
);
|
||||
}
|
||||
})
|
||||
;
|
||||
|
||||
$result = $query->get(['piggy_banks.*']);
|
||||
if (0 === $result->count()) {
|
||||
@@ -466,11 +455,12 @@ class ForceDecimalSize extends Command
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var PiggyBank $item */
|
||||
foreach ($result as $item) {
|
||||
/** @var string $field */
|
||||
foreach ($fields as $field) {
|
||||
$value = $item->$field;
|
||||
$value = $item->{$field};
|
||||
if (null === $value) {
|
||||
continue;
|
||||
}
|
||||
@@ -485,10 +475,6 @@ class ForceDecimalSize extends Command
|
||||
|
||||
/**
|
||||
* This method fixes all transactions in currency $currency.
|
||||
*
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function correctTransactionAmounts(TransactionCurrency $currency): void
|
||||
{
|
||||
@@ -532,6 +518,7 @@ class ForceDecimalSize extends Command
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var Transaction $item */
|
||||
foreach ($result as $item) {
|
||||
$value = $item->foreign_amount;
|
||||
@@ -548,9 +535,6 @@ class ForceDecimalSize extends Command
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function updateDecimals(): void
|
||||
{
|
||||
$this->friendlyInfo('Going to force the size of DECIMAL columns. Please hold.');
|
||||
@@ -567,15 +551,16 @@ class ForceDecimalSize extends Command
|
||||
if ('pgsql' === $type) {
|
||||
DB::select(sprintf('ALTER TABLE %s ALTER COLUMN %s TYPE DECIMAL(32,12);', $name, $field));
|
||||
sleep(1);
|
||||
|
||||
return;
|
||||
}
|
||||
if ('mysql' === $type) {
|
||||
DB::select(sprintf('ALTER TABLE %s CHANGE COLUMN %s %s DECIMAL(32, 12);', $name, $field, $field));
|
||||
sleep(1);
|
||||
|
||||
return;
|
||||
}
|
||||
$this->friendlyError(sprintf('Cannot handle database type "%s".', $type));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user