public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/timcharper/rails.git
Always treat integer :limit as byte length.  [#420 state:resolved]
tarmo (author)
Sat Jun 14 19:55:56 -0700 2008
jeremy (committer)
Sun Jun 22 20:42:31 -0700 2008
commit  baddea95e183b3bef290ec433f917ee8cd806934
tree    10bf700bcc147535ee694ce39cd4d4d110383b8d
parent  3610997ba32128921115bedb89c322a7bcbe161a
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *Edge*
0
 
0
+* Always treat integer :limit as byte length. #420 [Tarmo Tänav]
0
+
0
 * Partial updates don't update lock_version if nothing changed. #426 [Daniel Morrison]
0
 
0
 * Fix column collision with named_scope and :joins. #46 [Duncan Beevers, Mark Catley]
...
99
100
101
102
 
 
103
104
105
...
110
111
112
 
 
 
 
113
114
115
...
168
169
170
171
 
172
173
174
...
467
468
469
470
471
472
473
474
475
476
477
 
 
 
 
478
479
480
...
99
100
101
 
102
103
104
105
106
...
111
112
113
114
115
116
117
118
119
120
...
173
174
175
 
176
177
178
179
...
472
473
474
 
 
 
 
 
 
 
 
475
476
477
478
479
480
481
0
@@ -99,7 +99,8 @@ module ActiveRecord
0
         end
0
 
0
         def extract_limit(sql_type)
0
- if sql_type =~ /blob|text/i
0
+ case sql_type
0
+ when /blob|text/i
0
             case sql_type
0
             when /tiny/i
0
               255
0
@@ -110,6 +111,10 @@ module ActiveRecord
0
             else
0
               super # we could return 65535 here, but we leave it undecorated by default
0
             end
0
+ when /^int/i; 4
0
+ when /^bigint/i; 8
0
+ when /^smallint/i; 2
0
+ when /^mediumint/i; 3
0
           else
0
             super
0
           end
0
@@ -168,7 +173,7 @@ module ActiveRecord
0
         :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze,
0
         :string => { :name => "varchar", :limit => 255 },
0
         :text => { :name => "text" },
0
- :integer => { :name => "int"},
0
+ :integer => { :name => "int", :limit => 4 },
0
         :float => { :name => "float" },
0
         :decimal => { :name => "decimal" },
0
         :datetime => { :name => "datetime" },
0
@@ -467,14 +472,10 @@ module ActiveRecord
0
         return super unless type.to_s == 'integer'
0
 
0
         case limit
0
- when 0..3
0
- "smallint(#{limit})"
0
- when 4..8
0
- "int(#{limit})"
0
- when 9..20
0
- "bigint(#{limit})"
0
- else
0
- 'int(11)'
0
+ when 1..2; 'smallint'
0
+ when 3; 'mediumint'
0
+ when 4, nil; 'int(11)'
0
+ when 5..8; 'bigint'
0
         end
0
       end
0
 
...
47
48
49
 
 
 
 
 
 
50
51
52
...
785
786
787
788
789
790
791
792
793
 
 
 
 
794
795
796
...
47
48
49
50
51
52
53
54
55
56
57
58
...
791
792
793
 
 
 
 
 
 
794
795
796
797
798
799
800
0
@@ -47,6 +47,12 @@ module ActiveRecord
0
       end
0
 
0
       private
0
+ def extract_limit(sql_type)
0
+ return 8 if sql_type =~ /^bigint/i
0
+ return 2 if sql_type =~ /^smallint/i
0
+ super
0
+ end
0
+
0
         # Extracts the scale from PostgreSQL-specific data types.
0
         def extract_scale(sql_type)
0
           # Money type has a fixed scale of 2.
0
@@ -785,12 +791,10 @@ module ActiveRecord
0
       def type_to_sql(type, limit = nil, precision = nil, scale = nil)
0
         return super unless type.to_s == 'integer'
0
 
0
- if limit.nil? || limit == 4
0
- 'integer'
0
- elsif limit < 4
0
- 'smallint'
0
- else
0
- 'bigint'
0
+ case limit
0
+ when 1..2; 'smallint'
0
+ when 3..4, nil; 'integer'
0
+ when 5..8; 'bigint'
0
         end
0
       end
0
       
...
173
174
175
 
 
 
 
 
176
177
178
...
173
174
175
176
177
178
179
180
181
182
183
0
@@ -173,6 +173,11 @@ if ActiveRecord::Base.connection.supports_migrations?
0
         assert_equal 'smallint', one.sql_type
0
         assert_equal 'integer', four.sql_type
0
         assert_equal 'bigint', eight.sql_type
0
+ elsif current_adapter?(:MysqlAdapter)
0
+ assert_match /^int\(\d+\)/, default.sql_type
0
+ assert_match /^smallint\(\d+\)/, one.sql_type
0
+ assert_match /^int\(\d+\)/, four.sql_type
0
+ assert_match /^bigint\(\d+\)/, eight.sql_type
0
       elsif current_adapter?(:OracleAdapter)
0
         assert_equal 'NUMBER(38)', default.sql_type
0
         assert_equal 'NUMBER(1)', one.sql_type
...
72
73
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
76
77
...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
0
@@ -72,6 +72,52 @@ class SchemaDumperTest < ActiveRecord::TestCase
0
     assert_match %r{:null => false}, output
0
   end
0
 
0
+ def test_schema_dump_includes_limit_constraint_for_integer_columns
0
+ stream = StringIO.new
0
+
0
+ ActiveRecord::SchemaDumper.ignore_tables = [/^(?!integer_limits)/]
0
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
0
+ output = stream.string
0
+
0
+ if current_adapter?(:PostgreSQLAdapter)
0
+ assert_match %r{c_int_1.*:limit => 2}, output
0
+ assert_match %r{c_int_2.*:limit => 2}, output
0
+
0
+ # int 3 is 4 bytes in postgresql
0
+ assert_match %r{c_int_3.*}, output
0
+ assert_no_match %r{c_int_3.*:limit}, output
0
+
0
+ assert_match %r{c_int_4.*}, output
0
+ assert_no_match %r{c_int_4.*:limit}, output
0
+ elsif current_adapter?(:MysqlAdapter)
0
+ assert_match %r{c_int_1.*:limit => 2}, output
0
+ assert_match %r{c_int_2.*:limit => 2}, output
0
+ assert_match %r{c_int_3.*:limit => 3}, output
0
+
0
+ assert_match %r{c_int_4.*}, output
0
+ assert_no_match %r{c_int_4.*:limit}, output
0
+ elsif current_adapter?(:SQLiteAdapter)
0
+ assert_match %r{c_int_1.*:limit => 1}, output
0
+ assert_match %r{c_int_2.*:limit => 2}, output
0
+ assert_match %r{c_int_3.*:limit => 3}, output
0
+ assert_match %r{c_int_4.*:limit => 4}, output
0
+ end
0
+ assert_match %r{c_int_without_limit.*}, output
0
+ assert_no_match %r{c_int_without_limit.*:limit}, output
0
+
0
+ if current_adapter?(:SQLiteAdapter)
0
+ assert_match %r{c_int_5.*:limit => 5}, output
0
+ assert_match %r{c_int_6.*:limit => 6}, output
0
+ assert_match %r{c_int_7.*:limit => 7}, output
0
+ assert_match %r{c_int_8.*:limit => 8}, output
0
+ else
0
+ assert_match %r{c_int_5.*:limit => 8}, output
0
+ assert_match %r{c_int_6.*:limit => 8}, output
0
+ assert_match %r{c_int_7.*:limit => 8}, output
0
+ assert_match %r{c_int_8.*:limit => 8}, output
0
+ end
0
+ end
0
+
0
   def test_schema_dump_with_string_ignored_table
0
     stream = StringIO.new
0
 
...
407
408
409
 
 
 
 
 
 
 
410
411
412
...
407
408
409
410
411
412
413
414
415
416
417
418
419
0
@@ -407,6 +407,13 @@ ActiveRecord::Schema.define do
0
     t.column :key, :string
0
   end
0
 
0
+ create_table :integer_limits, :force => true do |t|
0
+ t.integer :"c_int_without_limit"
0
+ (1..8).each do |i|
0
+ t.integer :"c_int_#{i}", :limit => i
0
+ end
0
+ end
0
+
0
   except 'SQLite' do
0
     # fk_test_has_fk should be before fk_test_has_pk
0
     create_table :fk_test_has_fk, :force => true do |t|

Comments

    No one has commented yet.