diff --git a/lib/lhm.rb b/lib/lhm.rb index 8436e926..2bbbc78b 100644 --- a/lib/lhm.rb +++ b/lib/lhm.rb @@ -50,6 +50,17 @@ def change_table(table_name, options = {}, &block) true end + def sync_table(table_name, sync_table, options = {}, &block) + Lhm::Table.naming_strategy = lambda{|x| sync_table.to_s } + origin = Table.parse(table_name, connection) + invoker = SyncInvoker.new(origin, connection) + block.call(invoker.migrator) + invoker.run(options) + ensure + Lhm::Table.naming_strategy = nil + true + end + # Cleanup tables and triggers # # @param [Boolean] run execute now or just display information diff --git a/lib/lhm/chunker.rb b/lib/lhm/chunker.rb index 0691462c..b8365f37 100644 --- a/lib/lhm/chunker.rb +++ b/lib/lhm/chunker.rb @@ -29,6 +29,7 @@ def execute @next_to_insert = @start while @next_to_insert < @limit || (@start == @limit) stride = @throttler.stride + Lhm.logger.debug(copy(bottom, top(stride))) affected_rows = @connection.update(copy(bottom, top(stride))) if @throttler && affected_rows > 0 diff --git a/lib/lhm/entangler.rb b/lib/lhm/entangler.rb index c73bf2be..57502918 100644 --- a/lib/lhm/entangler.rb +++ b/lib/lhm/entangler.rb @@ -79,6 +79,7 @@ def validate def before entangle.each do |stmt| + Lhm.logger.debug(tagged(stmt)) @connection.execute(tagged(stmt)) end end @@ -99,4 +100,10 @@ def strip(sql) sql.strip.gsub(/\n */, "\n") end end + + class SyncEntangler < Entangler + def after + # do nothing + end + end end diff --git a/lib/lhm/invoker.rb b/lib/lhm/invoker.rb index 72baac1f..e3e44516 100644 --- a/lib/lhm/invoker.rb +++ b/lib/lhm/invoker.rb @@ -42,7 +42,7 @@ def run(options = {}) set_session_lock_wait_timeouts migration = @migrator.run - Entangler.new(migration, @connection).run do + Entangler.new(migration, @connection, options[:delete_trigger_after] && true).run do Chunker.new(migration, @connection, options).run if options[:atomic_switch] AtomicSwitcher.new(migration, @connection).run @@ -78,4 +78,19 @@ def normalize_options(options) raise end end + + class SyncInvoker < Invoker + def run(options = {}) + normalize_options(options) + set_session_lock_wait_timeouts + migration = @migrator.run + + SyncEntangler.new(migration, @connection).run do + Chunker.new(migration, @connection, options).run + end + rescue => e + revert + raise + end + end end diff --git a/lib/lhm/migrator.rb b/lib/lhm/migrator.rb index b0ee6608..de84fcd9 100644 --- a/lib/lhm/migrator.rb +++ b/lib/lhm/migrator.rb @@ -210,6 +210,7 @@ def destination_create original = %{CREATE TABLE `#{ @origin.name }`} replacement = %{CREATE TABLE `#{ @origin.destination_name }`} stmt = @origin.ddl.gsub(original, replacement) + Lhm.logger.debug(tagged(stmt)) @connection.execute(tagged(stmt)) end diff --git a/lib/lhm/table.rb b/lib/lhm/table.rb index 7ff30fd2..861710fd 100644 --- a/lib/lhm/table.rb +++ b/lib/lhm/table.rb @@ -6,6 +6,8 @@ module Lhm class Table attr_reader :name, :columns, :indices, :pk, :ddl + @@naming_strategy = nil + @@default_naming_strategy = lambda { |name| "lhmn_#{ @name }" } def initialize(name, pk = 'id', ddl = nil) @name = name @@ -15,13 +17,18 @@ def initialize(name, pk = 'id', ddl = nil) @ddl = ddl end + def self.naming_strategy=(naming_strategy) + @@naming_strategy = naming_strategy + end + def satisfies_id_column_requirement? !!((id = columns['id']) && id[:type] =~ /(bigint|int)\(\d+\)/) end def destination_name - "lhmn_#{ @name }" + naming_strategy = @@naming_strategy || @@default_naming_strategy + naming_strategy.call(@name) end def self.parse(table_name, connection)