Class: Lhm::Entangler
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from SqlHelper
#annotation, #idx_name, #idx_spec, #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.
16
17
18
19
20
21
|
# File 'lib/lhm/entangler.rb', line 16
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.
12
13
14
|
# File 'lib/lhm/entangler.rb', line 12
def connection
@connection
end
|
Instance Method Details
#after ⇒ Object
84
85
86
|
# File 'lib/lhm/entangler.rb', line 84
def after
@connection.sql(untangle)
end
|
#before ⇒ Object
80
81
82
|
# File 'lib/lhm/entangler.rb', line 80
def before
@connection.sql(entangle)
end
|
#create_delete_trigger ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/lhm/entangler.rb', line 57
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
39
40
41
42
43
44
45
46
|
# File 'lib/lhm/entangler.rb', line 39
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
48
49
50
51
52
53
54
55
|
# File 'lib/lhm/entangler.rb', line 48
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
23
24
25
26
27
28
29
|
# File 'lib/lhm/entangler.rb', line 23
def entangle
[
create_delete_trigger,
create_insert_trigger,
create_update_trigger
]
end
|
#revert ⇒ Object
88
89
90
|
# File 'lib/lhm/entangler.rb', line 88
def revert
after
end
|
#trigger(type) ⇒ Object
66
67
68
|
# File 'lib/lhm/entangler.rb', line 66
def trigger(type)
"lhmt_#{ type }_#{ @origin.name }"
end
|
#untangle ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/lhm/entangler.rb', line 31
def untangle
[
"drop trigger if exists `#{ trigger(:del) }`",
"drop trigger if exists `#{ trigger(:ins) }`",
"drop trigger if exists `#{ trigger(:upd) }`"
]
end
|
#validate ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'lib/lhm/entangler.rb', line 70
def validate
unless @connection.table_exists?(@origin.name)
error("#{ @origin.name } does not exist")
end
unless @connection.table_exists?(@destination.name)
error("#{ @destination.name } does not exist")
end
end
|