lib/lhm/entangler.rb in lhm-1.0.0.rc2 vs lib/lhm/entangler.rb in lhm-1.0.0.rc3
- old
+ new
@@ -1,33 +1,32 @@
-#
-# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
-# Schmidt
-#
-# Creates entanglement between two tables. All creates, updates and deletes
-# to origin will be repeated on the the destination table.
-#
+# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
+# Schmidt
require 'lhm/command'
+require 'lhm/sql_helper'
module Lhm
class Entangler
include Command
+ include SqlHelper
- attr_reader :epoch
+ attr_reader :connection
+ # Creates entanglement between two tables. All creates, updates and deletes
+ # to origin will be repeated on the destination table.
def initialize(migration, connection = nil)
@common = migration.intersection
@origin = migration.origin
@destination = migration.destination
@connection = connection
end
def entangle
[
- create_trigger_del,
- create_trigger_ins,
- create_trigger_upd
+ create_delete_trigger,
+ create_insert_trigger,
+ create_update_trigger
]
end
def untangle
[
@@ -35,29 +34,29 @@
"drop trigger if exists `#{ trigger(:ins) }`",
"drop trigger if exists `#{ trigger(:upd) }`"
]
end
- def create_trigger_ins
+ def create_insert_trigger
strip %Q{
create trigger `#{ trigger(:ins) }`
after insert on `#{ @origin.name }` for each row
replace into `#{ @destination.name }` (#{ @common.joined })
values (#{ @common.typed("NEW") })
}
end
- def create_trigger_upd
+ def create_update_trigger
strip %Q{
create trigger `#{ trigger(:upd) }`
after update on `#{ @origin.name }` for each row
replace into `#{ @destination.name }` (#{ @common.joined })
values (#{ @common.typed("NEW") })
}
end
- def create_trigger_del
+ def create_delete_trigger
strip %Q{
create trigger `#{ trigger(:del) }`
after delete on `#{ @origin.name }` for each row
delete ignore from `#{ @destination.name }`
where `#{ @destination.name }`.`id` = OLD.`id`
@@ -66,14 +65,10 @@
def trigger(type)
"lhmt_#{ type }_#{ @origin.name }"
end
- #
- # Command implementation
- #
-
def validate
unless table?(@origin.name)
error("#{ @origin.name } does not exist")
end
@@ -82,11 +77,10 @@
end
end
def before
sql(entangle)
- @epoch = connection.select_value("select max(id) from #{ @origin.name }").to_i
end
def after
sql(untangle)
end
@@ -100,6 +94,5 @@
def strip(sql)
sql.strip.gsub(/\n */, "\n")
end
end
end
-