From 5d5de61f452faed47e6d5442a834d5f884b7e40b Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 11 Oct 2015 17:58:04 +0900 Subject: 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. --- .../connection_adapters/abstract_mysql_adapter.rb | 67 ++-------------------- .../mysql/schema_definitions.rb | 65 +++++++++++++++++++++ 2 files changed, 70 insertions(+), 62 deletions(-) create mode 100644 activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb (limited to 'activerecord') 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_(?.+)\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_(?.+)\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 -- cgit v1.2.3 From 11fc84c0137408c22222925f8e16a4e28a60c9c5 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 11 Oct 2015 18:12:23 +0900 Subject: Move schema creation class into `mysql/schema_creation.rb` Current master branch includes many schema creation improvements in MySQL. It extract these features to the appropriate file. --- .../connection_adapters/abstract_mysql_adapter.rb | 55 +-------------------- .../connection_adapters/mysql/schema_creation.rb | 57 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 53 deletions(-) create mode 100644 activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb (limited to 'activerecord') 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 487eac91dd..b260133f4a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -1,4 +1,5 @@ require 'active_record/connection_adapters/abstract_adapter' +require 'active_record/connection_adapters/mysql/schema_creation' require 'active_record/connection_adapters/mysql/schema_definitions' require 'active_support/core_ext/string/strip' @@ -8,64 +9,12 @@ module ActiveRecord class AbstractMysqlAdapter < AbstractAdapter include Savepoints - class SchemaCreation < AbstractAdapter::SchemaCreation - private - - def visit_DropForeignKey(name) - "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 - - def visit_ChangeColumnDefinition(o) - change_column_sql = "CHANGE #{quote_column_name(o.name)} #{accept(o.column)}" - add_column_position!(change_column_sql, column_options(o.column)) - end - - def column_options(o) - column_options = super - column_options[:charset] = o.charset - column_options - end - - def add_column_options!(sql, options) - if options[:charset] - sql << " CHARACTER SET #{options[:charset]}" - end - if options[:collation] - sql << " COLLATE #{options[:collation]}" - end - super - end - - def add_column_position!(sql, options) - if options[:first] - sql << " FIRST" - elsif options[:after] - sql << " AFTER #{quote_column_name(options[:after])}" - end - sql - end - - def index_in_create(table_name, column_name, options) - index_name, index_type, index_columns, _, _, index_using = @conn.add_index_options(table_name, column_name, options) - "#{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns}) " - end - end - def update_table_definition(table_name, base) # :nodoc: MySQL::Table.new(table_name, base) end def schema_creation - SchemaCreation.new self + MySQL::SchemaCreation.new(self) end def column_spec_for_primary_key(column) diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb new file mode 100644 index 0000000000..1e2c859af9 --- /dev/null +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb @@ -0,0 +1,57 @@ +module ActiveRecord + module ConnectionAdapters + module MySQL + class SchemaCreation < AbstractAdapter::SchemaCreation + private + + def visit_DropForeignKey(name) + "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 + + def visit_ChangeColumnDefinition(o) + change_column_sql = "CHANGE #{quote_column_name(o.name)} #{accept(o.column)}" + add_column_position!(change_column_sql, column_options(o.column)) + end + + def column_options(o) + column_options = super + column_options[:charset] = o.charset + column_options + end + + def add_column_options!(sql, options) + if options[:charset] + sql << " CHARACTER SET #{options[:charset]}" + end + if options[:collation] + sql << " COLLATE #{options[:collation]}" + end + super + end + + def add_column_position!(sql, options) + if options[:first] + sql << " FIRST" + elsif options[:after] + sql << " AFTER #{quote_column_name(options[:after])}" + end + sql + end + + def index_in_create(table_name, column_name, options) + index_name, index_type, index_columns, _, _, index_using = @conn.add_index_options(table_name, column_name, options) + "#{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns}) " + end + end + end + end +end -- cgit v1.2.3