acts_as_shardable 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +50 -6
- data/Rakefile +10 -1
- data/lib/acts_as_shardable.rb +7 -7
- data/lib/acts_as_shardable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a9466ea6e8a41550e3841d5c102b3b4d0e8eedb
|
4
|
+
data.tar.gz: 9435090d2a582ed16af98ddefaebfd7e856ca8ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd4e8d246d4275f0de40694b306c6a1daaeec81a4f93ad6096dc8791efc9ca1db8d6fba65beeefe6a031fe9791f0029fcdcb9aa5b950ebcd31795b80b0ea90d9
|
7
|
+
data.tar.gz: 00ca76beffcd57f220e69409020bc5d3accfd6de3f4e1f317acdfc7717ce421b32826c8bd4ae05a6872e7bea39535f737f63bda026c96eafd14883dbe52fa176
|
data/README.md
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# ActsAsShardable
|
2
2
|
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/acts_as_shardable`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
3
|
## Installation
|
8
4
|
|
9
5
|
Add this line to your application's Gemfile:
|
@@ -22,13 +18,61 @@ Or install it yourself as:
|
|
22
18
|
|
23
19
|
## Usage
|
24
20
|
|
25
|
-
|
21
|
+
Example
|
22
|
+
|
23
|
+
In your model:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Mod2Model < ActiveRecord::Base
|
27
|
+
acts_as_shardable by: :hash_id, mod: 2
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
and migrations:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class CreateMod2Models < ActiveRecord::Migration
|
35
|
+
def self.up
|
36
|
+
shards.times do |i|
|
37
|
+
create_table("mod2_models_%04d" % i) do |t|
|
38
|
+
t.integer :hash_id
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.down
|
44
|
+
shards.times do |i|
|
45
|
+
drop_table("cc_study_durations_%04d" % i)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.shards
|
50
|
+
2
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Then if you call
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
Mod2Model.create(hash: 1)
|
59
|
+
```
|
60
|
+
|
61
|
+
it will save the record into `mod2_models_0001`.
|
62
|
+
|
63
|
+
And
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
Mod2Model.create(hash: 2)
|
67
|
+
```
|
68
|
+
|
69
|
+
will save the record into `mod2_models_0002`.
|
26
70
|
|
27
71
|
## Development
|
28
72
|
|
29
73
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
74
|
|
31
|
-
|
75
|
+
run `rake test` to run tests.
|
32
76
|
|
33
77
|
## Contributing
|
34
78
|
|
data/Rakefile
CHANGED
data/lib/acts_as_shardable.rb
CHANGED
@@ -10,16 +10,16 @@ module ActsAsShardable
|
|
10
10
|
Mutex.new
|
11
11
|
end
|
12
12
|
|
13
|
-
def acts_as_shardable(
|
14
|
-
class_attribute :
|
13
|
+
def acts_as_shardable(by:, mod:)
|
14
|
+
class_attribute :shard_method, :shard_mod, :module_name, :base_table_name
|
15
15
|
|
16
16
|
mutex.synchronize do
|
17
|
-
self.
|
17
|
+
self.shard_method = by
|
18
18
|
self.shard_mod = mod
|
19
19
|
self.module_name = self.name.deconstantize.safe_constantize || Object
|
20
20
|
self.base_table_name = self.name.demodulize.pluralize.underscore
|
21
21
|
self.table_name = "#{self.base_table_name}_0000"
|
22
|
-
self.validates self.
|
22
|
+
self.validates self.shard_method.to_sym, presence: true
|
23
23
|
|
24
24
|
# Updates the associated record with values matching those of the instance attributes.
|
25
25
|
# Returns the number of affected rows.
|
@@ -28,7 +28,7 @@ module ActsAsShardable
|
|
28
28
|
if attributes_values.empty?
|
29
29
|
0
|
30
30
|
else
|
31
|
-
was, is = changes[self.class.base_class.
|
31
|
+
was, is = changes[self.class.base_class.shard_method]
|
32
32
|
if was
|
33
33
|
transaction do
|
34
34
|
self.class.base_class.sharding(is).unscoped.insert attributes_values
|
@@ -42,7 +42,7 @@ module ActsAsShardable
|
|
42
42
|
|
43
43
|
1
|
44
44
|
else
|
45
|
-
self.class.base_class.sharding(self
|
45
|
+
self.class.base_class.sharding(public_send(self.class.base_class.shard_method)).unscoped._update_record attributes_values, id, id_was
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -54,7 +54,7 @@ module ActsAsShardable
|
|
54
54
|
define_method :_create_record do |attribute_names = self.attribute_names|
|
55
55
|
attributes_values = arel_attributes_with_values_for_create(attribute_names)
|
56
56
|
|
57
|
-
new_id = self.class.base_class.sharding(self
|
57
|
+
new_id = self.class.base_class.sharding(public_send(self.class.base_class.shard_method)).unscoped.insert attributes_values
|
58
58
|
self.id ||= new_id if self.class.base_class.primary_key
|
59
59
|
|
60
60
|
@new_record = false
|
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.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2016-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|