db-sync 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/db/sync.rb +33 -16
- data/lib/db/sync/model.rb +12 -1
- data/lib/db/sync/version.rb +1 -1
- data/lib/tasks/db-sync.rake +14 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bef6c074030fc6547c0805a20e284a5fd8e71af
|
4
|
+
data.tar.gz: 4b961e94877e81150a7bfcfa93e57d6395cce481
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 806b73455ee1664e88fd030bbdccb57817ef58da0a8ece91b309cab2986655df1b7c17bfb9af96b654595d1b068ffc172b853f7711eb6ebc97c255ba09a9bfb8
|
7
|
+
data.tar.gz: 918a47cd7e01e4c9c7964a44a79db4b102abcb7b65721767b91cf67cd79e3e340826456c16ed86fde5aaad3756dbf91587769f99f06a1bf83c0dc559f42e9ecf
|
data/lib/db/sync.rb
CHANGED
@@ -9,7 +9,12 @@ module Db
|
|
9
9
|
class Sync
|
10
10
|
include ActiveSupport::Configurable
|
11
11
|
|
12
|
-
def
|
12
|
+
def log
|
13
|
+
@log ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def sync_up(commit = true)
|
17
|
+
@log = []
|
13
18
|
# TODO: change to row by row loading
|
14
19
|
working_tables.each do |table|
|
15
20
|
table_model = data_model(table)
|
@@ -17,47 +22,58 @@ module Db
|
|
17
22
|
|
18
23
|
data = File.read(table_filename(table))
|
19
24
|
all_records = YAML.load(data)
|
20
|
-
current_records = table_model.records
|
25
|
+
current_records = table_model.records
|
21
26
|
|
22
27
|
diff = Db::Sync::Diff.new(current_records, all_records, table_model.pkey)
|
23
|
-
insert_records(table, diff.inserts)
|
24
|
-
delete_records(table, diff.deletes)
|
25
|
-
update_records(table, diff.updates)
|
28
|
+
insert_records(table, diff.inserts, commit)
|
29
|
+
delete_records(table, diff.deletes, commit)
|
30
|
+
update_records(table, diff.updates, commit)
|
26
31
|
end
|
27
32
|
end
|
28
33
|
|
29
|
-
def
|
34
|
+
def insert_records(table, inserts, commit = true)
|
30
35
|
inserts.each do |record|
|
36
|
+
log << "[#{table}] INSERT #{record}"
|
37
|
+
next unless commit
|
38
|
+
|
31
39
|
insert_manager = Arel::InsertManager.new(ActiveRecord::Base)
|
32
40
|
arel_model = Arel::Table.new(table)
|
33
41
|
insert_data = record.map do |key, value|
|
34
42
|
[arel_model[key], value]
|
35
43
|
end
|
44
|
+
|
36
45
|
insert_manager.insert(insert_data)
|
37
46
|
# print "#{insert_manager.to_sql}\n"
|
38
47
|
ActiveRecord::Base.connection.execute(insert_manager.to_sql)
|
39
48
|
end
|
40
49
|
end
|
41
50
|
|
42
|
-
def
|
51
|
+
def delete_records(table, deletes, commit = false)
|
43
52
|
arel_model = Arel::Table.new(table)
|
44
53
|
|
45
54
|
deletes.each do |delete_params|
|
55
|
+
log << "[#{table}] DELETE #{delete_params}"
|
56
|
+
next unless commit
|
57
|
+
|
46
58
|
delete_manager = Arel::DeleteManager.new(ActiveRecord::Base)
|
47
59
|
delete_manager.from(arel_model)
|
48
60
|
delete_data = delete_params.map do |key, value|
|
49
61
|
[arel_model[key].eq(value)]
|
50
62
|
end
|
63
|
+
|
51
64
|
delete_manager.where(delete_data)
|
52
65
|
# print "#{delete_manager.to_sql}\n"
|
53
66
|
ActiveRecord::Base.connection.execute(delete_manager.to_sql)
|
54
67
|
end
|
55
68
|
end
|
56
69
|
|
57
|
-
def
|
70
|
+
def update_records(table, updates, commit = false)
|
58
71
|
arel_model = Arel::Table.new(table)
|
59
72
|
|
60
73
|
updates.each do |update|
|
74
|
+
log << "[#{table}] UPDATE #{update[:key]} with #{update[:changes]}"
|
75
|
+
next unless commit
|
76
|
+
|
61
77
|
update_manager = Arel::UpdateManager.new(ActiveRecord::Base)
|
62
78
|
update_key = update[:key].map do |key, value|
|
63
79
|
[arel_model[key].eq(value)]
|
@@ -65,13 +81,14 @@ module Db
|
|
65
81
|
update_changes = update[:changes].map do |key, value|
|
66
82
|
[arel_model[key], value]
|
67
83
|
end
|
84
|
+
|
68
85
|
update_manager.table(arel_model).where(update_key).set(update_changes)
|
69
86
|
# print "#{update_manager.to_sql}\n"
|
70
87
|
ActiveRecord::Base.connection.execute(update_manager.to_sql)
|
71
88
|
end
|
72
89
|
end
|
73
90
|
|
74
|
-
def
|
91
|
+
def sync_down
|
75
92
|
# TODO: change to row by row saving
|
76
93
|
working_tables.each do |table|
|
77
94
|
File.open(table_filename(table), 'w') do |f|
|
@@ -81,32 +98,32 @@ module Db
|
|
81
98
|
end
|
82
99
|
end
|
83
100
|
|
84
|
-
def
|
101
|
+
def table_model_records(table)
|
85
102
|
# TODO: Some kind of paging
|
86
103
|
table_model = data_model(table)
|
87
|
-
table_model.records
|
104
|
+
table_model.records
|
88
105
|
end
|
89
106
|
|
90
|
-
def
|
107
|
+
def working_tables
|
91
108
|
config.tables || all_tables
|
92
109
|
end
|
93
110
|
|
94
|
-
def
|
111
|
+
def all_tables
|
95
112
|
ActiveRecord::Base.connection.tables.reject do |table|
|
96
113
|
%w(schema_info, schema_migrations).include?(table)
|
97
114
|
end
|
98
115
|
end
|
99
116
|
|
100
|
-
def
|
117
|
+
def table_filename(table)
|
101
118
|
# TODO: change data with custom dir
|
102
119
|
File.join(Rails.root || '.', 'db', 'data', "#{table}.yml")
|
103
120
|
end
|
104
121
|
|
105
|
-
def
|
122
|
+
def configure
|
106
123
|
yield(config)
|
107
124
|
end
|
108
125
|
|
109
|
-
def
|
126
|
+
def data_model(table)
|
110
127
|
result = Class.new(Db::Sync::Model)
|
111
128
|
result.table_name = table
|
112
129
|
result
|
data/lib/db/sync/model.rb
CHANGED
@@ -23,7 +23,18 @@ class Db::Sync::Model < ActiveRecord::Base
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
def self.ordered_attributes
|
27
|
+
pkey + (attribute_names - pkey).sort
|
28
|
+
end
|
29
|
+
|
26
30
|
def self.records
|
27
|
-
|
31
|
+
attributes_order = ordered_attributes
|
32
|
+
order(pkey).map do |record|
|
33
|
+
res = {}
|
34
|
+
attributes_order.each do |key|
|
35
|
+
res[key] = record[key]
|
36
|
+
end
|
37
|
+
res
|
38
|
+
end
|
28
39
|
end
|
29
40
|
end
|
data/lib/db/sync/version.rb
CHANGED
data/lib/tasks/db-sync.rake
CHANGED
@@ -2,12 +2,24 @@ namespace :db do
|
|
2
2
|
namespace :sync do
|
3
3
|
desc 'Download data from the databse into files.'
|
4
4
|
task down: :environment do
|
5
|
-
Db::Sync.
|
5
|
+
synchronizer = Db::Sync.new
|
6
|
+
synchronizer.sync_down
|
6
7
|
end
|
7
8
|
|
8
9
|
desc 'Upload data from the files into the database.'
|
9
10
|
task up: :environment do
|
10
|
-
|
11
|
+
commit = ENV['commit'] == 'true'
|
12
|
+
synchronizer = Db::Sync.new
|
13
|
+
sync_up_and_print(synchronizer, commit)
|
14
|
+
if !commit && synchronizer.log.present?
|
15
|
+
print "Commit Changes? [y/n]\n"
|
16
|
+
sync_up_and_print(synchronizer, true) if STDIN.gets.chomp == 'y'
|
17
|
+
end
|
11
18
|
end
|
12
19
|
end
|
13
20
|
end
|
21
|
+
|
22
|
+
def self.sync_up_and_print(synchronizer, commit)
|
23
|
+
synchronizer.sync_up(commit)
|
24
|
+
print synchronizer.log.join("\n") + "\n"
|
25
|
+
end
|