Skip to content

Commit

Permalink
Make Database#reset_primary_key_sequence work on PostgreSQL 10+
Browse files Browse the repository at this point in the history
Selecting directly from the sequence no longer provides the
increment and min value, so the new pg_sequence catalog table
needs to be used.
  • Loading branch information
jeremyevans committed Nov 1, 2017
1 parent f0af2f8 commit 8370980
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== master

* Make Database#reset_primary_key_sequence work on PostgreSQL 10+ (jeremyevans)

* Support :connect_sqls Database option for easily issuing sql commands on all new connections (jeremyevans)

* Support :extensions Database option for loading extensions when initializing, useful in connection strings (jeremyevans)
Expand Down
14 changes: 12 additions & 2 deletions lib/sequel/adapters/shared/postgres.rb
Expand Up @@ -506,10 +506,20 @@ 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)}

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

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

def rollback_prepared_transaction(transaction_id, opts=OPTS)
Expand Down

0 comments on commit 8370980

Please sign in to comment.