Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .ai/guidelines/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tests
stubs
app-modules/**/tests
.phpunit.cache
.ai

# Ide
.editorconfig
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Laravelcm\DatabaseMigration\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

final class ResetPostgresSequencesCommand extends Command
{
protected $signature = 'db:reset-sequences {--dry-run : Show what would be reset without executing}';

protected $description = 'Reset PostgreSQL sequences to match current MAX(id) values';

public function handle(): int
{
$isDryRun = $this->option('dry-run');

if ($isDryRun) {
$this->warn('🔍 DRY RUN MODE - No sequences will be actually reset');
}

$this->info('🔄 Resetting PostgreSQL sequences...');
$this->newLine();

$tables = collect(Schema::getTableListing())
->filter(fn (string $table): bool => Schema::hasColumn($table, 'id'))
->filter(fn (string $table): bool => $this->hasSequence($table));

if ($tables->isEmpty()) {
$this->warn('❌ No tables with sequences found');

return Command::SUCCESS;
}

$progressBar = $this->output->createProgressBar($tables->count());
$progressBar->start();

$resetCount = 0;
$skipCount = 0;

foreach ($tables as $table) {
try {
$maxId = DB::table($table)->max('id') ?? 0;

if ($isDryRun) {
$this->line("Would reset {$table}_id_seq to {$maxId}");
} else {
DB::statement("SELECT setval('{$table}_id_seq', COALESCE(MAX(id), 1)) FROM {$table};");
}

$resetCount++;
} catch (\Exception $e) {
$this->line("⚠️ Skipped {$table}: {$e->getMessage()}");
$skipCount++;
}

$progressBar->advance();
}

$progressBar->finish();
$this->newLine(2);

if ($isDryRun) {
$this->info("✅ Dry run completed - {$resetCount} sequences would be reset, {$skipCount} skipped");
} else {
$this->info("✅ Sequences reset completed - {$resetCount} reset, {$skipCount} skipped");
}

return Command::SUCCESS;
}

private function hasSequence(string $table): bool
{
try {
$result = DB::select("SELECT 1 FROM pg_class WHERE relname = '{$table}_id_seq' AND relkind = 'S'");

return ! empty($result);
} catch (\Exception) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\ServiceProvider;
use Laravelcm\DatabaseMigration\Commands\MigrateDatabaseCommand;
use Laravelcm\DatabaseMigration\Commands\MigrateFilesToS3Command;
use Laravelcm\DatabaseMigration\Commands\ResetPostgresSequencesCommand;
use Laravelcm\DatabaseMigration\Commands\SshTunnelCommand;
use Laravelcm\DatabaseMigration\Services\DatabaseMigrationService;
use Laravelcm\DatabaseMigration\Services\SshTunnelService;
Expand All @@ -31,6 +32,7 @@ public function boot(): void
SshTunnelCommand::class,
MigrateDatabaseCommand::class,
MigrateFilesToS3Command::class,
ResetPostgresSequencesCommand::class,
]);

$this->publishes([
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property-read int $id
* @property-read string $id
* @property array<array-key, mixed>|null $metadata
*/
final class Transaction extends Model
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"codeat3/blade-phosphor-icons": "^2.0",
"cyrildewit/eloquent-viewable": "^7.0",
"doctrine/dbal": "^4.2.1",
"dutchcodingcompany/livewire-recaptcha": "^1.0",
"filament/filament": "^3.3",
"filament/spatie-laravel-media-library-plugin": "^3.3",
"filament/spatie-laravel-translatable-plugin": "^3.3",
Expand Down
Loading
Loading