Method: Sequel::Postgres::DatabaseMethods#reset_primary_key_sequence

Defined in:
lib/sequel/adapters/shared/postgres.rb

#reset_primary_key_sequence(table) ⇒ Object

Reset the primary key sequence for the given table, basing it on the maximum current value of the table’s primary key.



431
432
433
434
435
436
437
438
439
# File 'lib/sequel/adapters/shared/postgres.rb', line 431

def reset_primary_key_sequence(table)
  return unless seq = primary_key_sequence(table)
  pk = SQL::Identifier.new(primary_key(table))
  db = self
  seq_ds = db.from(LiteralString.new(seq))
  s, t = schema_and_table(table)
  table = Sequel.qualify(s, t) if s
  get{setval(seq, db[table].select{coalesce(max(pk)+seq_ds.select{:increment_by}, seq_ds.select(:min_value))}, false)}
end