diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2015-02-14 15:59:33 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2015-09-18 20:22:32 +0900 |
commit | dfeb3ee78a44b630ef414a7ce6e93265b2cc4f28 (patch) | |
tree | f48fc57752bc45c297ef9c195f6b3d09afb5f9ed | |
parent | f3772f729c72d098156b35eb105d3ffdd928c5eb (diff) | |
download | rails-dfeb3ee78a44b630ef414a7ce6e93265b2cc4f28.tar.gz rails-dfeb3ee78a44b630ef414a7ce6e93265b2cc4f28.tar.bz2 rails-dfeb3ee78a44b630ef414a7ce6e93265b2cc4f28.zip |
Add `unsigned` types for numeric data types in MySQL
In the case of using `unsigned` as the type:
create_table :foos do |t|
t.unsigned_integer :unsigned_integer
t.unsigned_bigint :unsigned_bigint
t.unsigned_float :unsigned_float
t.unsigned_decimal :unsigned_decimal, precision: 10, scale: 2
end
4 files changed, 56 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 8c53069f5b..1f300dea5d 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -9,6 +9,15 @@ t.decimal :unsigned_decimal, unsigned: true, precision: 10, scale: 2 end + In the case of using `unsigned` as the type: + + create_table :foos do |t| + t.unsigned_integer :unsigned_integer + t.unsigned_bigint :unsigned_bigint + t.unsigned_float :unsigned_float + t.unsigned_decimal :unsigned_decimal, precision: 10, scale: 2 + end + *Ryuta Kamizono* * Lookup the attribute name for `restrict_with_error` messages on the 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 55de098f70..245d476abb 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -14,6 +14,22 @@ 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 @@ -29,6 +45,9 @@ 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] @@ -1108,6 +1127,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 diff --git a/activerecord/test/cases/adapters/mysql/unsigned_type_test.rb b/activerecord/test/cases/adapters/mysql/unsigned_type_test.rb index 12c52b97b6..84c5394c2e 100644 --- a/activerecord/test/cases/adapters/mysql/unsigned_type_test.rb +++ b/activerecord/test/cases/adapters/mysql/unsigned_type_test.rb @@ -42,6 +42,19 @@ class MysqlUnsignedTypeTest < ActiveRecord::MysqlTestCase end end + test "schema definition can use unsigned as the type" do + @connection.change_table("unsigned_types") do |t| + t.unsigned_integer :unsigned_integer_t + t.unsigned_bigint :unsigned_bigint_t + t.unsigned_float :unsigned_float_t + t.unsigned_decimal :unsigned_decimal_t, precision: 10, scale: 2 + end + + @connection.columns("unsigned_types").select { |c| /^unsigned_/ === c.name }.each do |column| + assert column.unsigned? + end + end + test "schema dump includes unsigned option" do schema = dump_table_schema "unsigned_types" assert_match %r{t.integer\s+"unsigned_integer",\s+limit: 4,\s+unsigned: true$}, schema diff --git a/activerecord/test/cases/adapters/mysql2/unsigned_type_test.rb b/activerecord/test/cases/adapters/mysql2/unsigned_type_test.rb index aea2ef4af5..a6f6dd21bb 100644 --- a/activerecord/test/cases/adapters/mysql2/unsigned_type_test.rb +++ b/activerecord/test/cases/adapters/mysql2/unsigned_type_test.rb @@ -42,6 +42,19 @@ class Mysql2UnsignedTypeTest < ActiveRecord::Mysql2TestCase end end + test "schema definition can use unsigned as the type" do + @connection.change_table("unsigned_types") do |t| + t.unsigned_integer :unsigned_integer_t + t.unsigned_bigint :unsigned_bigint_t + t.unsigned_float :unsigned_float_t + t.unsigned_decimal :unsigned_decimal_t, precision: 10, scale: 2 + end + + @connection.columns("unsigned_types").select { |c| /^unsigned_/ === c.name }.each do |column| + assert column.unsigned? + end + end + test "schema dump includes unsigned option" do schema = dump_table_schema "unsigned_types" assert_match %r{t.integer\s+"unsigned_integer",\s+limit: 4,\s+unsigned: true$}, schema |