Skip to content

Commit

Permalink
Support default_schema when reseting primary key sequences on Postgre…
Browse files Browse the repository at this point in the history
…SQL (Fixes #596)
  • Loading branch information
jeremyevans committed Jan 7, 2013
1 parent b31e8f6 commit 52ad6b0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD === HEAD


* Support default_schema when reseting primary key sequences on PostgreSQL (jeremyevans) (#596)

* Allow treating tinyint(1) unsigned columns as booleans in the mysql adapters (jeremyevans) * Allow treating tinyint(1) unsigned columns as booleans in the mysql adapters (jeremyevans)


* Support the jdbc-hsqldb gem in the jdbc adapter, since it has been updated to 2.2.9 (jeremyevans) * Support the jdbc-hsqldb gem in the jdbc adapter, since it has been updated to 2.2.9 (jeremyevans)
Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/adapters/shared/postgres.rb
Expand Up @@ -429,6 +429,8 @@ def reset_primary_key_sequence(table)
pk = SQL::Identifier.new(primary_key(table)) pk = SQL::Identifier.new(primary_key(table))
db = self db = self
seq_ds = db.from(LiteralString.new(seq)) 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)} get{setval(seq, db[table].select{coalesce(max(pk)+seq_ds.select{:increment_by}, seq_ds.select(:min_value))}, false)}
end end


Expand Down
19 changes: 19 additions & 0 deletions spec/adapters/postgres_spec.rb
Expand Up @@ -539,6 +539,25 @@ def logger.method_missing(m, msg)
@db[:posts].insert.should == 21 @db[:posts].insert.should == 21
@db[:posts].order(:a).map(:a).should == [1, 2, 10, 20, 21] @db[:posts].order(:a).map(:a).should == [1, 2, 10, 20, 21]
end end

specify "should support resetting the primary key sequence with default_schema" do
begin
@db.run("DROP SCHEMA p") rescue nil
@db.run("CREATE SCHEMA p")
@db.default_schema = :p
@db.create_table(:posts){primary_key :a}
@db[:p__posts].insert(:a=>20).should == 20
@db[:p__posts].insert.should == 1
@db[:p__posts].insert.should == 2
@db[:p__posts].insert(:a=>10).should == 10
@db.reset_primary_key_sequence(:posts).should == 21
@db[:p__posts].insert.should == 21
@db[:p__posts].order(:a).map(:a).should == [1, 2, 10, 20, 21]
ensure
@db.default_schema = nil
@db.run("DROP SCHEMA p CASCADE")
end
end


specify "should support specifying Integer/Bignum/Fixnum types in primary keys and have them be auto incrementing" do specify "should support specifying Integer/Bignum/Fixnum types in primary keys and have them be auto incrementing" do
@db.create_table(:posts){primary_key :a, :type=>Integer} @db.create_table(:posts){primary_key :a, :type=>Integer}
Expand Down

0 comments on commit 52ad6b0

Please sign in to comment.