Skip to content
This repository
Browse code

PostgreSQL: migrations support :limit with :integer columns by mappin…

…g limit < 4 to smallint, > 4 to bigint, and anything else to integer. Closes #2900.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4335 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 491b4a3c84194931de51c3331b028dcfcb606074 1 parent c069f36
Jeremy Kemper authored
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* PostgreSQL: migrations support :limit with :integer columns by mapping limit < 4 to smallint, > 4 to bigint, and anything else to integer. #2900 [keegan@thebasement.org]
  4
+
3 5
 * Dates and times interpret empty strings as nil rather than 2000-01-01. #4830 [kajism@yahoo.com]
4 6
 
5 7
 * Allow :uniq => true with has_many :through associations. [Jeremy Kemper]
12  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -339,6 +339,18 @@ def remove_index(table_name, options) #:nodoc:
339 339
         execute "DROP INDEX #{index_name(table_name, options)}"
340 340
       end
341 341
 
  342
+      def type_to_sql(type, limit = nil) #:nodoc:
  343
+        return super unless type.to_s == 'integer'
  344
+
  345
+        if limit.nil? || limit == 4
  346
+          'integer'
  347
+        elsif limit < 4
  348
+          'smallint'
  349
+        else
  350
+          'bigint'
  351
+        end
  352
+      end
  353
+
342 354
       private
343 355
         BYTEA_COLUMN_TYPE_OID = 17
344 356
         TIMESTAMPOID = 1114
40  activerecord/test/migration_test.rb
@@ -118,6 +118,46 @@ def test_create_table_with_defaults
118 118
     ensure
119 119
       Person.connection.drop_table :testings rescue nil
120 120
     end
  121
+
  122
+    def test_create_table_with_limits
  123
+      Person.connection.create_table :testings do |t|
  124
+        t.column :foo, :string, :limit => 255
  125
+
  126
+        t.column :default_int, :integer
  127
+
  128
+        t.column :one_int,   :integer, :limit => 1
  129
+        t.column :four_int,  :integer, :limit => 4
  130
+        t.column :eight_int, :integer, :limit => 8
  131
+      end
  132
+
  133
+      columns = Person.connection.columns(:testings)
  134
+      foo = columns.detect { |c| c.name == "foo" }
  135
+      assert_equal 255, foo.limit
  136
+
  137
+      default = columns.detect { |c| c.name == "default_int" }
  138
+      one     = columns.detect { |c| c.name == "one_int"     }
  139
+      four    = columns.detect { |c| c.name == "four_int"    }
  140
+      eight   = columns.detect { |c| c.name == "eight_int"   }
  141
+
  142
+      if current_adapter?(:PostgreSQLAdapter)
  143
+        assert_equal 'integer', default.sql_type
  144
+        assert_equal 'smallint', one.sql_type
  145
+        assert_equal 'integer', four.sql_type
  146
+        assert_equal 'bigint', eight.sql_type
  147
+      elsif current_adapter?(:MysqlAdapter)
  148
+        assert_equal 'int(11)', default.sql_type
  149
+        assert_equal 'int(1)', one.sql_type
  150
+        assert_equal 'int(4)', four.sql_type
  151
+        assert_equal 'int(8)', eight.sql_type
  152
+      else
  153
+        assert_equal 'integer', default.sql_type
  154
+        assert_equal 'integer(1)', one.sql_type
  155
+        assert_equal 'integer(4)', four.sql_type
  156
+        assert_equal 'integer(8)', eight.sql_type
  157
+      end
  158
+    ensure
  159
+      Person.connection.drop_table :testings rescue nil
  160
+    end
121 161
   
122 162
     # SQL Server and Sybase will not allow you to add a NOT NULL column
123 163
     # to a table without specifying a default value, so the

0 notes on commit 491b4a3

Please sign in to comment.
Something went wrong with that request. Please try again.