Skip to content

Instantly share code, notes, and snippets.

@mpugach
Forked from icyleaf/ar_migrate.rb
Created October 26, 2015 09:51
Show Gist options
  • Save mpugach/ec0f835b64ffb765715f to your computer and use it in GitHub Desktop.
Save mpugach/ec0f835b64ffb765715f to your computer and use it in GitHub Desktop.

Revisions

  1. @icyleaf icyleaf revised this gist Feb 19, 2014. 1 changed file with 12 additions and 13 deletions.
    25 changes: 12 additions & 13 deletions ar_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,3 @@
    def type_to_sql(type, limit = nil, precision = nil, scale = nil)
    return super unless type.to_s == 'integer'

    case limit
    when 1; 'tinyint'
    when 2; 'smallint'
    when 3; 'mediumint'
    when nil, 4, 11; 'int(11)' # compatibility with MySQL default
    when 5..8; 'bigint'
    else raise(ActiveRecordError, "No integer type has byte size #{limit}")
    end
    end

    # activerecord-3.0.0/lib/active_record/connection_adapters/mysql_adapter.rb
    # Maps logical Rails types to MySQL-specific data types.
    def type_to_sql(type, limit = nil, precision = nil, scale = nil)
    @@ -24,4 +11,16 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
    when 5..8; 'bigint'
    else raise(ActiveRecordError, "No integer type has byte size #{limit}")
    end
    end

    # ActiveRecord Migrate
    create_table 'example' do |t|
    t.integer :int # int (4 bytes, max 2,147,483,647)
    t.integer :int1, :limit => 1 # tinyint (1 byte, -128 to 127)
    t.integer :int2, :limit => 2 # smallint (2 bytes, max 32,767)
    t.integer :int3, :limit => 3 # mediumint (3 bytes, max 8,388,607)
    t.integer :int4, :limit => 4 # int (4 bytes)
    t.integer :int5, :limit => 5 # bigint (8 bytes, max 9,223,372,036,854,775,807)
    t.integer :int8, :limit => 8 # bigint (8 bytes)
    t.integer :int11, :limit => 11 # int (4 bytes)
    end
  2. @icyleaf icyleaf created this gist Feb 19, 2014.
    27 changes: 27 additions & 0 deletions ar_migrate.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    def type_to_sql(type, limit = nil, precision = nil, scale = nil)
    return super unless type.to_s == 'integer'

    case limit
    when 1; 'tinyint'
    when 2; 'smallint'
    when 3; 'mediumint'
    when nil, 4, 11; 'int(11)' # compatibility with MySQL default
    when 5..8; 'bigint'
    else raise(ActiveRecordError, "No integer type has byte size #{limit}")
    end
    end

    # activerecord-3.0.0/lib/active_record/connection_adapters/mysql_adapter.rb
    # Maps logical Rails types to MySQL-specific data types.
    def type_to_sql(type, limit = nil, precision = nil, scale = nil)
    return super unless type.to_s == 'integer'

    case limit
    when 1; 'tinyint'
    when 2; 'smallint'
    when 3; 'mediumint'
    when nil, 4, 11; 'int(11)' # compatibility with MySQL default
    when 5..8; 'bigint'
    else raise(ActiveRecordError, "No integer type has byte size #{limit}")
    end
    end