Skip to content

Commit

Permalink
Merge pull request #157 from almontasser/fix/numeric-id-column-check
Browse files Browse the repository at this point in the history
Add numeric type check for 'id' column in PostgreSQL sequence fix
  • Loading branch information
tnylea authored Nov 14, 2024
2 parents 393935c + e17363c commit afc1a51
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ function fixPostgresSequence()
$tables = \DB::select('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\' ORDER BY table_name;');
foreach ($tables as $table) {
if (\Schema::hasColumn($table->table_name, 'id')) {
$seq = \DB::table($table->table_name)->max('id') + 1;
\DB::select('SELECT setval(pg_get_serial_sequence(\'' . $table->table_name . '\', \'id\'), coalesce(' . $seq . ',1), false) FROM ' . $table->table_name);
$columnType = \DB::select("SELECT data_type FROM information_schema.columns WHERE table_name = '{$table->table_name}' AND column_name = 'id'")[0]->data_type;
// Only proceed if the 'id' column is numeric
if (in_array($columnType, ['integer', 'bigint', 'smallint', 'smallserial', 'serial', 'bigserial'])) {
$seq = \DB::table($table->table_name)->max('id') + 1;
\DB::select('SELECT setval(pg_get_serial_sequence(\'' . $table->table_name . '\', \'id\'), coalesce(' . $seq . ',1), false) FROM ' . $table->table_name);
}
}
}
}
}
}

0 comments on commit afc1a51

Please sign in to comment.