1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
class ReferentialCloning < ApplicationModel
include AASM
belongs_to :source_referential, class_name: 'Referential'
belongs_to :target_referential, class_name: 'Referential'
after_commit :clone, on: :create
def clone
ReferentialCloningWorker.perform_async(id)
end
def clone_with_status!
run!
clone!
successful!
rescue Exception => e
Rails.logger.error "Clone failed : #{e}"
Rails.logger.error e.backtrace.join('\n')
failed!
end
def clone!
report = Benchmark.measure do
command = "#{dump_command} | #{sed_command} | #{restore_command}"
unless system command
raise "Copy of #{source_schema} to #{target_schema} failed"
end
end
target_referential.check_migration_count(report)
clean
end
def source_schema
source_referential.slug
end
def target_schema
target_referential.slug
end
def host
ActiveRecord::Base.connection_config[:host]
end
def username
ActiveRecord::Base.connection_config[:username]
end
def password
ActiveRecord::Base.connection_config[:password]
end
def database
ActiveRecord::Base.connection_config[:database]
end
def dump_command
"PGPASSWORD='#{password}' pg_dump --host #{host} --username #{username} --schema=#{source_schema} #{database}"
end
def sed_command
"sed -e 's@#{source_schema}@#{target_schema}@'"
end
def restore_command
"PGPASSWORD='#{password}' psql -q --host #{host} --username #{username} #{database}"
end
def clean
CleanUp.new(referential: target_referential).clean
end
private
aasm column: :status do
state :new, :initial => true
state :pending
state :successful
state :failed
event :run, after: :update_started_at do
transitions :from => [:new, :failed], :to => :pending
end
event :successful, after: :update_ended_at do
after do
target_referential.update_attribute(:ready, true)
end
transitions :from => [:pending, :failed], :to => :successful
end
event :failed, after: :update_ended_at do
transitions :from => :pending, :to => :failed
after do
target_referential.failed!
end
end
end
def update_started_at
update_attribute(:started_at, Time.now)
end
def update_ended_at
update_attribute(:ended_at, Time.now)
end
end
|