Files
firefly-iii/app/Console/Commands/Upgrade/FixPostgresSequences.php

82 lines
4.0 KiB
PHP
Raw Normal View History

2021-05-01 06:46:36 +02:00
<?php
2021-08-10 19:31:55 +02:00
/*
* FixPostgresSequences.php
2023-04-16 07:33:12 +02:00
* Copyright (c) 2023 james@firefly-iii.org
2021-08-10 19:31:55 +02:00
*
* 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/>.
*/
2021-05-01 09:47:21 +02:00
declare(strict_types=1);
2021-05-01 06:46:36 +02:00
2023-04-16 07:33:12 +02:00
namespace FireflyIII\Console\Commands\Upgrade;
2021-05-01 06:46:36 +02:00
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2021-05-01 06:46:36 +02:00
use Illuminate\Console\Command;
/**
* Class FixPostgresSequences
*/
class FixPostgresSequences extends Command
{
use ShowsFriendlyMessages;
2021-05-01 06:46:36 +02:00
protected $description = 'Fixes issues with PostgreSQL sequences.';
2023-11-05 09:54:53 +01:00
2021-05-01 06:46:36 +02:00
protected $signature = 'firefly-iii:fix-pgsql-sequences';
/**
* Execute the console command.
*/
public function handle(): int
{
2023-12-20 19:35:52 +01:00
if ('pgsql' !== \DB::connection()->getName()) {
2021-05-01 06:46:36 +02:00
return 0;
}
$this->friendlyLine('Going to verify PostgreSQL table sequences.');
2023-12-22 17:28:42 +01:00
$tablesToCheck = ['2fa_tokens', 'account_meta', 'account_types', 'accounts', 'attachments', 'auto_budgets', 'available_budgets', 'bills', 'budget_limits', 'budget_transaction', 'budget_transaction_journal', 'budgets', 'categories', 'category_transaction', 'category_transaction_journal', 'configuration', 'currency_exchange_rates', 'failed_jobs', 'group_journals', 'jobs', 'journal_links', 'journal_meta', 'limit_repetitions', 'link_types', 'locations', 'migrations', 'notes', 'oauth_clients', 'oauth_personal_access_clients', 'object_groups', 'permissions', 'piggy_bank_events', 'piggy_bank_repetitions', 'piggy_banks', 'preferences', 'recurrences', 'recurrences_meta', 'recurrences_repetitions', 'recurrences_transactions', 'roles', 'rt_meta', 'rule_actions', 'rule_groups', 'rule_triggers', 'rules', 'tag_transaction_journal', 'tags', 'transaction_currencies', 'transaction_groups', 'transaction_journals', 'transaction_types', 'transactions', 'users', 'webhook_attempts', 'webhook_messages', 'webhooks'];
2021-05-01 06:46:36 +02:00
foreach ($tablesToCheck as $tableToCheck) {
$this->friendlyLine(sprintf('Checking the next id sequence for table "%s".', $tableToCheck));
2021-05-01 06:46:36 +02:00
2023-12-20 19:35:52 +01:00
$highestId = \DB::table($tableToCheck)->select(\DB::raw('MAX(id)'))->first();
$nextId = \DB::table($tableToCheck)->select(\DB::raw(sprintf('nextval(\'%s_id_seq\')', $tableToCheck)))->first();
if (null === $nextId) {
$this->friendlyInfo(sprintf('nextval is NULL for table "%s", go to next table.', $tableToCheck));
2023-12-20 19:35:52 +01:00
2021-05-01 07:07:32 +02:00
continue;
}
2021-05-01 06:46:36 +02:00
2023-11-26 12:10:42 +01:00
if ($nextId->nextval < $highestId->max) { // @phpstan-ignore-line
2023-12-20 19:35:52 +01:00
\DB::select(sprintf('SELECT setval(\'%s_id_seq\', %d)', $tableToCheck, $highestId->max));
$highestId = \DB::table($tableToCheck)->select(\DB::raw('MAX(id)'))->first();
$nextId = \DB::table($tableToCheck)->select(\DB::raw(sprintf('nextval(\'%s_id_seq\')', $tableToCheck)))->first();
2023-11-26 12:10:42 +01:00
if ($nextId->nextval > $highestId->max) { // @phpstan-ignore-line
$this->friendlyInfo(sprintf('Table "%s" autoincrement corrected.', $tableToCheck));
2021-05-01 06:46:36 +02:00
}
if ($nextId->nextval <= $highestId->max) {
$this->friendlyWarning(sprintf('Arff! The nextval sequence is still all screwed up on table "%s".', $tableToCheck));
2021-05-01 06:46:36 +02:00
}
}
2021-05-01 07:14:43 +02:00
if ($nextId->nextval >= $highestId->max) {
$this->friendlyPositive(sprintf('Table "%s" autoincrement is correct.', $tableToCheck));
2021-05-01 07:14:43 +02:00
}
2021-05-01 06:46:36 +02:00
}
return 0;
}
}