diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-20 14:01:54 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-20 14:01:54 -0600 |
commit | ee865eb244c8db11dcfbadbce0994d3c6488e7ec (patch) | |
tree | 35251657b9401fa7fab66980769b25876c22cfd5 /activerecord/lib/active_record | |
parent | cbfe5bb3c40956a0481b7c3d10177b8c68e63aaa (diff) | |
parent | f8438ae336f8f7d38c83a178d6ab6a9af635ee6c (diff) | |
download | rails-ee865eb244c8db11dcfbadbce0994d3c6488e7ec.tar.gz rails-ee865eb244c8db11dcfbadbce0994d3c6488e7ec.tar.bz2 rails-ee865eb244c8db11dcfbadbce0994d3c6488e7ec.zip |
Merge pull request #21962 from kamipo/fix_tinyblob
Fix to correctly schema dump the `tinyblob`
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 31 insertions, 11 deletions
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 f710ec10fc..e9365cf8d0 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -134,6 +134,7 @@ module ActiveRecord time: { name: "time" }, date: { name: "date" }, binary: { name: "blob" }, + blob: { name: "blob" }, boolean: { name: "tinyint", limit: 1 }, bigint: { name: "bigint" }, json: { name: "json" }, @@ -679,12 +680,18 @@ module ActiveRecord # Maps logical Rails types to MySQL-specific data types. def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = nil) sql = case type.to_s - when 'binary' - binary_to_sql(limit) when 'integer' integer_to_sql(limit) when 'text' text_to_sql(limit) + when 'blob' + binary_to_sql(limit) + when 'binary' + if (0..0xfff) === limit + "varbinary(#{limit})" + else + binary_to_sql(limit) + end else super(type, limit, precision, scale) end @@ -997,15 +1004,6 @@ module ActiveRecord MySQL::TableDefinition.new(native_database_types, name, temporary, options, as) end - def binary_to_sql(limit) # :nodoc: - case limit - when 0..0xfff; "varbinary(#{limit})" - when nil; "blob" - when 0x1000..0xffffffff; "blob(#{limit})" - else raise(ActiveRecordError, "No binary type has byte length #{limit}") - end - end - def integer_to_sql(limit) # :nodoc: case limit when 1; 'tinyint' @@ -1028,6 +1026,16 @@ module ActiveRecord end end + def binary_to_sql(limit) # :nodoc: + case limit + when 0..0xff; 'tinyblob' + when nil, 0x100..0xffff; 'blob' + when 0x10000..0xffffff; 'mediumblob' + when 0x1000000..0xffffffff; 'longblob' + else raise(ActiveRecordError, "No binary type has byte length #{limit}") + end + end + class MysqlJson < Type::Internal::AbstractJson # :nodoc: def changed_in_place?(raw_old_value, new_value) # Normalization is required because MySQL JSON data format includes diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb index bcf397cd77..29e8c73d46 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb @@ -7,6 +7,10 @@ module ActiveRecord super end + def blob(*args, **options) + args.each { |name| column(name, :blob, options) } + end + def json(*args, **options) args.each { |name| column(name, :json, options) } end diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb index 6448b8b5e2..3c48d0554e 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -27,6 +27,14 @@ module ActiveRecord private + def schema_type(column) + if column.sql_type == 'tinyblob' + 'blob' + else + super + end + end + def schema_limit(column) super unless column.type == :boolean end |