diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-27 10:48:20 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-27 10:48:20 -0700 |
commit | 51adec35173b3becf39143fe59b5fa22b708245a (patch) | |
tree | 4e9d70b02db2c5cfb7ab886a8e33fc3ddd35681b | |
parent | a4c3d5ae1df9bacf9c36e9229184774ef1064942 (diff) | |
parent | 5d30e443908558713cff16cdad4b09e203c8fae8 (diff) | |
download | rails-51adec35173b3becf39143fe59b5fa22b708245a.tar.gz rails-51adec35173b3becf39143fe59b5fa22b708245a.tar.bz2 rails-51adec35173b3becf39143fe59b5fa22b708245a.zip |
Merge pull request #7960 from pwnall/mysql_varbinary
Use the MySQL varbinary type when appropriate in migrations
7 files changed, 71 insertions, 18 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index c99f09f478..815902a129 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Small binary fields use the `VARBINARY` MySQL type, instead of `TINYBLOB`. + + *Victor Costan* + * Decode URI encoded attributes on database connection URLs. *Shawn Veader* diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index b0e7bd7e82..2c57b52017 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -498,6 +498,13 @@ module ActiveRecord # Maps logical Rails types to MySQL-specific data types. def type_to_sql(type, limit = nil, precision = nil, scale = nil) case type.to_s + when 'binary' + case limit + when 0..0xfff; "varbinary(#{limit})" + when nil; "blob" + when 0x1000..0xffffffff; "blob(#{limit})" + else raise(ActiveRecordError, "No binary type has character length #{limit}") + end when 'integer' case limit when 1; 'tinyint' diff --git a/activerecord/test/cases/adapters/mysql/sql_types_test.rb b/activerecord/test/cases/adapters/mysql/sql_types_test.rb new file mode 100644 index 0000000000..1ddb1b91c9 --- /dev/null +++ b/activerecord/test/cases/adapters/mysql/sql_types_test.rb @@ -0,0 +1,14 @@ +require "cases/helper" + +class SqlTypesTest < ActiveRecord::TestCase + def test_binary_types + assert_equal 'varbinary(64)', type_to_sql(:binary, 64) + assert_equal 'varbinary(4095)', type_to_sql(:binary, 4095) + assert_equal 'blob(4096)', type_to_sql(:binary, 4096) + assert_equal 'blob', type_to_sql(:binary) + end + + def type_to_sql(*args) + ActiveRecord::Base.connection.type_to_sql(*args) + end +end diff --git a/activerecord/test/cases/adapters/mysql2/sql_types_test.rb b/activerecord/test/cases/adapters/mysql2/sql_types_test.rb new file mode 100644 index 0000000000..1ddb1b91c9 --- /dev/null +++ b/activerecord/test/cases/adapters/mysql2/sql_types_test.rb @@ -0,0 +1,14 @@ +require "cases/helper" + +class SqlTypesTest < ActiveRecord::TestCase + def test_binary_types + assert_equal 'varbinary(64)', type_to_sql(:binary, 64) + assert_equal 'varbinary(4095)', type_to_sql(:binary, 4095) + assert_equal 'blob(4096)', type_to_sql(:binary, 4096) + assert_equal 'blob', type_to_sql(:binary) + end + + def type_to_sql(*args) + ActiveRecord::Base.connection.type_to_sql(*args) + end +end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 5f13124e5b..7ff0044bd4 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -203,6 +203,12 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{t.text\s+"body",\s+null: false$}, output end + def test_schema_dump_includes_length_for_mysql_binary_fields + output = standard_dump + assert_match %r{t.binary\s+"var_binary",\s+limit: 255$}, output + assert_match %r{t.binary\s+"var_binary_large",\s+limit: 4095$}, output + end + def test_schema_dump_includes_length_for_mysql_blob_and_text_fields output = standard_dump assert_match %r{t.binary\s+"tiny_blob",\s+limit: 255$}, output diff --git a/activerecord/test/schema/mysql2_specific_schema.rb b/activerecord/test/schema/mysql2_specific_schema.rb index 24a43d7ece..f25f72c481 100644 --- a/activerecord/test/schema/mysql2_specific_schema.rb +++ b/activerecord/test/schema/mysql2_specific_schema.rb @@ -1,15 +1,19 @@ ActiveRecord::Schema.define do - create_table :binary_fields, :force => true do |t| - t.binary :tiny_blob, :limit => 255 - t.binary :normal_blob, :limit => 65535 - t.binary :medium_blob, :limit => 16777215 - t.binary :long_blob, :limit => 2147483647 - t.text :tiny_text, :limit => 255 - t.text :normal_text, :limit => 65535 - t.text :medium_text, :limit => 16777215 - t.text :long_text, :limit => 2147483647 + create_table :binary_fields, force: true do |t| + t.binary :var_binary, limit: 255 + t.binary :var_binary_large, limit: 4095 + t.column :tiny_blob, 'tinyblob', limit: 255 + t.binary :normal_blob, limit: 65535 + t.binary :medium_blob, limit: 16777215 + t.binary :long_blob, limit: 2147483647 + t.text :tiny_text, limit: 255 + t.text :normal_text, limit: 65535 + t.text :medium_text, limit: 16777215 + t.text :long_text, limit: 2147483647 end + add_index :binary_fields, :var_binary + ActiveRecord::Base.connection.execute <<-SQL DROP PROCEDURE IF EXISTS ten; SQL diff --git a/activerecord/test/schema/mysql_specific_schema.rb b/activerecord/test/schema/mysql_specific_schema.rb index 802c08b819..5401c12ed5 100644 --- a/activerecord/test/schema/mysql_specific_schema.rb +++ b/activerecord/test/schema/mysql_specific_schema.rb @@ -1,15 +1,19 @@ ActiveRecord::Schema.define do - create_table :binary_fields, :force => true do |t| - t.binary :tiny_blob, :limit => 255 - t.binary :normal_blob, :limit => 65535 - t.binary :medium_blob, :limit => 16777215 - t.binary :long_blob, :limit => 2147483647 - t.text :tiny_text, :limit => 255 - t.text :normal_text, :limit => 65535 - t.text :medium_text, :limit => 16777215 - t.text :long_text, :limit => 2147483647 + create_table :binary_fields, force: true do |t| + t.binary :var_binary, limit: 255 + t.binary :var_binary_large, limit: 4095 + t.column :tiny_blob, 'tinyblob', limit: 255 + t.binary :normal_blob, limit: 65535 + t.binary :medium_blob, limit: 16777215 + t.binary :long_blob, limit: 2147483647 + t.text :tiny_text, limit: 255 + t.text :normal_text, limit: 65535 + t.text :medium_text, limit: 16777215 + t.text :long_text, limit: 2147483647 end + add_index :binary_fields, :var_binary + ActiveRecord::Base.connection.execute <<-SQL DROP PROCEDURE IF EXISTS ten; SQL |