lib/lhm/migrator.rb in lhm-1.3.0 vs lib/lhm/migrator.rb in lhm-2.0.0
- old
+ new
@@ -11,11 +11,11 @@
# `run` returns a Migration which can be used for the remaining process.
class Migrator
include Command
include SqlHelper
- attr_reader :name, :statements, :connection
+ attr_reader :name, :statements, :connection, :conditions
def initialize(table, connection = nil)
@connection = connection
@origin = table
@name = table.destination_name
@@ -133,14 +133,33 @@
# A column name given as String or Symbol. An Array of Strings or Symbols
# for compound indexes.
# @param [String, Symbol] index_name
# Optional name of the index to be removed
def remove_index(columns, index_name = nil)
+ columns = [columns].flatten.map(&:to_sym)
+ from_origin = @origin.indices.find {|name, cols| cols.map(&:to_sym) == columns}
+ index_name ||= from_origin[0] unless from_origin.nil?
index_name ||= idx_name(@origin.name, columns)
ddl("drop index `%s` on `%s`" % [index_name, @name])
end
+ # Filter the data that is copied into the new table by the provided SQL.
+ # This SQL will be inserted into the copy directly after the "from"
+ # statement - so be sure to use inner/outer join syntax and not cross joins.
+ #
+ # @example Add a conditions filter to the migration.
+ # Lhm.change_table(:sounds) do |m|
+ # m.filter("inner join users on users.`id` = sounds.`user_id` and sounds.`public` = 1")
+ # end
+ #
+ # @param [ String ] sql The sql filter.
+ #
+ # @return [ String ] The sql filter.
+ def filter(sql)
+ @conditions = sql
+ end
+
private
def validate
unless @connection.table_exists?(@origin.name)
error("could not find origin table #{ @origin.name }")
@@ -158,10 +177,10 @@
end
def execute
destination_create
@connection.sql(@statements)
- Migration.new(@origin, destination_read)
+ Migration.new(@origin, destination_read, conditions)
end
def destination_create
@connection.destination_create(@origin)
end