Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Migrate table #148

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/lhm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/lhm/chunker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/lhm/entangler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def validate

def before
entangle.each do |stmt|
Lhm.logger.debug(tagged(stmt))
@connection.execute(tagged(stmt))
end
end
Expand All @@ -99,4 +100,10 @@ def strip(sql)
sql.strip.gsub(/\n */, "\n")
end
end

class SyncEntangler < Entangler
def after
# do nothing
end
end
end
17 changes: 16 additions & 1 deletion lib/lhm/invoker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/lhm/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion lib/lhm/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down