Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #4353: bigint_support_in_migrations.diff

File bigint_support_in_migrations.diff, 13.5 kB (added by shammond@northpub.com, 2 years ago)

Subversion diff

  • activerecord/test/connections/native_mysql/connection.rb

    old new  
    99 
    1010ActiveRecord::Base.establish_connection( 
    1111  :adapter  => "mysql", 
    12   :username => "rails", 
     12  :username => "root", 
     13  :password => "qns2fck", 
    1314  :encoding => "utf8", 
    1415  :database => db1 
    1516) 
    1617 
    1718Course.establish_connection( 
    1819  :adapter  => "mysql", 
    19   :username => "rails", 
     20  :username => "root", 
     21  :password => "qns2fck", 
    2022  :database => db2 
    2123) 
  • activerecord/test/migration_test.rb

    old new  
    4141      Person.connection.remove_column("people", "favorite_day") rescue nil 
    4242      Person.connection.remove_column("people", "male") rescue nil 
    4343      Person.connection.remove_column("people", "administrator") rescue nil 
     44      Person.connection.remove_column("people", "salary") rescue nil 
    4445      Person.reset_column_information 
    4546    end 
    4647 
     
    7172    ensure 
    7273      Person.connection.drop_table :testings rescue nil 
    7374    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   
    7594    def test_create_table_with_not_null_column 
    7695      Person.connection.create_table :testings do |t| 
    7796        t.column :foo, :string, :null => false 
     
    153172      Person.connection.add_column "people", "birthday", :datetime 
    154173      Person.connection.add_column "people", "favorite_day", :date 
    155174      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' } 
    157177      bob = Person.find(:first) 
    158178         
    159179      assert_equal bob.first_name, 'bob' 
     
    161181      assert_equal bob.bio, "I was born ...." 
    162182      assert_equal bob.age, 18 
    163183      assert_equal bob.male?, true 
    164      
     184      assert_equal bob.salary, 123456789123456789  
    165185      assert_equal String, bob.first_name.class 
    166186      assert_equal String, bob.last_name.class 
    167187      assert_equal String, bob.bio.class 
    168188      assert_equal Fixnum, bob.age.class 
    169189      assert_equal Time, bob.birthday.class 
     190      assert_equal Bignum, bob.salary.class 
    170191 
    171192      if current_adapter?(:SQLServerAdapter) || current_adapter?(:OracleAdapter) || current_adapter?(:SybaseAdapter) 
    172193        # SQL Server, Sybase, and Oracle don't differentiate between date/time 
     
    274295 
    275296    def test_change_column 
    276297      Person.connection.add_column 'people', 'age', :integer 
     298      Person.connection.add_column 'people', 'salary', :integer 
    277299      old_columns = Person.connection.columns(Person.table_name, "#{name} Columns") 
    278300      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 } 
    281302       
     303      assert_nothing_raised { Person.connection.change_column 'people', 'age', :string } 
     304      assert_nothing_raised { Person.connection.change_column 'people', 'salary', :biginteger } 
     305       
    282306      new_columns = Person.connection.columns(Person.table_name, "#{name} Columns") 
    283307      assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer } 
    284308      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 
    285318    end     
    286319 
    287320    def test_change_column_with_new_default 
  • activerecord/test/fixtures/db_definitions/sqlite.sql

    old new  
    198198  'id' INTEGER NOT NULL PRIMARY KEY, 
    199199  'tps_report_number' INTEGER DEFAULT NULL, 
    200200  'version' INTEGER NOT NULL DEFAULT 0 
    201 
     201); 
     202 
  • activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

    old new  
    4646      # [<tt>:id</tt>] 
    4747      #   Set to true or false to add/not add a primary key column 
    4848      #   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. 
    4953      # [<tt>:primary_key</tt>] 
    5054      #   The name of the primary key, if one is to be added automatically. 
    5155      #   Defaults to +id+. 
     
    8892      # 
    8993      # See also TableDefinition#column for details on how to create columns. 
    9094      def create_table(name, options = {}) 
     95        use_big_id = options[:use_big_id] || false 
    9196        table_definition = TableDefinition.new(self) 
    92         table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false 
     97        table_definition.primary_key(options[:primary_key] || "id", use_big_id) unless options[:id] == false 
    9398 
    9499        yield table_definition 
    95100 
  • activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    old new  
    5151      def type_cast(value) 
    5252        return nil if value.nil? 
    5353        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) 
    6465          else value 
    6566        end 
    6667      end 
    6768 
    6869      def type_cast_code(var_name) 
    6970        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})" 
    8082          else nil 
    8183        end 
    8284      end 
     
    137139 
    138140        def simplified_type(field_type) 
    139141          case field_type 
     142            when /bigint/i 
     143              :biginteger 
    140144            when /int/i 
    141145              :integer 
    142146            when /float|double|decimal|numeric/i 
     
    194198 
    195199      # Appends a primary key definition to the table definition. 
    196200      # 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 
    199207      end 
    200208 
    201209      # Returns a ColumnDefinition for the column with name +name+. 
     
    206214      # Instantiates a new column for the table. 
    207215      # The +type+ parameter must be one of the following values: 
    208216      # <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>, 
    211219      # <tt>:binary</tt>, <tt>:boolean</tt>. 
    212220      # 
    213221      # Available options are (none of these exists by default): 
  • activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb

    old new  
    106106      def native_database_types #:nodoc: 
    107107        { 
    108108          :primary_key => "INTEGER PRIMARY KEY NOT NULL", 
     109          :big_primary_key => "INTEGER PRIMARY KEY NOT NULL", 
    109110          :string      => { :name => "varchar", :limit => 255 }, 
    110111          :text        => { :name => "text" }, 
    111112          :integer     => { :name => "integer" }, 
     113          :biginteger  => { :name => "integer" }, 
    112114          :float       => { :name => "float" }, 
    113115          :datetime    => { :name => "datetime" }, 
    114116          :timestamp   => { :name => "datetime" }, 
     
    294296                :limit => column.limit, :default => column.default, 
    295297                :null => column.null) 
    296298            end 
    297             @definition.primary_key(primary_key(from)
     299            @definition.primary_key(primary_key(from), false
    298300            yield @definition if block_given? 
    299301          end 
    300302           
  • activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    old new  
    9898      def native_database_types #:nodoc 
    9999        { 
    100100          :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", 
     101          :big_primary_key => "bigint(20) DEFAULT NULL auto_increment PRIMARY KEY", 
    101102          :string      => { :name => "varchar", :limit => 255 }, 
    102103          :text        => { :name => "text" }, 
    103104          :integer     => { :name => "int", :limit => 11 }, 
     105          :biginteger  => { :name => "bigint", :limit => 20 }, 
    104106          :float       => { :name => "float" }, 
    105107          :datetime    => { :name => "datetime" }, 
    106108          :timestamp   => { :name => "datetime" },