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.



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/sequel/adapters/shared/postgres.rb', line 764

def reset_primary_key_sequence(table)
  return unless seq = primary_key_sequence(table)
  pk = SQL::Identifier.new(primary_key(table))
  db = self
  s, t = schema_and_table(table)
  table = Sequel.qualify(s, t) if s

  if server_version >= 100000
    seq_ds = .from(:pg_sequence).where(:seqrelid=>regclass_oid(LiteralString.new(seq)))
    increment_by = :seqincrement
    min_value = :seqmin
  # :nocov:
  else
    seq_ds = .from(LiteralString.new(seq))
    increment_by = :increment_by
    min_value = :min_value
  # :nocov:
  end

  get{setval(seq, db[table].select(coalesce(max(pk)+seq_ds.select(increment_by), seq_ds.select(min_value))), false)}
end