aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJeremy Daer <jeremydaer@gmail.com>2015-09-19 17:44:57 -0700
committerJeremy Daer <jeremydaer@gmail.com>2015-09-19 17:44:57 -0700
commit26aa1b58b0e9451d15a0ee32da3c004d44e5a356 (patch)
treeb9be1f77f015324ce91ea010834ef81cb49c84fd /activerecord/lib/active_record
parentd3f2c59bdd4f91a1562e6c405a9b2dc62163d943 (diff)
parentdfeb3ee78a44b630ef414a7ce6e93265b2cc4f28 (diff)
downloadrails-26aa1b58b0e9451d15a0ee32da3c004d44e5a356.tar.gz
rails-26aa1b58b0e9451d15a0ee32da3c004d44e5a356.tar.bz2
rails-26aa1b58b0e9451d15a0ee32da3c004d44e5a356.zip
Merge pull request #17696 from kamipo/unsigned_integer_support
Add `unsigned` support for numeric data types in MySQL
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb53
1 files changed, 49 insertions, 4 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 d200170b97..97f54dc0b0 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -14,10 +14,26 @@ module ActiveRecord
def json(*args, **options)
args.each { |name| column(name, :json, options) }
end
+
+ def unsigned_integer(*args, **options)
+ args.each { |name| column(name, :unsigned_integer, options) }
+ end
+
+ def unsigned_bigint(*args, **options)
+ args.each { |name| column(name, :unsigned_bigint, options) }
+ end
+
+ def unsigned_float(*args, **options)
+ args.each { |name| column(name, :unsigned_float, options) }
+ end
+
+ def unsigned_decimal(*args, **options)
+ args.each { |name| column(name, :unsigned_decimal, options) }
+ end
end
class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition
- attr_accessor :charset
+ attr_accessor :charset, :unsigned
end
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
@@ -29,7 +45,11 @@ module ActiveRecord
when :primary_key
column.type = :integer
column.auto_increment = true
+ when /\Aunsigned_(?<type>.+)\z/
+ column.type = $~[:type].to_sym
+ column.unsigned = true
end
+ column.unsigned ||= options[:unsigned]
column.charset = options[:charset]
column
end
@@ -52,6 +72,11 @@ module ActiveRecord
"DROP FOREIGN KEY #{name}"
end
+ def visit_ColumnDefinition(o)
+ o.sql_type = type_to_sql(o.type, o.limit, o.precision, o.scale, o.unsigned)
+ super
+ end
+
def visit_AddColumnDefinition(o)
add_column_position!(super, column_options(o.column))
end
@@ -104,6 +129,7 @@ module ActiveRecord
spec = {}
if column.auto_increment?
spec[:id] = ':bigint' if column.bigint?
+ spec[:unsigned] = 'true' if column.unsigned?
return if spec.empty?
else
spec[:id] = column.type.inspect
@@ -112,6 +138,16 @@ module ActiveRecord
spec
end
+ def prepare_column_options(column)
+ spec = super
+ spec[:unsigned] = 'true' if column.unsigned?
+ spec
+ end
+
+ def migration_keys
+ super + [:unsigned]
+ end
+
private
def schema_limit(column)
@@ -158,6 +194,10 @@ module ActiveRecord
sql_type =~ /blob/i || type == :text
end
+ def unsigned?
+ /unsigned/ === sql_type
+ end
+
def case_sensitive?
collation && !collation.match(/_ci$/)
end
@@ -793,8 +833,8 @@ module ActiveRecord
end
# Maps logical Rails types to MySQL-specific data types.
- def type_to_sql(type, limit = nil, precision = nil, scale = nil)
- case type.to_s
+ 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'
@@ -802,8 +842,11 @@ module ActiveRecord
when 'text'
text_to_sql(limit)
else
- super
+ super(type, limit, precision, scale)
end
+
+ sql << ' unsigned' if unsigned && type != :primary_key
+ sql
end
# SHOW VARIABLES LIKE 'name'
@@ -1180,6 +1223,8 @@ module ActiveRecord
ActiveRecord::Type.register(:json, MysqlJson, adapter: :mysql2)
ActiveRecord::Type.register(:string, MysqlString, adapter: :mysql)
ActiveRecord::Type.register(:string, MysqlString, adapter: :mysql2)
+ ActiveRecord::Type.register(:unsigned_integer, Type::UnsignedInteger, adapter: :mysql)
+ ActiveRecord::Type.register(:unsigned_integer, Type::UnsignedInteger, adapter: :mysql2)
end
end
end