acts_as_shardable 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a9466ea6e8a41550e3841d5c102b3b4d0e8eedb
4
- data.tar.gz: 9435090d2a582ed16af98ddefaebfd7e856ca8ce
3
+ metadata.gz: 682f5dff4f95776b0c2a8d19e67d7521de209bb0
4
+ data.tar.gz: 9f1478defcf88c299bdba64aa6fec066c9b55bfe
5
5
  SHA512:
6
- metadata.gz: cd4e8d246d4275f0de40694b306c6a1daaeec81a4f93ad6096dc8791efc9ca1db8d6fba65beeefe6a031fe9791f0029fcdcb9aa5b950ebcd31795b80b0ea90d9
7
- data.tar.gz: 00ca76beffcd57f220e69409020bc5d3accfd6de3f4e1f317acdfc7717ce421b32826c8bd4ae05a6872e7bea39535f737f63bda026c96eafd14883dbe52fa176
6
+ metadata.gz: 4acf829b64e9391ded87d396a69daf4c20569850d76f51e967f6de2b8ab0acb22df8a74d42201030ac21330539838087f99ca78ba3bec3003ce1d633b6f30cb3
7
+ data.tar.gz: e1cc62c238ab080f94d802443289047d674222df0f871087d926e0095222bfa0c243f696d4467161565599d6277cd4e1c14ef1671c19180ea5c2c8eab7c3df4d
data/README.md CHANGED
@@ -42,7 +42,7 @@ class CreateMod2Models < ActiveRecord::Migration
42
42
 
43
43
  def self.down
44
44
  shards.times do |i|
45
- drop_table("cc_study_durations_%04d" % i)
45
+ drop_table("mod2_models_%04d" % i)
46
46
  end
47
47
  end
48
48
 
@@ -24,37 +24,92 @@ module ActsAsShardable
24
24
  # Updates the associated record with values matching those of the instance attributes.
25
25
  # Returns the number of affected rows.
26
26
  define_method :_update_record do |attribute_names = self.attribute_names|
27
- attributes_values = arel_attributes_with_values_for_update(attribute_names)
28
- if attributes_values.empty?
29
- 0
27
+
28
+ was, is = changes[self.class.base_class.shard_method]
29
+
30
+ if was
31
+ # shard_column changed
32
+ table_was = self.class.base_class.sharding(was).table_name
33
+ table_is = self.class.base_class.sharding(is).table_name
34
+ raise WrongShardingError, "Please move from #{table_was} to #{table_is} manually."
30
35
  else
31
- was, is = changes[self.class.base_class.shard_method]
32
- if was
33
- transaction do
34
- self.class.base_class.sharding(is).unscoped.insert attributes_values
35
-
36
- raise ReadOnlyRecord, "#{self.class.base_class.sharding(was)} is marked as readonly" if readonly?
37
- destroy_associations
38
- destroy_row if persisted?
39
- @destroyed = true
40
- freeze
41
- end
36
+ # shard_column not changing
37
+ if locking_enabled?
38
+ lock_col = self.class.base_class.locking_column
39
+ previous_lock_value = self[lock_col]
40
+ self[lock_col] = previous_lock_value + 1
41
+
42
+ attribute_names += [lock_col].compact
43
+ attribute_names.uniq!
44
+
45
+ begin
46
+ relation = self.class.base_class.sharding(is).unscoped
47
+
48
+ affected_rows = relation.where(
49
+ self.class.primary_key => id,
50
+ lock_col => previous_lock_value,
51
+ ).update_all(
52
+ Hash[attributes_for_update(attribute_names).map do |name|
53
+ [name, _read_attribute(name)]
54
+ end]
55
+ )
56
+
57
+ unless affected_rows == 1
58
+ raise ActiveRecord::StaleObjectError.new(self, "update")
59
+ end
60
+
61
+ affected_rows
42
62
 
43
- 1
63
+ # If something went wrong, revert the version.
64
+ rescue Exception
65
+ send(lock_col + '=', previous_lock_value)
66
+ raise
67
+ end
44
68
  else
45
- self.class.base_class.sharding(public_send(self.class.base_class.shard_method)).unscoped._update_record attributes_values, id, id_was
69
+ attributes_values = arel_attributes_with_values_for_update(attribute_names)
70
+ if attributes_values.empty?
71
+ 0
72
+ else
73
+ shard.unscoped._update_record attributes_values, id, id_was
74
+ end
46
75
  end
47
76
  end
48
77
  end
49
78
 
50
79
  private :_update_record
51
80
 
81
+
82
+ define_method :touch do |*names|
83
+ raise ActiveRecordError, "cannot touch on a new record object" unless persisted?
84
+
85
+ attributes = timestamp_attributes_for_update_in_model
86
+ attributes.concat(names)
87
+
88
+ unless attributes.empty?
89
+ current_time = current_time_from_proper_timezone
90
+ changes = {}
91
+
92
+ attributes.each do |column|
93
+ column = column.to_s
94
+ changes[column] = write_attribute(column, current_time)
95
+ end
96
+
97
+ changes[self.class.locking_column] = increment_lock if locking_enabled?
98
+
99
+ clear_attribute_changes(changes.keys)
100
+ primary_key = self.class.primary_key
101
+ shard.unscoped.where(primary_key => self[primary_key]).update_all(changes) == 1
102
+ else
103
+ true
104
+ end
105
+ end
106
+
52
107
  # Creates a record with values matching those of the instance attributes
53
108
  # and returns its id.
54
109
  define_method :_create_record do |attribute_names = self.attribute_names|
55
110
  attributes_values = arel_attributes_with_values_for_create(attribute_names)
56
111
 
57
- new_id = self.class.base_class.sharding(public_send(self.class.base_class.shard_method)).unscoped.insert attributes_values
112
+ new_id = shard.unscoped.insert attributes_values
58
113
  self.id ||= new_id if self.class.base_class.primary_key
59
114
 
60
115
  @new_record = false
@@ -63,6 +118,12 @@ module ActsAsShardable
63
118
 
64
119
  private :_create_record
65
120
 
121
+ define_method :shard do
122
+ self.class.base_class.sharding(self[self.class.base_class.shard_method])
123
+ end
124
+
125
+ private :shard
126
+
66
127
  self.class.send :define_method, :sharding do |column|
67
128
  i = column.to_i % shard_mod
68
129
  klass = "#{base_class.name.demodulize}_%04d" % i
@@ -81,7 +142,8 @@ module ActsAsShardable
81
142
  self.const_get('ActiveRecord_Relation').class_exec do
82
143
  def to_proto(*args)
83
144
  msg_class = base_class.name.demodulize.pluralize
84
- Messages.const_get(msg_class).new(msg_class.underscore => map { |r| r.to_proto(*args) })
145
+ module_name = base_class.name.deconstantize.constantize
146
+ module_name::Messages.const_get(msg_class).new(msg_class.underscore => map { |r| r.to_proto(*args) })
85
147
  end
86
148
  end
87
149
  end
@@ -90,7 +152,12 @@ module ActsAsShardable
90
152
  end
91
153
  end
92
154
  end
155
+
156
+ define_method :real do
157
+ self.class.sharding(self[self.class.base_class.shard_method]).find(id)
158
+ end
93
159
  end
160
+
94
161
  end
95
162
 
96
163
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsShardable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_shardable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - scorix
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-13 00:00:00.000000000 Z
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler