diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2015-10-11 17:58:04 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2015-10-11 17:58:04 +0900 |
commit | 5d5de61f452faed47e6d5442a834d5f884b7e40b (patch) | |
tree | 08a04d96380993414e065cf67fd629b65bbb4531 /activerecord/lib/active_record | |
parent | f50d953ff646b84a1c791a8034cfb4f3789fc8bf (diff) | |
download | rails-5d5de61f452faed47e6d5442a834d5f884b7e40b.tar.gz rails-5d5de61f452faed47e6d5442a834d5f884b7e40b.tar.bz2 rails-5d5de61f452faed47e6d5442a834d5f884b7e40b.zip |
Move schema definition classes into `mysql/schema_definitions.rb`
Current master branch includes many schema definition improvements in
MySQL. It extract these features to the appropriate file.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 67 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb | 65 |
2 files changed, 70 insertions, 62 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 24408d6bb6..487eac91dd 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -1,3 +1,6 @@ +require 'active_record/connection_adapters/abstract_adapter' +require 'active_record/connection_adapters/mysql/schema_definitions' + require 'active_support/core_ext/string/strip' module ActiveRecord @@ -5,66 +8,6 @@ module ActiveRecord class AbstractMysqlAdapter < AbstractAdapter include Savepoints - module ColumnMethods - def primary_key(name, type = :primary_key, **options) - options[:auto_increment] = true if type == :bigint - super - end - - 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, :unsigned - end - - class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition - include ColumnMethods - - def new_column_definition(name, type, options) # :nodoc: - column = super - case column.type - 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 - - private - - def create_column_definition(name, type) - ColumnDefinition.new(name, type) - end - end - - class Table < ActiveRecord::ConnectionAdapters::Table - include ColumnMethods - end - class SchemaCreation < AbstractAdapter::SchemaCreation private @@ -118,7 +61,7 @@ module ActiveRecord end def update_table_definition(table_name, base) # :nodoc: - Table.new(table_name, base) + MySQL::Table.new(table_name, base) end def schema_creation @@ -1143,7 +1086,7 @@ module ActiveRecord end def create_table_definition(name, temporary = false, options = nil, as = nil) # :nodoc: - TableDefinition.new(native_database_types, name, temporary, options, as) + MySQL::TableDefinition.new(native_database_types, name, temporary, options, as) end def binary_to_sql(limit) # :nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb new file mode 100644 index 0000000000..bcf397cd77 --- /dev/null +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb @@ -0,0 +1,65 @@ +module ActiveRecord + module ConnectionAdapters + module MySQL + module ColumnMethods + def primary_key(name, type = :primary_key, **options) + options[:auto_increment] = true if type == :bigint + super + end + + 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, :unsigned + end + + class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition + include ColumnMethods + + def new_column_definition(name, type, options) # :nodoc: + column = super + case column.type + 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 + + private + + def create_column_definition(name, type) + MySQL::ColumnDefinition.new(name, type) + end + end + + class Table < ActiveRecord::ConnectionAdapters::Table + include ColumnMethods + end + end + end +end |