-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_sync.rb
More file actions
executable file
·65 lines (51 loc) · 1.77 KB
/
database_sync.rb
File metadata and controls
executable file
·65 lines (51 loc) · 1.77 KB
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
#!/usr/bin/env ruby
require 'dotenv'
Dotenv.load
require 'thor'
require 'active_record'
require Dir.pwd + '/lib/left_model.rb'
require Dir.pwd + '/lib/right_model.rb'
module Database
class Sync < Thor
desc 'sync', 'synchronize tables between two databases'
def sync
tables = ENV['TABLES'].split(',')
log("Tables to sync: #{tables.join(', ')}")
tables.each do |table|
log("Processing table '#{table}'...")
left_models_ids = []
total_records = 0
created_records = 0
deleted_records = 0
LeftModel.table_name = table
RightModel.table_name = table
model_right_attributes_names = RightModel.first.attribute_names
LeftModel.order('id desc').each do |model|
left_models_ids << model.id
total_records += 1
model_left_attributes = model.attributes.select{|key,_| model_right_attributes_names.include? key}
if RightModel.exists?(model.id)
RightModel.find(model.id).update_attributes(model_left_attributes)
else
created_records += 1
m = RightModel.new
m.assign_attributes(model_left_attributes)
m.save
end
end
deleted_records = RightModel.where("id not in (?)", left_models_ids).count()
RightModel.where("id not in (?)", left_models_ids).delete_all
log("Total: #{total_records} synced records")
log("Created: #{created_records} records")
log("Deleted: #{deleted_records} records")
log("------------------------------------------")
end
log('Finished!')
end
private
def log(message)
puts "[database_sync] #{Time.now}: #{message}"
end
end
end
Database::Sync.start