Skip to content

Commit

Permalink
Fix primary_key :column, :type=>Bignum on SQLite and H2
Browse files Browse the repository at this point in the history
On SQLite, it was broken after Bignum changed to bigint
instead of integer.  It never worked on H2, as explicitly
setting a type overrote the identity feature, so it was
no longer autoincrementing.

Add an integration test to check support on other adapters.
  • Loading branch information
jeremyevans committed Mar 28, 2012
1 parent 32b0fbc commit c99dc23
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD

* primary_key :column, :type=>Bignum now works correctly on H2 (jeremyevans)

* Add query_literals extension for treating regular strings like literal strings in select, group, and order methods (jeremyevans)

* Actually use RETURNING for deletes/updates on PostgreSQL 8.2-9.0 (jeremyevans)
Expand Down Expand Up @@ -38,7 +40,7 @@

* Add many_to_one_pk_lookup plugin, for using a simple primary key lookup for many_to_one associations (great with caching) (jeremyevans)

* Use bigint type instead of integer for Bignum generic type on SQLite (jeremyevans)
* Use bigint type instead of integer for Bignum generic type on SQLite, except for auto incrementing primary keys (jeremyevans)

* Add Database#dump_foreign_key_migration for just dumping foreign key constraints to the schema dumper extension (jeremyevans)

Expand Down
8 changes: 7 additions & 1 deletion lib/sequel/adapters/jdbc/h2.rb
Expand Up @@ -25,7 +25,7 @@ def rollback_prepared_transaction(transaction_id)

# H2 uses an IDENTITY type
def serial_primary_key_options
{:primary_key => true, :type => :identity}
{:primary_key => true, :type => :identity, :identity=>true}
end

# H2 supports CREATE TABLE IF NOT EXISTS syntax.
Expand Down Expand Up @@ -114,6 +114,12 @@ def primary_key_index_re
def schema_column_type(db_type)
db_type == 'clob' ? :string : super
end

# Use BIGINT IDENTITY for identity columns that use bigint, fixes
# the case where primary_key :column, :type=>Bignum is used.
def type_literal_generic_bignum(column)
column[:identity] ? 'BIGINT IDENTITY' : super
end
end

# Dataset class for H2 datasets accessed via JDBC.
Expand Down
7 changes: 7 additions & 0 deletions lib/sequel/adapters/shared/sqlite.rb
Expand Up @@ -410,6 +410,13 @@ def tables_and_views(filter, opts)
m = output_identifier_meth
metadata_dataset.from(:sqlite_master).server(opts[:server]).filter(filter).map{|r| m.call(r[:name])}
end

# SQLite only supports AUTOINCREMENT on integer columns, not
# bigint columns, so use integer instead of bigint for those
# columns.
def type_literal_generic_bignum(column)
column[:auto_increment] ? :integer : super
end
end

# Instance methods for datasets that connect to an SQLite database
Expand Down
10 changes: 10 additions & 0 deletions spec/integration/dataset_test.rb
Expand Up @@ -23,6 +23,16 @@
{:id => 3, :number=>30} ]
end

specify "should support sequential primary keys with a Bignum" do
@db.create_table!(:items) do
primary_key :id, :type=>Bignum
Integer :number
end
@ds << {:number=>20}
@ds << {:number=>30}
@ds.order(:number).all.should == [{:id => 1, :number=>20}, {:id => 2, :number=>30}]
end

cspecify "should insert with a primary key specified", :db2, :mssql do
@ds.insert(:id=>100, :number=>20)
@ds.count.should == 2
Expand Down

0 comments on commit c99dc23

Please sign in to comment.