lib/sequel/adapters/shared/mysql.rb in sequel-5.46.0 vs lib/sequel/adapters/shared/mysql.rb in sequel-5.47.0

- old
+ new

@@ -185,10 +185,19 @@ # Options: # :server :: Set the server to use def views(opts=OPTS) full_tables('VIEW', opts) end + + # Renames multiple tables in a single call. + # + # DB.rename_tables [:items, :old_items], [:other_items, :old_other_items] + # # RENAME TABLE items TO old_items, other_items TO old_other_items + def rename_tables(*renames) + execute_ddl(rename_tables_sql(renames)) + renames.each{|from,| remove_cached_schema(from)} + end private def alter_table_add_column_sql(table, op) pos = if after_col = op[:after] @@ -469,9 +478,17 @@ end # Parse the schema for the given table to get an array of primary key columns def primary_key_from_schema(table) schema(table).select{|a| a[1][:primary_key]}.map{|a| a[0]} + end + + # SQL statement for renaming multiple tables. + def rename_tables_sql(renames) + rename_tos = renames.map do |from, to| + "#{quote_schema_table(from)} TO #{quote_schema_table(to)}" + end.join(', ') + "RENAME TABLE #{rename_tos}" end # Rollback the currently open XA transaction def rollback_transaction(conn, opts=OPTS) if (s = opts[:prepare]) && savepoint_level(conn) <= 1