class Lhm::Entangler
Attributes
connection[R]
Public Class Methods
new(migration, connection = nil, options = {})
click to toggle source
Creates entanglement between two tables. All creates, updates and deletes to origin will be repeated on the destination table.
# File lib/lhm/entangler.rb, line 17 def initialize(migration, connection = nil, options = {}) @intersection = migration.intersection @origin = migration.origin @destination = migration.destination @connection = connection @retry_helper = SqlRetry.new( @connection, { log_prefix: "Entangler" }.merge!(options.fetch(:retriable, {})) ) end
Public Instance Methods
after()
click to toggle source
# File lib/lhm/entangler.rb, line 99 def after untangle.each do |stmt| @retry_helper.with_retries do |retriable_connection| retriable_connection.execute(stmt) end end end
before()
click to toggle source
# File lib/lhm/entangler.rb, line 91 def before entangle.each do |stmt| @retry_helper.with_retries do |retriable_connection| retriable_connection.execute(stmt) end end end
create_delete_trigger()
click to toggle source
# File lib/lhm/entangler.rb, line 64 def create_delete_trigger strip %Q{ create trigger `#{ trigger(:del) }` after delete on `#{ @origin.name }` for each row delete ignore from `#{ @destination.name }` #{ SqlHelper.annotation } where `#{ @destination.name }`.`id` = OLD.`id` } end
create_insert_trigger()
click to toggle source
# File lib/lhm/entangler.rb, line 46 def create_insert_trigger strip %Q{ create trigger `#{ trigger(:ins) }` after insert on `#{ @origin.name }` for each row replace into `#{ @destination.name }` (#{ @intersection.destination.joined }) #{ SqlHelper.annotation } values (#{ @intersection.origin.typed('NEW') }) } end
create_update_trigger()
click to toggle source
# File lib/lhm/entangler.rb, line 55 def create_update_trigger strip %Q{ create trigger `#{ trigger(:upd) }` after update on `#{ @origin.name }` for each row replace into `#{ @destination.name }` (#{ @intersection.destination.joined }) #{ SqlHelper.annotation } values (#{ @intersection.origin.typed('NEW') }) } end
entangle()
click to toggle source
# File lib/lhm/entangler.rb, line 30 def entangle [ create_delete_trigger, create_insert_trigger, create_update_trigger ] end
expected_triggers()
click to toggle source
# File lib/lhm/entangler.rb, line 77 def expected_triggers [trigger(:ins), trigger(:upd), trigger(:del)] end
revert()
click to toggle source
# File lib/lhm/entangler.rb, line 107 def revert after end
trigger(type)
click to toggle source
# File lib/lhm/entangler.rb, line 73 def trigger(type) "lhmt_#{ type }_#{ @origin.name }"[0...64] end
untangle()
click to toggle source
# File lib/lhm/entangler.rb, line 38 def untangle [ "drop trigger if exists `#{ trigger(:del) }`", "drop trigger if exists `#{ trigger(:ins) }`", "drop trigger if exists `#{ trigger(:upd) }`" ] end
validate()
click to toggle source
# File lib/lhm/entangler.rb, line 81 def validate unless @connection.data_source_exists?(@origin.name) error("#{ @origin.name } does not exist") end unless @connection.data_source_exists?(@destination.name) error("#{ @destination.name } does not exist") end end
Private Instance Methods
strip(sql)
click to toggle source
# File lib/lhm/entangler.rb, line 113 def strip(sql) sql.strip.gsub(/\n */, "\n") end