Skip to content

Commit

Permalink
Added that the MySQL adapter should map integer to either smallint, i…
Browse files Browse the repository at this point in the history
…nt, or bigint depending on the :limit just like PostgreSQL [DHH]
  • Loading branch information
David Heinemeier Hansson committed Apr 25, 2008
1 parent 1959db3 commit a375465
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Added that the MySQL adapter should map integer to either smallint, int, or bigint depending on the :limit just like PostgreSQL [DHH]

* Change validates_uniqueness_of :case_sensitive option default back to true (from [9160]). Love your database columns, don't LOWER them. [rick]

* Add support for interleaving migrations by storing which migrations have run in the new schema_migrations table. Closes #11493 [jordi]
Expand Down
Expand Up @@ -463,6 +463,22 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
end

# 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 0..3
"smallint(#{limit})"
when 4..8
"int(#{limit})"
when 9..20
"bigint(#{limit})"
else
'int(11)'
end
end


# SHOW VARIABLES LIKE 'name'
def show_variable(name)
Expand Down

4 comments on commit a375465

@matthuhiggins
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would replace:
:integer => { :name => "int"}, :limit => 11 },

with:
:integer => { :name => "int"},

Otherwise, the default case of ‘int(11)’ is never executed.

@digitalhobbit
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with that change, this is causing problems when cloning the test database, as integer columns that were originally specified without an explicit limit in the migration now have a limit of 11, resulting in them being created as bigint(11) rather than int(11). This in turn breaks the clone when using foreign key constraints.

See [#55] for more details.

@tjoneseng
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch is not backwards compatible in general. The default for :integer was int(11), so the new range for “integer” should include 11 (see my meta-patch at http://timothynjones.wordpress.com/2008/06/10/change-to-activerecord-mysql-adapter-breaks-tests/).

@tarmo
Copy link
Contributor

@tarmo tarmo commented on a375465 Jun 16, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timothynjones, this code is actually going to chance once more with: http://rails.lighthouseapp.com/projects/8994/tickets/420-interpret-limit-as-number-of-bytes

And the :limit is no longer going to specify the number of decimal places, instead if will specify the number of bytes, so I’d suggest not at all adding a :limit option to your integers unless you need bigint or mediumint or smallint. For schema.rb there should be no compatibility problems as I believe schema.rb has never dumped :limit for :integer types (rather the change would actually make schema.rb more accurate in being able to create correct integer types where it used to always create 4 byte integers).

Please sign in to comment.