Ticket #4353: bigint_support_in_migrations.diff
| File bigint_support_in_migrations.diff, 13.5 kB (added by shammond@northpub.com, 2 years ago) |
|---|
-
activerecord/test/connections/native_mysql/connection.rb
old new 9 9 10 10 ActiveRecord::Base.establish_connection( 11 11 :adapter => "mysql", 12 :username => "rails", 12 :username => "root", 13 :password => "qns2fck", 13 14 :encoding => "utf8", 14 15 :database => db1 15 16 ) 16 17 17 18 Course.establish_connection( 18 19 :adapter => "mysql", 19 :username => "rails", 20 :username => "root", 21 :password => "qns2fck", 20 22 :database => db2 21 23 ) -
activerecord/test/migration_test.rb
old new 41 41 Person.connection.remove_column("people", "favorite_day") rescue nil 42 42 Person.connection.remove_column("people", "male") rescue nil 43 43 Person.connection.remove_column("people", "administrator") rescue nil 44 Person.connection.remove_column("people", "salary") rescue nil 44 45 Person.reset_column_information 45 46 end 46 47 … … 71 72 ensure 72 73 Person.connection.drop_table :testings rescue nil 73 74 end 74 75 76 def test_create_table_using_big_id 77 Person.connection.create_table :testings, :use_big_id => true do |t| 78 t.column :foo, :string 79 end 80 81 new_columns = Person.connection.columns(:testings) 82 assert new_columns.find { |c| c.name == 'foo' and c.type == :string } 83 84 # Only Mysql distinguishes between bigint and integer 85 if current_adapter?(:MysqlAdapter) 86 assert new_columns.find { |c| c.name == 'id' and c.type == :biginteger } 87 else 88 assert new_columns.find { |c| c.name == 'id' and c.type == :integer } 89 end 90 ensure 91 Person.connection.drop_table :testings rescue nil 92 end 93 75 94 def test_create_table_with_not_null_column 76 95 Person.connection.create_table :testings do |t| 77 96 t.column :foo, :string, :null => false … … 153 172 Person.connection.add_column "people", "birthday", :datetime 154 173 Person.connection.add_column "people", "favorite_day", :date 155 174 Person.connection.add_column "people", "male", :boolean 156 assert_nothing_raised { Person.create :first_name => 'bob', :last_name => 'bobsen', :bio => "I was born ....", :age => 18, :height => 1.78, :birthday => 18.years.ago, :favorite_day => 10.days.ago, :male => true } 175 Person.connection.add_column "people", "salary", :biginteger 176 assert_nothing_raised { Person.create :first_name => 'bob', :last_name => 'bobsen', :bio => "I was born ....", :age => 18, :height => 1.78, :birthday => 18.years.ago, :favorite_day => 10.days.ago, :male => true, :salary => '123456789123456789' } 157 177 bob = Person.find(:first) 158 178 159 179 assert_equal bob.first_name, 'bob' … … 161 181 assert_equal bob.bio, "I was born ...." 162 182 assert_equal bob.age, 18 163 183 assert_equal bob.male?, true 164 184 assert_equal bob.salary, 123456789123456789 165 185 assert_equal String, bob.first_name.class 166 186 assert_equal String, bob.last_name.class 167 187 assert_equal String, bob.bio.class 168 188 assert_equal Fixnum, bob.age.class 169 189 assert_equal Time, bob.birthday.class 190 assert_equal Bignum, bob.salary.class 170 191 171 192 if current_adapter?(:SQLServerAdapter) || current_adapter?(:OracleAdapter) || current_adapter?(:SybaseAdapter) 172 193 # SQL Server, Sybase, and Oracle don't differentiate between date/time … … 274 295 275 296 def test_change_column 276 297 Person.connection.add_column 'people', 'age', :integer 298 Person.connection.add_column 'people', 'salary', :integer 277 299 old_columns = Person.connection.columns(Person.table_name, "#{name} Columns") 278 300 assert old_columns.find { |c| c.name == 'age' and c.type == :integer } 279 280 assert_nothing_raised { Person.connection.change_column "people", "age", :string } 301 assert old_columns.find { |c| c.name == 'salary' and c.type == :integer } 281 302 303 assert_nothing_raised { Person.connection.change_column 'people', 'age', :string } 304 assert_nothing_raised { Person.connection.change_column 'people', 'salary', :biginteger } 305 282 306 new_columns = Person.connection.columns(Person.table_name, "#{name} Columns") 283 307 assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer } 284 308 assert new_columns.find { |c| c.name == 'age' and c.type == :string } 309 310 # MySQL is the only database that distinguishes between :integer and 311 # :biginteger. 312 if current_adapter?(:MysqlAdapter) 313 assert_nil new_columns.find { |c| c.name == 'salary' and c.type == :integer } 314 assert new_columns.find { |c| c.name == 'salary' and c.type == :biginteger } 315 else 316 assert new_columns.find { |c| c.name == 'salary' and c.type == :integer } 317 end 285 318 end 286 319 287 320 def test_change_column_with_new_default -
activerecord/test/fixtures/db_definitions/sqlite.sql
old new 198 198 'id' INTEGER NOT NULL PRIMARY KEY, 199 199 'tps_report_number' INTEGER DEFAULT NULL, 200 200 'version' INTEGER NOT NULL DEFAULT 0 201 ) 201 ); 202 -
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
old new 46 46 # [<tt>:id</tt>] 47 47 # Set to true or false to add/not add a primary key column 48 48 # automatically. Defaults to true. 49 # [<tt>:use_big_id</tt>] 50 # Set to true to use a bigint as the primary_key in the table. 51 # This only make a difference in MySQL, other adaptors should ignore 52 # this. Defaults to false, and is ignores if :id is false. 49 53 # [<tt>:primary_key</tt>] 50 54 # The name of the primary key, if one is to be added automatically. 51 55 # Defaults to +id+. … … 88 92 # 89 93 # See also TableDefinition#column for details on how to create columns. 90 94 def create_table(name, options = {}) 95 use_big_id = options[:use_big_id] || false 91 96 table_definition = TableDefinition.new(self) 92 table_definition.primary_key(options[:primary_key] || "id" ) unless options[:id] == false97 table_definition.primary_key(options[:primary_key] || "id", use_big_id) unless options[:id] == false 93 98 94 99 yield table_definition 95 100 -
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
old new 51 51 def type_cast(value) 52 52 return nil if value.nil? 53 53 case type 54 when :string then value 55 when :text then value 56 when :integer then value.to_i rescue value ? 1 : 0 57 when :float then value.to_f 58 when :datetime then self.class.string_to_time(value) 59 when :timestamp then self.class.string_to_time(value) 60 when :time then self.class.string_to_dummy_time(value) 61 when :date then self.class.string_to_date(value) 62 when :binary then self.class.binary_to_string(value) 63 when :boolean then self.class.value_to_boolean(value) 54 when :string then value 55 when :text then value 56 when :integer then value.to_i rescue value ? 1 : 0 57 when :biginteger then value.to_i rescue value ? 1 : 0 58 when :float then value.to_f 59 when :datetime then self.class.string_to_time(value) 60 when :timestamp then self.class.string_to_time(value) 61 when :time then self.class.string_to_dummy_time(value) 62 when :date then self.class.string_to_date(value) 63 when :binary then self.class.binary_to_string(value) 64 when :boolean then self.class.value_to_boolean(value) 64 65 else value 65 66 end 66 67 end 67 68 68 69 def type_cast_code(var_name) 69 70 case type 70 when :string then nil 71 when :text then nil 72 when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)" 73 when :float then "#{var_name}.to_f" 74 when :datetime then "#{self.class.name}.string_to_time(#{var_name})" 75 when :timestamp then "#{self.class.name}.string_to_time(#{var_name})" 76 when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})" 77 when :date then "#{self.class.name}.string_to_date(#{var_name})" 78 when :binary then "#{self.class.name}.binary_to_string(#{var_name})" 79 when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})" 71 when :string then nil 72 when :text then nil 73 when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)" 74 when :biginteger then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)" 75 when :float then "#{var_name}.to_f" 76 when :datetime then "#{self.class.name}.string_to_time(#{var_name})" 77 when :timestamp then "#{self.class.name}.string_to_time(#{var_name})" 78 when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})" 79 when :date then "#{self.class.name}.string_to_date(#{var_name})" 80 when :binary then "#{self.class.name}.binary_to_string(#{var_name})" 81 when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})" 80 82 else nil 81 83 end 82 84 end … … 137 139 138 140 def simplified_type(field_type) 139 141 case field_type 142 when /bigint/i 143 :biginteger 140 144 when /int/i 141 145 :integer 142 146 when /float|double|decimal|numeric/i … … 194 198 195 199 # Appends a primary key definition to the table definition. 196 200 # Can be called multiple times, but this is probably not a good idea. 197 def primary_key(name) 198 column(name, native[:primary_key]) 201 def primary_key(name, use_big_id) 202 if use_big_id 203 column(name, native[:big_primary_key]) 204 else 205 column(name, native[:primary_key]) 206 end 199 207 end 200 208 201 209 # Returns a ColumnDefinition for the column with name +name+. … … 206 214 # Instantiates a new column for the table. 207 215 # The +type+ parameter must be one of the following values: 208 216 # <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>, 209 # <tt>:integer</tt>, <tt>: float</tt>, <tt>:datetime</tt>,210 # <tt>: timestamp</tt>, <tt>:time</tt>, <tt>:date</tt>,217 # <tt>:integer</tt>, <tt>:biginteger</tt>, <tt>:float</tt>, 218 # <tt>:datetime</tt>, <tt>:timestamp</tt>, <tt>:time</tt>, <tt>:date</tt>, 211 219 # <tt>:binary</tt>, <tt>:boolean</tt>. 212 220 # 213 221 # Available options are (none of these exists by default): -
activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
old new 106 106 def native_database_types #:nodoc: 107 107 { 108 108 :primary_key => "INTEGER PRIMARY KEY NOT NULL", 109 :big_primary_key => "INTEGER PRIMARY KEY NOT NULL", 109 110 :string => { :name => "varchar", :limit => 255 }, 110 111 :text => { :name => "text" }, 111 112 :integer => { :name => "integer" }, 113 :biginteger => { :name => "integer" }, 112 114 :float => { :name => "float" }, 113 115 :datetime => { :name => "datetime" }, 114 116 :timestamp => { :name => "datetime" }, … … 294 296 :limit => column.limit, :default => column.default, 295 297 :null => column.null) 296 298 end 297 @definition.primary_key(primary_key(from) )299 @definition.primary_key(primary_key(from), false) 298 300 yield @definition if block_given? 299 301 end 300 302 -
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
old new 98 98 def native_database_types #:nodoc 99 99 { 100 100 :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", 101 :big_primary_key => "bigint(20) DEFAULT NULL auto_increment PRIMARY KEY", 101 102 :string => { :name => "varchar", :limit => 255 }, 102 103 :text => { :name => "text" }, 103 104 :integer => { :name => "int", :limit => 11 }, 105 :biginteger => { :name => "bigint", :limit => 20 }, 104 106 :float => { :name => "float" }, 105 107 :datetime => { :name => "datetime" }, 106 108 :timestamp => { :name => "datetime" },