Class: Lhm::Entangler
Constant Summary
collapse
- LOG_PREFIX =
"Entangler"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from SqlHelper
#annotation, #idx_name, #idx_spec, #tagged, #version_string
Methods included from Command
#run
Constructor Details
#initialize(migration, connection = nil) ⇒ Entangler
Creates entanglement between two tables. All creates, updates and deletes to origin will be repeated on the destination table.
20
21
22
23
24
25
|
# File 'lib/lhm/entangler.rb', line 20
def initialize(migration, connection = nil)
@intersection = migration.intersection
@origin = migration.origin
@destination = migration.destination
@connection = connection
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
14
15
16
|
# File 'lib/lhm/entangler.rb', line 14
def connection
@connection
end
|
Instance Method Details
#after ⇒ Object
95
96
97
98
99
100
|
# File 'lib/lhm/entangler.rb', line 95
def after
untangle.each do |stmt|
@connection.execute(stmt, should_retry: true, log_prefix: LOG_PREFIX)
end
Lhm.logger.info("Dropped triggers on #{@origin.name}")
end
|
#before ⇒ Object
88
89
90
91
92
93
|
# File 'lib/lhm/entangler.rb', line 88
def before
entangle.each do |stmt|
@connection.execute(stmt, should_retry: true, log_prefix: LOG_PREFIX)
end
Lhm.logger.info("Created triggers on #{@origin.name}")
end
|
#create_delete_trigger ⇒ Object
61
62
63
64
65
66
67
68
|
# File 'lib/lhm/entangler.rb', line 61
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 ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/lhm/entangler.rb', line 43
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 ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/lhm/entangler.rb', line 52
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 ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/lhm/entangler.rb', line 27
def entangle
[
create_delete_trigger,
create_insert_trigger,
create_update_trigger
]
end
|
#expected_triggers ⇒ Object
74
75
76
|
# File 'lib/lhm/entangler.rb', line 74
def expected_triggers
[trigger(:ins), trigger(:upd), trigger(:del)]
end
|
#revert ⇒ Object
102
103
104
|
# File 'lib/lhm/entangler.rb', line 102
def revert
after
end
|
#trigger(type) ⇒ Object
70
71
72
|
# File 'lib/lhm/entangler.rb', line 70
def trigger(type)
"lhmt_#{ type }_#{ @origin.name }"[0...64]
end
|
#untangle ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/lhm/entangler.rb', line 35
def untangle
[
"drop trigger if exists `#{ trigger(:del) }`",
"drop trigger if exists `#{ trigger(:ins) }`",
"drop trigger if exists `#{ trigger(:upd) }`"
]
end
|
#validate ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/lhm/entangler.rb', line 78
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
|