Permalink
Browse files

On postgres, use bigserial type if :type=>Bignum is given as an optio…

…n to primary_key

This makes Sequel's shared postgres adapter act more similarly to the
other adapters (e.g. mysql), which allow you to specify :type=>Bignum
as a primary_key option to create an autoincrementing 64 bit integer
primary key field.

This also changes the schema dumper to specify the :type option to
primary key if it isn't Integer.
  • Loading branch information...
1 parent 5643d01 commit f917300a56f7cf592fbf0aaa5b2eb1b781550ab5 @jeremyevans committed Nov 16, 2009
View
@@ -1,5 +1,9 @@
=== HEAD
+* In the schema_dumper, explicitly specify the :type option if it isn't Integer (jeremyevans)
+
+* On postgres, use bigserial type if :type=>Bignum is given as an option to primary_key (jeremyevans)
+
* Use READ_DEFAULT_GROUP in the mysql adapter to load the options in the client section of the my.cnf file (crohr)
=== 3.6.0 (2009-11-02)
@@ -335,7 +335,7 @@ def reset_primary_key_sequence(table)
# PostgreSQL uses SERIAL psuedo-type instead of AUTOINCREMENT for
# managing incrementing primary keys.
def serial_primary_key_options
- {:primary_key => true, :type => :serial}
+ {:primary_key => true, :serial => true, :type=>Integer}
end
# The version of the PostgreSQL server, used for determining capability.
@@ -551,11 +551,21 @@ def sql_function_args(args)
"(#{Array(args).map{|a| Array(a).reverse.join(' ')}.join(', ')})"
end
+ # Handle bigserial type if :serial option is present
+ def type_literal_generic_bignum(column)
+ column[:serial] ? :bigserial : super
+ end
+
# PostgreSQL uses the bytea data type for blobs
def type_literal_generic_file(column)
:bytea
end
+ # Handle serial type if :serial option is present
+ def type_literal_generic_integer(column)
+ column[:serial] ? :serial : super
+ end
+
# PostgreSQL prefers the text datatype. If a fixed size is requested,
# the char type is used. If the text type is specifically
# disallowed or there is a size specified, use the varchar type.
@@ -86,7 +86,12 @@ def default.inspect
# name and arguments to it to pass to a Schema::Generator to recreate the column.
def column_schema_to_generator_opts(name, schema, options)
if options[:single_pk] && schema_autoincrementing_primary_key?(schema)
- [:primary_key, name]
+ type_hash = column_schema_to_ruby_type(schema)
+ if type_hash == {:type=>Integer}
+ [:primary_key, name]
+ else
+ [:primary_key, name, type_hash]
+ end
else
col_opts = options[:same_db] ? {:type=>schema[:db_type]} : column_schema_to_ruby_type(schema)
type = col_opts.delete(:type)
@@ -235,6 +235,18 @@ def logger.method_missing(m, msg)
@db[:posts].order(:a).map(:a).should == [1, 2, 10, 20, 21]
end
+ 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[:posts].insert.should == 1
+ @db[:posts].insert.should == 2
+ @db.create_table!(:posts){primary_key :a, :type=>Fixnum}
+ @db[:posts].insert.should == 1
+ @db[:posts].insert.should == 2
+ @db.create_table!(:posts){primary_key :a, :type=>Bignum}
+ @db[:posts].insert.should == 1
+ @db[:posts].insert.should == 2
+ end
+
specify "should not raise an error if attempting to resetting the primary key sequence for a table without a primary key" do
@db.create_table(:posts){Integer :a}
@db.reset_primary_key_sequence(:posts).should == nil
@@ -80,6 +80,8 @@
[:c2, {:db_type=>'datetime', :allow_null=>false}]]
when :t5
[[:c1, {:db_type=>'blahblah', :allow_null=>true}]]
+ when :t6
+ [[:c1, {:db_type=>'bigint', :primary_key=>true, :allow_null=>true}]]
end
end
end
@@ -88,6 +90,10 @@
@d.dump_table_schema(:t1).should == "create_table(:t1) do\n primary_key :c1\n String :c2, :size=>20\nend"
end
+ it "should dump non-Integer primary key columns with explicit :type" do
+ @d.dump_table_schema(:t6).should == "create_table(:t6) do\n primary_key :c1, :type=>Bignum\nend"
+ end
+
it "should use a composite primary_key calls if there is a composite primary key" do
@d.dump_table_schema(:t2).should == "create_table(:t2) do\n Integer :c1, :null=>false\n BigDecimal :c2, :null=>false\n \n primary_key [:c1, :c2]\nend"
end

0 comments on commit f917300

Please sign in to comment.