sequel_bitemporal 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -77,8 +77,11 @@ module Sequel
77
77
  valid_from.to_time<=n &&
78
78
  valid_to.to_time>n
79
79
  end
80
- def destroy
81
- master.destroy_version self
80
+ def destroy(opts={})
81
+ expand_previous_version = opts.fetch(:expand_previous_version){
82
+ valid_from.to_time>::Sequel::Plugins::Bitemporal.now
83
+ }
84
+ master.destroy_version self, expand_previous_version
82
85
  end
83
86
  end
84
87
  unless opts[:delegate]==false
@@ -173,20 +176,27 @@ module Sequel
173
176
  versions_dataset.where(expired_at: nil).where("valid_to>valid_from").update expired_at: Time.now
174
177
  end
175
178
 
176
- def destroy_version(version)
179
+ def destroy_version(version, expand_previous_version)
177
180
  point_in_time = Time.now
178
181
  return false if version.valid_to.to_time<=point_in_time
179
182
  model.db.transaction do
180
183
  success = true
181
- previous = versions_dataset.where({
182
- expired_at: nil,
183
- valid_to: version.valid_from,
184
- }).where("valid_to>valid_from").first
185
- if previous
186
- success &&= save_fossil previous, created_at: point_in_time, valid_to: version.valid_to
187
- success &&= previous.update expired_at: point_in_time
184
+ version_was_valid = point_in_time>=version.valid_from.to_time
185
+ if expand_previous_version
186
+ previous = versions_dataset.where({
187
+ expired_at: nil,
188
+ valid_to: version.valid_from,
189
+ }).where("valid_to>valid_from").first
190
+ if previous
191
+ if version_was_valid
192
+ success &&= save_fossil previous, created_at: point_in_time, valid_from: point_in_time, valid_to: version.valid_to
193
+ else
194
+ success &&= save_fossil previous, created_at: point_in_time, valid_to: version.valid_to
195
+ success &&= previous.update expired_at: point_in_time
196
+ end
197
+ end
188
198
  end
189
- success &&= save_fossil version, created_at: point_in_time, valid_to: point_in_time if point_in_time>=version.valid_from.to_time
199
+ success &&= save_fossil version, created_at: point_in_time, valid_to: point_in_time if version_was_valid
190
200
  success &&= version.update expired_at: point_in_time
191
201
  raise Sequel::Rollback unless success
192
202
  success
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "sequel_bitemporal"
6
- s.version = "0.4.7"
6
+ s.version = "0.4.8"
7
7
  s.authors = ["Joseph HALTER", "Jonathan TRON"]
8
8
  s.email = ["joseph.halter@thetalentbox.com", "jonathan.tron@thetalentbox.com"]
9
9
  s.homepage = "https://github.com/TalentBox/sequel_bitemporal"
@@ -197,18 +197,37 @@ describe "Sequel::Plugins::Bitemporal" do
197
197
  end
198
198
  it "allows deleting current version" do
199
199
  master = @master_class.new
200
+ master.update_attributes name: "Single Standard", price: 92, valid_from: Date.today-2, valid_to: Date.today
200
201
  master.update_attributes name: "Single Standard", price: 98
201
202
  master.update_attributes name: "Single Standard", price: 94, valid_from: Date.today+2
202
203
  Timecop.freeze Date.today+1
203
204
  master.current_version.destroy.should be_true
204
205
  master.should have_versions %Q{
205
206
  | name | price | created_at | expired_at | valid_from | valid_to | current |
207
+ | Single Standard | 92 | 2009-11-28 | | 2009-11-26 | 2009-11-28 | |
206
208
  | Single Standard | 98 | 2009-11-28 | 2009-11-28 | 2009-11-28 | MAX DATE | |
207
209
  | Single Standard | 98 | 2009-11-28 | 2009-11-29 | 2009-11-28 | 2009-11-30 | |
208
210
  | Single Standard | 94 | 2009-11-28 | | 2009-11-30 | MAX DATE | |
209
211
  | Single Standard | 98 | 2009-11-29 | | 2009-11-28 | 2009-11-29 | |
210
212
  }
211
213
  end
214
+ it "allows deleting current version to restore the previous one" do
215
+ master = @master_class.new
216
+ master.update_attributes name: "Single Standard", price: 92, valid_from: Date.today-2, valid_to: Date.today
217
+ master.update_attributes name: "Single Standard", price: 98
218
+ master.update_attributes name: "Single Standard", price: 94, valid_from: Date.today+2
219
+ Timecop.freeze Date.today+1
220
+ master.current_version.destroy(expand_previous_version: true).should be_true
221
+ master.should have_versions %Q{
222
+ | name | price | created_at | expired_at | valid_from | valid_to | current |
223
+ | Single Standard | 92 | 2009-11-28 | | 2009-11-26 | 2009-11-28 | |
224
+ | Single Standard | 98 | 2009-11-28 | 2009-11-28 | 2009-11-28 | MAX DATE | |
225
+ | Single Standard | 98 | 2009-11-28 | 2009-11-29 | 2009-11-28 | 2009-11-30 | |
226
+ | Single Standard | 94 | 2009-11-28 | | 2009-11-30 | MAX DATE | |
227
+ | Single Standard | 92 | 2009-11-29 | | 2009-11-29 | 2009-11-30 | true |
228
+ | Single Standard | 98 | 2009-11-29 | | 2009-11-28 | 2009-11-29 | |
229
+ }
230
+ end
212
231
  it "allows deleting a future version" do
213
232
  master = @master_class.new
214
233
  master.update_attributes name: "Single Standard", price: 98
@@ -223,6 +242,19 @@ describe "Sequel::Plugins::Bitemporal" do
223
242
  | Single Standard | 98 | 2009-11-29 | | 2009-11-28 | MAX DATE | true |
224
243
  }
225
244
  end
245
+ it "allows deleting a future version without expanding the current one" do
246
+ master = @master_class.new
247
+ master.update_attributes name: "Single Standard", price: 98
248
+ master.update_attributes name: "Single Standard", price: 94, valid_from: Date.today+2
249
+ Timecop.freeze Date.today+1
250
+ master.versions.last.destroy(expand_previous_version: false).should be_true
251
+ master.should have_versions %Q{
252
+ | name | price | created_at | expired_at | valid_from | valid_to | current |
253
+ | Single Standard | 98 | 2009-11-28 | 2009-11-28 | 2009-11-28 | MAX DATE | |
254
+ | Single Standard | 98 | 2009-11-28 | | 2009-11-28 | 2009-11-30 | true |
255
+ | Single Standard | 94 | 2009-11-28 | 2009-11-29 | 2009-11-30 | MAX DATE | |
256
+ }
257
+ end
226
258
  it "allows deleting all versions" do
227
259
  master = @master_class.new
228
260
  master.update_attributes name: "Single Standard", price: 98
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_bitemporal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-12-17 00:00:00.000000000 Z
13
+ date: 2012-01-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sequel
17
- requirement: &70102092111400 !ruby/object:Gem::Requirement
17
+ requirement: &2160168600 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 3.30.0
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70102092111400
25
+ version_requirements: *2160168600
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: sqlite3
28
- requirement: &70102092109880 !ruby/object:Gem::Requirement
28
+ requirement: &2160167460 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70102092109880
36
+ version_requirements: *2160167460
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &70102092127900 !ruby/object:Gem::Requirement
39
+ requirement: &2160166300 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 2.8.0.rc1
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70102092127900
47
+ version_requirements: *2160166300
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: timecop
50
- requirement: &70102092125440 !ruby/object:Gem::Requirement
50
+ requirement: &2160165300 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70102092125440
58
+ version_requirements: *2160165300
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rake
61
- requirement: &70102092123640 !ruby/object:Gem::Requirement
61
+ requirement: &2160164540 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,7 +66,7 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *70102092123640
69
+ version_requirements: *2160164540
70
70
  description: Bitemporal versioning for sequel, fully tested.
71
71
  email:
72
72
  - joseph.halter@thetalentbox.com
@@ -102,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
102
  version: '0'
103
103
  segments:
104
104
  - 0
105
- hash: -2437842052147877954
105
+ hash: -1570782327247228228
106
106
  required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  none: false
108
108
  requirements:
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  segments:
113
113
  - 0
114
- hash: -2437842052147877954
114
+ hash: -1570782327247228228
115
115
  requirements: []
116
116
  rubyforge_project:
117
117
  rubygems_version: 1.8.10