mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Merge pull request #7495 from firefly-iii/expand-fix
Fix decimals command expanded.
This commit is contained in:
commit
342d72b0a6
@ -47,7 +47,7 @@ class CreateFirstUser extends Command
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'firefly-iii:create-first-user {email}';
|
protected $signature = 'firefly-iii:create-first-user {email}';
|
||||||
private UserRepositoryInterface $repository;
|
private UserRepositoryInterface $repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,7 +23,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Console\Commands\System;
|
namespace FireflyIII\Console\Commands\System;
|
||||||
|
|
||||||
use FireflyIII\Console\Commands\VerifiesAccessToken;
|
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AutoBudget;
|
use FireflyIII\Models\AutoBudget;
|
||||||
@ -38,6 +37,7 @@ use FireflyIII\Models\Transaction;
|
|||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
@ -51,14 +51,26 @@ use Illuminate\Support\Facades\Log;
|
|||||||
*/
|
*/
|
||||||
class ForceDecimalSize extends Command
|
class ForceDecimalSize extends Command
|
||||||
{
|
{
|
||||||
use VerifiesAccessToken;
|
|
||||||
|
|
||||||
protected $description = 'This command resizes DECIMAL columns in MySQL or PostgreSQL and correct amounts (only MySQL).';
|
protected $description = 'This command resizes DECIMAL columns in MySQL or PostgreSQL and correct amounts (only MySQL).';
|
||||||
protected $signature = 'firefly-iii:force-decimal-size
|
protected $signature = 'firefly-iii:force-decimal-size';
|
||||||
{--user=1 : The user ID.}
|
private string $cast;
|
||||||
{--token= : The user\'s access token.}';
|
private array $classes = [
|
||||||
private array $decimals = [];
|
'accounts' => Account::class,
|
||||||
private array $tables = [
|
'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;
|
||||||
|
private array $tables = [
|
||||||
'accounts' => ['virtual_balance'],
|
'accounts' => ['virtual_balance'],
|
||||||
'auto_budgets' => ['amount'],
|
'auto_budgets' => ['amount'],
|
||||||
'available_budgets' => ['amount'],
|
'available_budgets' => ['amount'],
|
||||||
@ -81,18 +93,12 @@ class ForceDecimalSize extends Command
|
|||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
Log::debug('Now in ForceDecimalSize::handle()');
|
Log::debug('Now in ForceDecimalSize::handle()');
|
||||||
if (!$this->verifyAccessToken()) {
|
$this->determineDatabaseType();
|
||||||
$this->error('Invalid access token.');
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->error('Running this command is dangerous and can cause data loss.');
|
$this->error('Running this command is dangerous and can cause data loss.');
|
||||||
$this->error('Please do not continue.');
|
$this->error('Please do not continue.');
|
||||||
$question = $this->confirm('Do you want to continue?');
|
$question = $this->confirm('Do you want to continue?');
|
||||||
if (true === $question) {
|
if (true === $question) {
|
||||||
$user = $this->getUser();
|
|
||||||
Log::channel('audit')->info(sprintf('User #%d ("%s") forced DECIMAL size.', $user->id, $user->email));
|
|
||||||
$this->correctAmounts();
|
$this->correctAmounts();
|
||||||
$this->updateDecimals();
|
$this->updateDecimals();
|
||||||
}
|
}
|
||||||
@ -109,20 +115,23 @@ class ForceDecimalSize extends Command
|
|||||||
*/
|
*/
|
||||||
private function correctAccountAmounts(TransactionCurrency $currency, array $fields): void
|
private function correctAccountAmounts(TransactionCurrency $currency, array $fields): void
|
||||||
{
|
{
|
||||||
|
$operator = $this->operator;
|
||||||
|
$cast = $this->cast;
|
||||||
|
$regularExpression = $this->regularExpression;
|
||||||
|
|
||||||
/** @var Builder $query */
|
/** @var Builder $query */
|
||||||
$query = Account::leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
$query = Account::leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||||
->where('account_meta.name', 'currency_id')
|
->where('account_meta.name', 'currency_id')
|
||||||
->where('account_meta.data', json_encode((string)$currency->id));
|
->where('account_meta.data', json_encode((string)$currency->id));
|
||||||
$query->where(static function (Builder $q) use ($fields, $currency) {
|
$query->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
$q->orWhere(
|
$q->orWhere(
|
||||||
DB::raw(sprintf('CAST(accounts.%s AS CHAR)', $field)),
|
DB::raw(sprintf('CAST(accounts.%s AS %s)', $field, $cast)),
|
||||||
'REGEXP',
|
$operator,
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$result = $query->get(['accounts.*']);
|
$result = $query->get(['accounts.*']);
|
||||||
if (0 === $result->count()) {
|
if (0 === $result->count()) {
|
||||||
$this->line(sprintf('Correct: All accounts in %s', $currency->code));
|
$this->line(sprintf('Correct: All accounts in %s', $currency->code));
|
||||||
@ -151,53 +160,22 @@ class ForceDecimalSize extends Command
|
|||||||
*/
|
*/
|
||||||
private function correctAmounts(): void
|
private function correctAmounts(): void
|
||||||
{
|
{
|
||||||
if ('mysql' !== config('database.default')) {
|
// if sqlite, add function?
|
||||||
$this->line('Skip correcting amounts...');
|
if ('sqlite' === (string)config('database.default')) {
|
||||||
|
DB::connection()->getPdo()->sqliteCreateFunction('REGEXP', function ($pattern, $value) {
|
||||||
|
mb_regex_encoding('UTF-8');
|
||||||
|
$pattern = trim($pattern, '"');
|
||||||
|
return (false !== mb_ereg($pattern, (string) $value)) ? 1 : 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!in_array((string)config('database.default'), ['mysql', 'pgsql', 'sqlite'])) {
|
||||||
|
$this->line(sprintf('Skip correcting amounts, does not support "%s"...', (string)config('database.default')));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->correctAmountsByCurrency();
|
$this->correctAmountsByCurrency();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method fixes all auto budgets in currency $currency.
|
|
||||||
* @param TransactionCurrency $currency
|
|
||||||
* @param array $fields
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function correctAutoBudgetAmounts(TransactionCurrency $currency, array $fields): void
|
|
||||||
{
|
|
||||||
/** @var Builder $query */
|
|
||||||
$query = AutoBudget::where('transaction_currency_id', $currency->id)->where(static function (Builder $q) use ($fields, $currency) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$q->orWhere(
|
|
||||||
DB::raw(sprintf('CAST(%s AS CHAR)', $field)),
|
|
||||||
'REGEXP',
|
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$result = $query->get(['*']);
|
|
||||||
if (0 === $result->count()) {
|
|
||||||
$this->line(sprintf('Correct: All auto budgets in %s', $currency->code));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/** @var AutoBudget $item */
|
|
||||||
foreach ($result as $item) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$value = $item->$field;
|
|
||||||
if (null === $value) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// fix $field by rounding it down correctly.
|
|
||||||
$pow = pow(10, (int)$currency->decimal_places);
|
|
||||||
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
|
|
||||||
$this->line(sprintf('Auto budget #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
|
|
||||||
AutoBudget::find($item->id)->update([$field => $correct]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method loops all enabled currencies and then calls the method that will fix all objects in this currency.
|
* This method loops all enabled currencies and then calls the method that will fix all objects in this currency.
|
||||||
*
|
*
|
||||||
@ -205,7 +183,7 @@ class ForceDecimalSize extends Command
|
|||||||
*/
|
*/
|
||||||
private function correctAmountsByCurrency(): void
|
private function correctAmountsByCurrency(): void
|
||||||
{
|
{
|
||||||
$this->line('Going to correct amounts, using the SLOW lane.');
|
$this->line('Going to correct amounts.');
|
||||||
/** @var Collection $enabled */
|
/** @var Collection $enabled */
|
||||||
$enabled = TransactionCurrency::whereEnabled(1)->get();
|
$enabled = TransactionCurrency::whereEnabled(1)->get();
|
||||||
/** @var TransactionCurrency $currency */
|
/** @var TransactionCurrency $currency */
|
||||||
@ -214,129 +192,6 @@ class ForceDecimalSize extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method fixes all available budgets in currency $currency.
|
|
||||||
*
|
|
||||||
* @param TransactionCurrency $currency
|
|
||||||
* @param array $fields
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function correctAvailableBudgetAmounts(TransactionCurrency $currency, array $fields): void
|
|
||||||
{
|
|
||||||
/** @var Builder $query */
|
|
||||||
$query = AvailableBudget::where('transaction_currency_id', $currency->id)->where(static function (Builder $q) use ($fields, $currency) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$q->orWhere(
|
|
||||||
DB::raw(sprintf('CAST(%s AS CHAR)', $field)),
|
|
||||||
'REGEXP',
|
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$result = $query->get(['*']);
|
|
||||||
if (0 === $result->count()) {
|
|
||||||
$this->line(sprintf('Correct: All available budgets in %s', $currency->code));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/** @var AvailableBudget $item */
|
|
||||||
foreach ($result as $item) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$value = $item->$field;
|
|
||||||
if (null === $value) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// fix $field by rounding it down correctly.
|
|
||||||
$pow = pow(10, (int)$currency->decimal_places);
|
|
||||||
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
|
|
||||||
$this->line(sprintf('Available budget #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
|
|
||||||
AvailableBudget::find($item->id)->update([$field => $correct]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method fixes all bills in currency $currency.
|
|
||||||
*
|
|
||||||
* @param TransactionCurrency $currency
|
|
||||||
* @param array $fields
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function correctBillAmounts(TransactionCurrency $currency, array $fields): void
|
|
||||||
{
|
|
||||||
/** @var Builder $query */
|
|
||||||
$query = Bill::where('transaction_currency_id', $currency->id)->where(static function (Builder $q) use ($fields, $currency) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$q->orWhere(
|
|
||||||
DB::raw(sprintf('CAST(%s AS CHAR)', $field)),
|
|
||||||
'REGEXP',
|
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$result = $query->get(['*']);
|
|
||||||
if (0 === $result->count()) {
|
|
||||||
$this->line(sprintf('Correct: All bills in %s', $currency->code));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/** @var Bill $item */
|
|
||||||
foreach ($result as $item) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$value = $item->$field;
|
|
||||||
if (null === $value) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// fix $field by rounding it down correctly.
|
|
||||||
$pow = pow(10, (int)$currency->decimal_places);
|
|
||||||
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
|
|
||||||
$this->line(sprintf('Bill #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
|
|
||||||
Bill::find($item->id)->update([$field => $correct]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method fixes all budget limits in currency $currency.
|
|
||||||
*
|
|
||||||
* @param TransactionCurrency $currency
|
|
||||||
* @param array $fields
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function correctBudgetLimitAmounts(TransactionCurrency $currency, array $fields)
|
|
||||||
{
|
|
||||||
/** @var Builder $query */
|
|
||||||
$query = BudgetLimit::where('transaction_currency_id', $currency->id)->where(static function (Builder $q) use ($fields, $currency) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$q->orWhere(
|
|
||||||
DB::raw(sprintf('CAST(%s AS CHAR)', $field)),
|
|
||||||
'REGEXP',
|
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$result = $query->get(['*']);
|
|
||||||
if (0 === $result->count()) {
|
|
||||||
$this->line(sprintf('Correct: All budget limits in %s', $currency->code));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/** @var BudgetLimit $item */
|
|
||||||
foreach ($result as $item) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$value = $item->$field;
|
|
||||||
if (null === $value) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// fix $field by rounding it down correctly.
|
|
||||||
$pow = pow(10, (int)$currency->decimal_places);
|
|
||||||
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
|
|
||||||
$this->line(sprintf('Budget limit #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
|
|
||||||
BudgetLimit::find($item->id)->update([$field => $correct]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method loops the available tables that may need fixing, and calls for the right method that can fix them.
|
* This method loops the available tables that may need fixing, and calls for the right method that can fix them.
|
||||||
*
|
*
|
||||||
@ -357,21 +212,15 @@ class ForceDecimalSize extends Command
|
|||||||
$message = sprintf('Cannot handle table "%s"', $name);
|
$message = sprintf('Cannot handle table "%s"', $name);
|
||||||
$this->line($message);
|
$this->line($message);
|
||||||
throw new FireflyException($message);
|
throw new FireflyException($message);
|
||||||
break;
|
|
||||||
case 'accounts':
|
case 'accounts':
|
||||||
$this->correctAccountAmounts($currency, $fields);
|
$this->correctAccountAmounts($currency, $fields);
|
||||||
break;
|
break;
|
||||||
case 'auto_budgets':
|
case 'auto_budgets':
|
||||||
$this->correctAutoBudgetAmounts($currency, $fields);
|
|
||||||
break;
|
|
||||||
case 'available_budgets':
|
case 'available_budgets':
|
||||||
$this->correctAvailableBudgetAmounts($currency, $fields);
|
|
||||||
break;
|
|
||||||
case 'bills':
|
case 'bills':
|
||||||
$this->correctBillAmounts($currency, $fields);
|
|
||||||
break;
|
|
||||||
case 'budget_limits':
|
case 'budget_limits':
|
||||||
$this->correctBudgetLimitAmounts($currency, $fields);
|
case 'recurrences_transactions':
|
||||||
|
$this->correctGeneric($currency, $name);
|
||||||
break;
|
break;
|
||||||
case 'currency_exchange_rates':
|
case 'currency_exchange_rates':
|
||||||
case 'limit_repetitions':
|
case 'limit_repetitions':
|
||||||
@ -386,9 +235,6 @@ class ForceDecimalSize extends Command
|
|||||||
case 'piggy_banks':
|
case 'piggy_banks':
|
||||||
$this->correctPiggyAmounts($currency, $fields);
|
$this->correctPiggyAmounts($currency, $fields);
|
||||||
break;
|
break;
|
||||||
case 'recurrences_transactions':
|
|
||||||
$this->correctRecurringTransactionAmounts($currency, $fields);
|
|
||||||
break;
|
|
||||||
case 'transactions':
|
case 'transactions':
|
||||||
$this->correctTransactionAmounts($currency);
|
$this->correctTransactionAmounts($currency);
|
||||||
break;
|
break;
|
||||||
@ -396,6 +242,54 @@ 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
|
||||||
|
{
|
||||||
|
$class = $this->classes[$table];
|
||||||
|
$fields = $this->tables[$table];
|
||||||
|
$operator = $this->operator;
|
||||||
|
$cast = $this->cast;
|
||||||
|
$regularExpression = $this->regularExpression;
|
||||||
|
|
||||||
|
/** @var Builder $query */
|
||||||
|
$query = $class::where('transaction_currency_id', $currency->id)->where(
|
||||||
|
static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
$q->orWhere(
|
||||||
|
DB::raw(sprintf('CAST(%s AS %s)', $field, $cast)),
|
||||||
|
$operator,
|
||||||
|
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$result = $query->get(['*']);
|
||||||
|
if (0 === $result->count()) {
|
||||||
|
$this->line(sprintf('Correct: All %s in %s', $table, $currency->code));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/** @var Model $item */
|
||||||
|
foreach ($result as $item) {
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
$value = $item->$field;
|
||||||
|
if (null === $value) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// fix $field by rounding it down correctly.
|
||||||
|
$pow = pow(10, (int)$currency->decimal_places);
|
||||||
|
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
|
||||||
|
$this->line(sprintf('%s #%d has %s with value "%s", this has been corrected to "%s".', $table, $item->id, $field, $value, $correct));
|
||||||
|
$class::find($item->id)->update([$field => $correct]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method fixes all piggy banks in currency $currency.
|
* This method fixes all piggy banks in currency $currency.
|
||||||
*
|
*
|
||||||
@ -403,19 +297,23 @@ class ForceDecimalSize extends Command
|
|||||||
* @param array $fields
|
* @param array $fields
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function correctPiggyAmounts(TransactionCurrency $currency, array $fields)
|
private function correctPiggyAmounts(TransactionCurrency $currency, array $fields): void
|
||||||
{
|
{
|
||||||
|
$operator = $this->operator;
|
||||||
|
$cast = $this->cast;
|
||||||
|
$regularExpression = $this->regularExpression;
|
||||||
|
|
||||||
/** @var Builder $query */
|
/** @var Builder $query */
|
||||||
$query = PiggyBank::leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
$query = PiggyBank::leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
||||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||||
->where('account_meta.name', 'currency_id')
|
->where('account_meta.name', 'currency_id')
|
||||||
->where('account_meta.data', json_encode((string)$currency->id))
|
->where('account_meta.data', json_encode((string)$currency->id))
|
||||||
->where(static function (Builder $q) use ($fields, $currency) {
|
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
$q->orWhere(
|
$q->orWhere(
|
||||||
DB::raw(sprintf('CAST(piggy_banks.%s AS CHAR)', $field)),
|
DB::raw(sprintf('CAST(piggy_banks.%s AS %s)', $field, $cast)),
|
||||||
'REGEXP',
|
$operator,
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -449,18 +347,22 @@ class ForceDecimalSize extends Command
|
|||||||
*/
|
*/
|
||||||
private function correctPiggyEventAmounts(TransactionCurrency $currency, array $fields): void
|
private function correctPiggyEventAmounts(TransactionCurrency $currency, array $fields): void
|
||||||
{
|
{
|
||||||
|
$operator = $this->operator;
|
||||||
|
$cast = $this->cast;
|
||||||
|
$regularExpression = $this->regularExpression;
|
||||||
|
|
||||||
/** @var Builder $query */
|
/** @var Builder $query */
|
||||||
$query = PiggyBankEvent::leftJoin('piggy_banks', 'piggy_bank_events.piggy_bank_id', '=', 'piggy_banks.id')
|
$query = PiggyBankEvent::leftJoin('piggy_banks', 'piggy_bank_events.piggy_bank_id', '=', 'piggy_banks.id')
|
||||||
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
|
||||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||||
->where('account_meta.name', 'currency_id')
|
->where('account_meta.name', 'currency_id')
|
||||||
->where('account_meta.data', json_encode((string)$currency->id))
|
->where('account_meta.data', json_encode((string)$currency->id))
|
||||||
->where(static function (Builder $q) use ($fields, $currency) {
|
->where(static function (Builder $q) use ($fields, $currency, $cast, $operator, $regularExpression) {
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
$q->orWhere(
|
$q->orWhere(
|
||||||
DB::raw(sprintf('CAST(piggy_bank_events.%s AS CHAR)', $field)),
|
DB::raw(sprintf('CAST(piggy_bank_events.%s AS %s)', $field, $cast)),
|
||||||
'REGEXP',
|
$operator,
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -495,6 +397,9 @@ class ForceDecimalSize extends Command
|
|||||||
*/
|
*/
|
||||||
private function correctPiggyRepetitionAmounts(TransactionCurrency $currency, array $fields)
|
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.
|
// select all piggy bank repetitions with this currency and issue.
|
||||||
/** @var Builder $query */
|
/** @var Builder $query */
|
||||||
$query = PiggyBankRepetition::leftJoin('piggy_banks', 'piggy_bank_repetitions.piggy_bank_id', '=', 'piggy_banks.id')
|
$query = PiggyBankRepetition::leftJoin('piggy_banks', 'piggy_bank_repetitions.piggy_bank_id', '=', 'piggy_banks.id')
|
||||||
@ -502,12 +407,12 @@ class ForceDecimalSize extends Command
|
|||||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||||
->where('account_meta.name', 'currency_id')
|
->where('account_meta.name', 'currency_id')
|
||||||
->where('account_meta.data', json_encode((string)$currency->id))
|
->where('account_meta.data', json_encode((string)$currency->id))
|
||||||
->where(static function (Builder $q) use ($fields, $currency) {
|
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
$q->orWhere(
|
$q->orWhere(
|
||||||
DB::raw(sprintf('CAST(piggy_bank_repetitions.%s AS CHAR)', $field)),
|
DB::raw(sprintf('CAST(piggy_bank_repetitions.%s AS %s)', $field, $cast)),
|
||||||
'REGEXP',
|
$operator,
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
DB::raw(sprintf($regularExpression, $currency->decimal_places))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -533,46 +438,6 @@ class ForceDecimalSize extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method fixes all recurring transactions in currency $currency.
|
|
||||||
* @param TransactionCurrency $currency
|
|
||||||
* @param array $fields
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function correctRecurringTransactionAmounts(TransactionCurrency $currency, array $fields): void
|
|
||||||
{
|
|
||||||
/** @var Builder $query */
|
|
||||||
$query = RecurrenceTransaction::where('transaction_currency_id', $currency->id)->where(static function (Builder $q) use ($fields, $currency) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$q->orWhere(
|
|
||||||
DB::raw(sprintf('CAST(%s AS CHAR)', $field)),
|
|
||||||
'REGEXP',
|
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$result = $query->get(['*']);
|
|
||||||
if (0 === $result->count()) {
|
|
||||||
$this->line(sprintf('Correct: All recurring transactions in %s', $currency->code));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/** @var RecurrenceTransaction $item */
|
|
||||||
foreach ($result as $item) {
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$value = $item->$field;
|
|
||||||
if (null === $value) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// fix $field by rounding it down correctly.
|
|
||||||
$pow = pow(10, (int)$currency->decimal_places);
|
|
||||||
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
|
|
||||||
$this->line(sprintf('Recurring transaction #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
|
|
||||||
RecurrenceTransaction::find($item->id)->update([$field => $correct]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method fixes all transactions in currency $currency.
|
* This method fixes all transactions in currency $currency.
|
||||||
*
|
*
|
||||||
@ -584,15 +449,16 @@ class ForceDecimalSize extends Command
|
|||||||
// select all transactions with this currency and issue.
|
// select all transactions with this currency and issue.
|
||||||
/** @var Builder $query */
|
/** @var Builder $query */
|
||||||
$query = Transaction::where('transaction_currency_id', $currency->id)->where(
|
$query = Transaction::where('transaction_currency_id', $currency->id)->where(
|
||||||
DB::raw('CAST(amount AS CHAR)'),
|
DB::raw(sprintf('CAST(amount as %s)', $this->cast)),
|
||||||
'REGEXP',
|
$this->operator,
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
DB::raw(sprintf($this->regularExpression, $currency->decimal_places))
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = $query->get(['*']);
|
$result = $query->get(['transactions.*']);
|
||||||
if (0 === $result->count()) {
|
if (0 === $result->count()) {
|
||||||
$this->line(sprintf('Correct: All transactions in %s', $currency->code));
|
$this->line(sprintf('Correct: All transactions in %s', $currency->code));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Transaction $item */
|
/** @var Transaction $item */
|
||||||
foreach ($result as $item) {
|
foreach ($result as $item) {
|
||||||
$value = $item->amount;
|
$value = $item->amount;
|
||||||
@ -609,9 +475,9 @@ class ForceDecimalSize extends Command
|
|||||||
// select all transactions with this FOREIGN currency and issue.
|
// select all transactions with this FOREIGN currency and issue.
|
||||||
/** @var Builder $query */
|
/** @var Builder $query */
|
||||||
$query = Transaction::where('foreign_currency_id', $currency->id)->where(
|
$query = Transaction::where('foreign_currency_id', $currency->id)->where(
|
||||||
DB::raw('CAST(foreign_amount AS CHAR)'),
|
DB::raw(sprintf('CAST(foreign_amount as %s)', $this->cast)),
|
||||||
'REGEXP',
|
$this->operator,
|
||||||
DB::raw(sprintf('\'\\\\.[\\\\d]{%d}[1-9]+\'', $currency->decimal_places))
|
DB::raw(sprintf($this->regularExpression, $currency->decimal_places))
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = $query->get(['*']);
|
$result = $query->get(['*']);
|
||||||
@ -633,12 +499,29 @@ class ForceDecimalSize extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function determineDatabaseType(): void
|
||||||
|
{
|
||||||
|
// switch stuff based on database connection:
|
||||||
|
$this->operator = 'REGEXP';
|
||||||
|
$this->regularExpression = '\'\\\\.[\\\\d]{%d}[1-9]+\'';
|
||||||
|
$this->cast = 'CHAR';
|
||||||
|
if ('pgsql' === config('database.default')) {
|
||||||
|
$this->operator = 'SIMILAR TO';
|
||||||
|
$this->regularExpression = '\'%%\.[\d]{%d}[1-9]+%%\'';
|
||||||
|
$this->cast = 'TEXT';
|
||||||
|
}
|
||||||
|
if ('sqlite' === config('database.default')) {
|
||||||
|
$this->regularExpression = '"\\.[\d]{%d}[1-9]+"';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function updateDecimals(): void
|
private function updateDecimals(): void
|
||||||
{
|
{
|
||||||
$this->info('Going to force the size of DECIMAL columns. Please hold.');
|
$this->info('Going to force the size of DECIMAL columns. Please hold.');
|
||||||
|
$type = (string)config('database.default');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string $name
|
* @var string $name
|
||||||
@ -648,7 +531,19 @@ class ForceDecimalSize extends Command
|
|||||||
/** @var string $field */
|
/** @var string $field */
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
$this->line(sprintf('Updating table "%s", field "%s"...', $name, $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);
|
|
||||||
|
switch ($type) {
|
||||||
|
default:
|
||||||
|
$this->error(sprintf('Cannot handle database type "%s".', $type));
|
||||||
|
return;
|
||||||
|
case 'pgsql':
|
||||||
|
$query = sprintf('ALTER TABLE %s ALTER COLUMN %s TYPE DECIMAL(32,12);', $name, $field);
|
||||||
|
break;
|
||||||
|
case 'mysql':
|
||||||
|
$query = sprintf('ALTER TABLE %s CHANGE COLUMN %s %s DECIMAL(32, 12);', $name, $field, $field);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
DB::select($query);
|
DB::select($query);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
@ -69,13 +69,25 @@ class ChangesForV540 extends Migration
|
|||||||
Log::error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
|
Log::error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(Schema::hasColumn('bills', 'end_date') && Schema::hasColumn('bills', 'extension_date')) {
|
// in two steps for sqlite
|
||||||
|
if(Schema::hasColumn('bills', 'end_date')) {
|
||||||
try {
|
try {
|
||||||
Schema::table(
|
Schema::table(
|
||||||
'bills',
|
'bills',
|
||||||
static function (Blueprint $table) {
|
static function (Blueprint $table) {
|
||||||
$table->dropColumn('end_date');
|
$table->dropColumn('end_date');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (QueryException|ColumnDoesNotExist $e) {
|
||||||
|
Log::error(sprintf('Could not execute query: %s', $e->getMessage()));
|
||||||
|
Log::error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(Schema::hasColumn('bills', 'extension_date')) {
|
||||||
|
try {
|
||||||
|
Schema::table(
|
||||||
|
'bills',
|
||||||
|
static function (Blueprint $table) {
|
||||||
$table->dropColumn('extension_date');
|
$table->dropColumn('extension_date');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -72,7 +72,9 @@ class ChangesForV550 extends Migration
|
|||||||
Schema::table(
|
Schema::table(
|
||||||
'budget_transaction_journal',
|
'budget_transaction_journal',
|
||||||
function (Blueprint $table) {
|
function (Blueprint $table) {
|
||||||
$table->dropForeign('budget_id_foreign');
|
if ('sqlite' !== config('database.default')) {
|
||||||
|
$table->dropForeign('budget_id_foreign');
|
||||||
|
}
|
||||||
$table->dropColumn('budget_limit_id');
|
$table->dropColumn('budget_limit_id');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -86,12 +88,25 @@ class ChangesForV550 extends Migration
|
|||||||
Schema::dropIfExists('failed_jobs');
|
Schema::dropIfExists('failed_jobs');
|
||||||
|
|
||||||
// drop fields from budget limits
|
// drop fields from budget limits
|
||||||
if(Schema::hasColumn('budget_limits', 'period') && Schema::hasColumn('budget_limits', 'generated')) {
|
// in two steps for sqlite
|
||||||
|
if(Schema::hasColumn('budget_limits', 'period')) {
|
||||||
try {
|
try {
|
||||||
Schema::table(
|
Schema::table(
|
||||||
'budget_limits',
|
'budget_limits',
|
||||||
static function (Blueprint $table) {
|
static function (Blueprint $table) {
|
||||||
$table->dropColumn('period');
|
$table->dropColumn('period');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (QueryException|ColumnDoesNotExist $e) {
|
||||||
|
Log::error(sprintf('Could not execute query: %s', $e->getMessage()));
|
||||||
|
Log::error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(Schema::hasColumn('budget_limits', 'generated')) {
|
||||||
|
try {
|
||||||
|
Schema::table(
|
||||||
|
'budget_limits',
|
||||||
|
static function (Blueprint $table) {
|
||||||
$table->dropColumn('generated');
|
$table->dropColumn('generated');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -45,7 +45,9 @@ class ChangesForV550b2 extends Migration
|
|||||||
Schema::table(
|
Schema::table(
|
||||||
'recurrences_transactions',
|
'recurrences_transactions',
|
||||||
function (Blueprint $table) {
|
function (Blueprint $table) {
|
||||||
$table->dropForeign('type_foreign');
|
if ('sqlite' !== config('database.default')) {
|
||||||
|
$table->dropForeign('type_foreign');
|
||||||
|
}
|
||||||
if (Schema::hasColumn('recurrences_transactions', 'transaction_type_id')) {
|
if (Schema::hasColumn('recurrences_transactions', 'transaction_type_id')) {
|
||||||
$table->dropColumn('transaction_type_id');
|
$table->dropColumn('transaction_type_id');
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,9 @@ class UserGroups extends Migration
|
|||||||
Schema::table(
|
Schema::table(
|
||||||
$tableName,
|
$tableName,
|
||||||
function (Blueprint $table) use ($tableName) {
|
function (Blueprint $table) use ($tableName) {
|
||||||
$table->dropForeign(sprintf('%s_to_ugi', $tableName));
|
if ('sqlite' !== config('database.default')) {
|
||||||
|
$table->dropForeign(sprintf('%s_to_ugi', $tableName));
|
||||||
|
}
|
||||||
if (Schema::hasColumn($tableName, 'user_group_id')) {
|
if (Schema::hasColumn($tableName, 'user_group_id')) {
|
||||||
$table->dropColumn('user_group_id');
|
$table->dropColumn('user_group_id');
|
||||||
}
|
}
|
||||||
@ -83,7 +85,9 @@ class UserGroups extends Migration
|
|||||||
Schema::table(
|
Schema::table(
|
||||||
'users',
|
'users',
|
||||||
function (Blueprint $table) {
|
function (Blueprint $table) {
|
||||||
$table->dropForeign('type_user_group_id');
|
if ('sqlite' !== config('database.default')) {
|
||||||
|
$table->dropForeign('type_user_group_id');
|
||||||
|
}
|
||||||
if (Schema::hasColumn('users', 'user_group_id')) {
|
if (Schema::hasColumn('users', 'user_group_id')) {
|
||||||
$table->dropColumn('user_group_id');
|
$table->dropColumn('user_group_id');
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,9 @@ return new class () extends Migration {
|
|||||||
Schema::table(
|
Schema::table(
|
||||||
'currency_exchange_rates',
|
'currency_exchange_rates',
|
||||||
function (Blueprint $table) {
|
function (Blueprint $table) {
|
||||||
$table->dropForeign('cer_to_ugi');
|
if ('sqlite' !== config('database.default')) {
|
||||||
|
$table->dropForeign('cer_to_ugi');
|
||||||
|
}
|
||||||
if (Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
|
if (Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
|
||||||
$table->dropColumn('user_group_id');
|
$table->dropColumn('user_group_id');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user