Permalink
Browse files

Added that the MySQL adapter should map integer to either smallint, i…

…nt, or bigint depending on the :limit just like PostgreSQL [DHH]
  • Loading branch information...
David Heinemeier Hansson
David Heinemeier Hansson committed Apr 25, 2008
1 parent 1959db3 commit a37546517dad9f6d9a7de6e1dba4d960909d71e8
Showing with 18 additions and 0 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +16 −0 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -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]
@@ -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)

4 comments on commit a375465

@matthuhiggins

This comment has been minimized.

matthuhiggins replied Apr 27, 2008

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

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

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

@digitalhobbit

This comment has been minimized.

digitalhobbit replied May 21, 2008

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

This comment has been minimized.

tjoneseng replied Jun 16, 2008

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

This comment has been minimized.

Contributor

tarmo replied Jun 16, 2008

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.