Move the bulk alter table code into the abstract mysql adapter, hence it is supported for mysql2 as well now.

This commit is contained in:
Jon Leighton
2011-08-29 12:43:17 +01:00
parent 4fcd847c8d
commit fd22d040fe
3 changed files with 44 additions and 47 deletions

View File

@@ -1,5 +1,7 @@
*Rails 3.2.0 (unreleased)*
* Support bulk change_table in mysql2 adapter, as well as the mysql one. [Jon Leighton]
* If multiple parameters are sent representing a date, and some are blank, the
resulting object is nil. In previous releases those values defaulted to 1. This
only affects existing but blank parameters, missing ones still raise an error.

View File

@@ -140,6 +140,10 @@ module ActiveRecord
true
end
def supports_bulk_alter? #:nodoc:
true
end
def native_database_types
NATIVE_DATABASE_TYPES
end
@@ -401,6 +405,21 @@ module ActiveRecord
super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB"))
end
def bulk_change_table(table_name, operations) #:nodoc:
sqls = operations.map do |command, args|
table, arguments = args.shift, args
method = :"#{command}_sql"
if respond_to?(method)
send(method, table, *arguments)
else
raise "Unknown method called : #{method}(#{arguments.inspect})"
end
end.flatten.join(", ")
execute("ALTER TABLE #{quote_table_name(table_name)} #{sqls}")
end
# Renames a table.
#
# Example:
@@ -552,6 +571,29 @@ module ActiveRecord
rename_column_sql
end
def remove_column_sql(table_name, *column_names)
columns_for_remove(table_name, *column_names).map {|column_name| "DROP #{column_name}" }
end
alias :remove_columns_sql :remove_column
def add_index_sql(table_name, column_name, options = {})
index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
"ADD #{index_type} INDEX #{index_name} (#{index_columns})"
end
def remove_index_sql(table_name, options = {})
index_name = index_name_for_remove(table_name, options)
"DROP INDEX #{index_name}"
end
def add_timestamps_sql(table_name)
[add_column_sql(table_name, :created_at, :datetime), add_column_sql(table_name, :updated_at, :datetime)]
end
def remove_timestamps_sql(table_name)
[remove_column_sql(table_name, :updated_at), remove_column_sql(table_name, :created_at)]
end
private
def supports_views?

View File

@@ -97,11 +97,6 @@ module ActiveRecord
connect
end
# FIXME: Move to abstract adapter
def supports_bulk_alter? #:nodoc:
true
end
# Returns true, since this connection adapter supports prepared statement
# caching.
def supports_statement_cache?
@@ -312,48 +307,6 @@ module ActiveRecord
# Transactions aren't supported
end
# SCHEMA STATEMENTS ========================================
def bulk_change_table(table_name, operations) #:nodoc:
sqls = operations.map do |command, args|
table, arguments = args.shift, args
method = :"#{command}_sql"
if respond_to?(method)
send(method, table, *arguments)
else
raise "Unknown method called : #{method}(#{arguments.inspect})"
end
end.flatten.join(", ")
execute("ALTER TABLE #{quote_table_name(table_name)} #{sqls}")
end
protected
def remove_column_sql(table_name, *column_names)
columns_for_remove(table_name, *column_names).map {|column_name| "DROP #{column_name}" }
end
alias :remove_columns_sql :remove_column
def add_index_sql(table_name, column_name, options = {})
index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
"ADD #{index_type} INDEX #{index_name} (#{index_columns})"
end
def remove_index_sql(table_name, options = {})
index_name = index_name_for_remove(table_name, options)
"DROP INDEX #{index_name}"
end
def add_timestamps_sql(table_name)
[add_column_sql(table_name, :created_at, :datetime), add_column_sql(table_name, :updated_at, :datetime)]
end
def remove_timestamps_sql(table_name)
[remove_column_sql(table_name, :updated_at), remove_column_sql(table_name, :created_at)]
end
private
def exec_stmt(sql, name, binds)