nsync 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
@@ -80,6 +80,17 @@ module Nsync
80
80
  Nsync.config
81
81
  end
82
82
 
83
+ def latest_changes
84
+ update_repo &&
85
+ changes(config.version_manager.version,
86
+ repo.head.commit.id)
87
+ end
88
+
89
+ def changes(a, b)
90
+ diffs = repo.diff(a,b)
91
+ changeset_from_diffs(diffs)
92
+ end
93
+
83
94
  # Translates and applies the changes between commit id 'a' and commit id 'b' to
84
95
  # the datastore. This is used internally by rollback and update. Don't use this
85
96
  # unless you absolutely know what you are doing.
@@ -91,6 +102,7 @@ module Nsync
91
102
  # @param [String] a current data version commit id
92
103
  # @param [String] b new data version commit id
93
104
  def apply_changes(a, b)
105
+ return false if a == b
94
106
  config.lock do
95
107
  config.log.info("[NSYNC] Moving Nsync::Consumer from '#{a}' to '#{b}'")
96
108
  clear_queues
@@ -125,6 +137,19 @@ module Nsync
125
137
  end
126
138
  end
127
139
 
140
+ # Reprocesses all changes from the start of the repo to the current version
141
+ # for the class klass, queues will not be cleared, so you can use this to
142
+ # do powerful data reconstruction. You can also shoot your foot off. Be
143
+ # very careful
144
+ def reprocess_class!(klass)
145
+ diffs = repo.diff(first_commit, config.version_manager.version)
146
+ changeset = changeset_from_diffs(diffs)
147
+
148
+ changes = changeset[klass]
149
+ if changes
150
+ apply_changes_for_class(klass, changes)
151
+ end
152
+ end
128
153
 
129
154
  # @private
130
155
  class Change < Struct.new(:id, :diff)
@@ -199,6 +224,12 @@ module Nsync
199
224
  line.split(/\s+/)
200
225
  end
201
226
  end
227
+
228
+ # Gets the first commit id in the repo
229
+ def first_commit
230
+ self.repo.git.rev_list({:reverse => true}, "master").split("\n").first
231
+ end
232
+
202
233
  protected
203
234
  def get_or_create_repo
204
235
  if config.local? || File.exists?(config.repo_path)
@@ -209,7 +240,7 @@ module Nsync
209
240
  git = Grit::Git.new(config.repo_path)
210
241
  git.clone({:bare => true}, config.repo_url, config.repo_path)
211
242
  self.repo = Grit::Repo.new(config.repo_path)
212
- config.version_manager.version = git.rev_list({:reverse => true}, "master").split("\n").first
243
+ config.version_manager.version = first_commit
213
244
  return self.repo
214
245
  end
215
246
  end
@@ -58,6 +58,19 @@ module Nsync
58
58
  config.log.info("[NSYNC] Removed file '#{filename}'")
59
59
  end
60
60
 
61
+ def latest_changes
62
+ diff = repo.git.native('diff', {:full_index => true, :cached => true})
63
+
64
+ if diff =~ /diff --git a/
65
+ diff = diff.sub(/.*?(diff --git a)/m, '\1')
66
+ else
67
+ diff = ''
68
+ end
69
+
70
+ diffs = Grit::Diff.list_from_string(repo, diff)
71
+ changeset_from_diffs(diffs)
72
+ end
73
+
61
74
  # Commits and pushes the current changeset
62
75
  def commit(message="Friendly data update")
63
76
  config.lock do
data/nsync.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{nsync}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Hughes"]
12
- s.date = %q{2011-01-05}
12
+ s.date = %q{2011-01-08}
13
13
  s.description = %q{Nsync is designed to allow you to have a separate data
14
14
  processing app with its own data processing optimized database and a consumer
15
15
  app with its own database, while keeping the data as in sync as you want it.}
@@ -87,32 +87,28 @@ class NsyncConsumerTest < Test::Unit::TestCase
87
87
  end
88
88
  end
89
89
 
90
- [['local disk', nil],
91
- ['github', 'git://github.com/schleyfox/test_repo.git']].each do |name, url|
90
+ context "where the origin does exist" do
91
+ setup do
92
+ @repo = TestRepo.new
93
+ FileUtils.rm_rf @repo.bare_consumer_repo_path
92
94
 
93
- context "where the origin does exist on #{name}" do
94
- setup do
95
- @repo = TestRepo.new
96
- FileUtils.rm_rf @repo.bare_consumer_repo_path
97
-
98
- Nsync.config.repo_url = url || @repo.repo_path
99
- Nsync.config.repo_path = @repo.bare_consumer_repo_path
100
-
101
- @consumer = Nsync::Consumer.new
102
- end
103
-
104
- should "assign repo to a new bare repo" do
105
- assert @consumer.repo.is_a? Grit::Repo
106
- assert @consumer.repo.bare
107
- end
108
-
109
- should "set the origin remote" do
110
- origin = @consumer.remotes.detect{|r| r[0] == "origin" && r[2] == "(fetch)" }
111
- assert origin
112
- assert_equal Nsync.config.repo_url, origin[1]
113
- end
95
+ Nsync.config.repo_url = @repo.repo_path
96
+ Nsync.config.repo_path = @repo.bare_consumer_repo_path
97
+
98
+ @consumer = Nsync::Consumer.new
99
+ end
100
+
101
+ should "assign repo to a new bare repo" do
102
+ assert @consumer.repo.is_a? Grit::Repo
103
+ assert @consumer.repo.bare
104
+ end
105
+
106
+ should "set the origin remote" do
107
+ origin = @consumer.remotes.detect{|r| r[0] == "origin" && r[2] == "(fetch)" }
108
+ assert origin
109
+ assert_equal Nsync.config.repo_url, origin[1]
114
110
  end
115
- end
111
+ end
116
112
  end
117
113
  end
118
114
 
@@ -301,4 +297,32 @@ class NsyncConsumerTest < Test::Unit::TestCase
301
297
  end
302
298
  end
303
299
  end
300
+
301
+ context "Reprocessing all data for a class" do
302
+ setup do
303
+ @repo = TestRepo.new
304
+ Nsync.config.version_manager.stubs(:version => "current version")
305
+ FileUtils.rm_rf @repo.bare_consumer_repo_path
306
+
307
+ Nsync.config.repo_url = @repo.repo_path
308
+ Nsync.config.repo_path = @repo.bare_consumer_repo_path
309
+
310
+ @consumer = Nsync::Consumer.new
311
+ end
312
+
313
+ should "get changes from initial revision to current version and apply only for the class" do
314
+ changeset = {NsyncTestFoo => ["changes"], NsyncTestBar => ["changes"]}
315
+
316
+ @consumer.expects(:first_commit).returns("first version").once
317
+
318
+ @consumer.repo.expects(:diff).with("first version", "current version").returns(:diffs).once
319
+ @consumer.expects(:changeset_from_diffs).with(:diffs).returns(changeset).once
320
+
321
+ @consumer.expects(:apply_changes_for_class).with(NsyncTestBar, is_a(Array)).never
322
+ @consumer.expects(:apply_changes_for_class).with(NsyncTestFoo, ["changes"]).once
323
+
324
+
325
+ @consumer.reprocess_class!(NsyncTestFoo)
326
+ end
327
+ end
304
328
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 3
9
- version: 0.0.3
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Hughes
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-05 00:00:00 -05:00
17
+ date: 2011-01-08 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency