activerecord_worm_table 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,13 +12,14 @@ model methods, like finders, work transparently
12
12
 
13
13
  to use, just <tt>include ActiveRecord::WormTable</tt> into your model :
14
14
 
15
- class Foo \< ActiveRecord::Base
15
+ class Foo < ActiveRecord::Base
16
16
  include ActiveRecord::WormTable
17
17
  end
18
18
 
19
19
  there are class methods to get the different table names, and manipulate the version :
20
20
 
21
21
  * <tt>Foo.table_name</tt> : current active table name
22
+ * <tt>Foo.advance_version(&block)</tt> : run +block+ to create a new version of the table. the working table will appear active to the +Thread+ running +block+ , and if +block+ completes then the working table will become permanently globally active
22
23
  * <tt>Foo.working_table_name</tt> : current working table name
23
24
  * <tt>Foo.advance_version</tt> : make active table historical, working table active and create a new working table
24
25
 
@@ -43,7 +44,7 @@ operations too limited, to easily make it generic. wouldn't be too hard to exten
43
44
  future version unintentionally.
44
45
  * Commit, do not mess with rakefile, version, or history.
45
46
  (if you want to have your own version, that is fine but
46
- bump version in a commit by itself I can ignore when I pull)
47
+ bump version in a commit by itself I can ignore when I pull)
47
48
  * Send me a pull request. Bonus points for topic branches.
48
49
 
49
50
  == Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
@@ -26,10 +26,24 @@ module ActiveRecord
26
26
  def ClassMethods.included(mod)
27
27
  mod.instance_eval do
28
28
  alias_method :org_table_name, :table_name
29
- alias_method :table_name, :active_table_name
29
+ alias_method :table_name, :active_working_table_or_active_table_name
30
30
  end
31
31
  end
32
32
 
33
+ # number of historical tables to keep around for posterity, or more likely
34
+ # to ensure running transactions aren't taken down by advance_version
35
+ # recreating a table. default 2
36
+ def historical_version_count
37
+ @historical_version_count || 2
38
+ end
39
+
40
+ # set the number of historical tables to keep around to ensure running
41
+ # transactions aren't interrupted by truncating working tables. 2 is default
42
+ def set_historical_version_count(count)
43
+ @historical_version_count = count
44
+ end
45
+ alias :historical_version_count= :set_historical_version_count
46
+
33
47
  # hide the ActiveRecord::Base method, which redefines a table_name method,
34
48
  # and instead capture the given name as the base_table_name
35
49
  def set_table_name(name)
@@ -40,25 +54,14 @@ module ActiveRecord
40
54
  def base_table_name
41
55
  if !@base_table_name
42
56
  @base_table_name = org_table_name
57
+ # the original table_name method re-aliases itself !
43
58
  class << self
44
- alias_method :table_name, :active_table_name
59
+ alias_method :table_name, :active_working_table_or_active_table_name
45
60
  end
46
61
  end
47
62
  @base_table_name
48
63
  end
49
64
 
50
- # number of historical tables to keep around for posterity, or more likely
51
- # to ensure running transactions aren't taken down by advance_version
52
- # recreating a table
53
- def historical_version_count
54
- @historical_version_count || 2
55
- end
56
-
57
- def set_historical_version_count(count)
58
- @historical_version_count = count
59
- end
60
- alias :historical_version_count= :set_historical_version_count
61
-
62
65
  # use schema of from table to recreate to table
63
66
  def dup_table_schema(from, to)
64
67
  connection.execute( "drop table if exists #{to}")
@@ -121,7 +124,7 @@ module ActiveRecord
121
124
  end
122
125
  end
123
126
 
124
- # name of the active table
127
+ # name of the active table read direct from db
125
128
  def active_table_name
126
129
  st = switch_table_name
127
130
  begin
@@ -168,7 +171,37 @@ module ActiveRecord
168
171
  else
169
172
  connection.execute( "truncate table #{new_wtn}" )
170
173
  end
174
+ end
175
+
176
+ def thread_local_key_name
177
+ "ActiveRecord::WormTable::" + self.to_s
178
+ end
171
179
 
180
+ def active_working_table_name
181
+ Thread.current[thread_local_key_name]
182
+ end
183
+
184
+ def active_working_table_name=(name)
185
+ Thread.current[thread_local_key_name] = name
186
+ end
187
+
188
+ # name of the active table, or the working table if inside a new_version block
189
+ def active_working_table_or_active_table_name
190
+ active_working_table_name || active_table_name
191
+ end
192
+
193
+ # make the working table temporarily active [ for this thread only ],
194
+ # execute the block, and if completed without exception then
195
+ # make the working table permanently active
196
+ def new_version(&block)
197
+ begin
198
+ self.active_working_table_name = working_table_name
199
+ r = block.call
200
+ advance_version
201
+ r
202
+ ensure
203
+ self.active_working_table_name = nil
204
+ end
172
205
  end
173
206
  end
174
207
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_worm_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mccraig mccraig of the clan mccraig
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-05 00:00:00 +00:00
12
+ date: 2009-11-12 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency