activerecord_snapshot_view 0.9.3 → 0.10.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.9.3
1
+ 0.10.0
@@ -30,6 +30,22 @@ module ActiveRecord
30
30
  end
31
31
  end
32
32
 
33
+ # prepare a SnapshotView model for migration... move all data to the
34
+ # base table, make that current, and remove the other store tables.
35
+ # if the model no longer exists, or is no longer a SnapshotView,
36
+ # catch any errors
37
+ def self.prepare_to_migrate(model)
38
+ klassname = model.to_s.singularize.camelize
39
+ begin
40
+ klass = eval(klassname)
41
+ if klass.ancestors.include?(ActiveRecord::SnapshotView)
42
+ klass.prepare_to_migrate
43
+ end
44
+ rescue
45
+ $stderr << "model: #{klassname} no longer exists?\n"
46
+ end
47
+ end
48
+
33
49
  # if a block given to the +new_version+ method throws this exception,
34
50
  # then the working table will still be made current
35
51
  class SaveWork < Exception
@@ -93,6 +109,18 @@ module ActiveRecord
93
109
  connection.execute(new_ct)
94
110
  end
95
111
 
112
+ # copy all data to base table, reset switch table and drop suffixed tables... for migration support
113
+ def prepare_to_migrate
114
+ if active_table_name != base_table_name
115
+ connection.execute( "truncate table #{base_table_name}" )
116
+ connection.execute( "insert into #{base_table_name} select * from #{active_table_name}" )
117
+ switch_to(base_table_name)
118
+ end
119
+ suffixed_table_names.each do |stn|
120
+ connection.execute( "drop table if exists #{stn}" )
121
+ end
122
+ end
123
+
96
124
  def ensure_version_table(name)
97
125
  if !connection.table_exists?(name) &&
98
126
  base_table_name!=name # don't execute ddl unless necessary
@@ -161,18 +189,21 @@ module ActiveRecord
161
189
  end
162
190
  ensure_switch_table
163
191
  end
164
-
165
- # make working table active, then recreate new working table from base table schema
166
- def advance_version
192
+
193
+ # update switch table to point to a different table
194
+ def switch_to(table)
167
195
  st = ensure_switch_table
168
-
169
196
  # want a transaction at least here [surround is ok too] so
170
197
  # there is never an empty switch table
171
198
  ActiveRecord::Base.transaction do
172
- wtn = working_table_name
173
199
  connection.execute( "delete from #{st}")
174
- connection.execute( "insert into #{st} values (\'#{wtn}\')")
200
+ connection.execute( "insert into #{st} values (\'#{table}\')")
175
201
  end
202
+ end
203
+
204
+ # make working table active, then recreate new working table from base table schema
205
+ def advance_version
206
+ switch_to(working_table_name)
176
207
 
177
208
  # ensure the presence of the new active and working tables.
178
209
  # happens after the switch table update, since this may commit a surrounding
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 9
8
- - 3
9
- version: 0.9.3
7
+ - 10
8
+ - 0
9
+ version: 0.10.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - mccraig mccraig of the clan mccraig
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-01 00:00:00 +01:00
17
+ date: 2010-06-10 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency