Permalink
Browse files

Move the bulk alter table code into the abstract mysql adapter, hence…

… it is supported for mysql2 as well now.
  • Loading branch information...
1 parent 4fcd847 commit fd22d040fef48778a519825dd2f0cf2fd73a8965 @jonleighton jonleighton committed Aug 29, 2011
View
@@ -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.
@@ -140,6 +140,10 @@ def supports_savepoints?
true
end
+ def supports_bulk_alter? #:nodoc:
+ true
+ end
+
def native_database_types
NATIVE_DATABASE_TYPES
end
@@ -401,6 +405,21 @@ def create_table(table_name, options = {}) #:nodoc:
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 @@ def rename_column_sql(table_name, column_name, new_column_name)
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?
@@ -97,11 +97,6 @@ def initialize(connection, logger, connection_options, config)
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 @@ def begin_db_transaction #:nodoc:
# 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)

0 comments on commit fd22d04

Please sign in to comment.