activerecord-shard_for 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +5 -15
- data/lib/activerecord/shard_for/model.rb +17 -3
- data/lib/activerecord/shard_for/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e7b2e6d1521f83ec993bccc75ed606b39792a9b
|
4
|
+
data.tar.gz: ba474a4e900eabfb0bd551da9514111b9322e6df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d5fbe559b15b32b65313380b4aa3d433ffcd01379f19fb756612d7c0f831cd9046279c37e876e5edddac23ae13c5de3bd0d0608a89bd71ff8594b655173f3c6
|
7
|
+
data.tar.gz: 3df865e864a0e7c39faebb0907630353dce1f2d0663747893f111574ea74b63f1b2d1deac38b2f449ed6c4a53d31ef9ab9236fded931dae3acd163160b511c9c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -12,19 +12,6 @@ This is Sharding Library for ActiveRecord, inspire and import codes from [mixed_
|
|
12
12
|
- small
|
13
13
|
- pluggable
|
14
14
|
|
15
|
-
### Simple
|
16
|
-
|
17
|
-
This idea principal take over from `mixed_gauge`, and `activerecord-sharding`. `activerecord-shard_for` needs minimum and simply of settings.
|
18
|
-
|
19
|
-
### Small
|
20
|
-
|
21
|
-
- Small Dependency: To facilitate version up (important!).
|
22
|
-
- Small code: Easy to read code when cause trouble.
|
23
|
-
|
24
|
-
### pluggable
|
25
|
-
|
26
|
-
Default sharding rule is [modulo with hashed key](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/hash_modulo_router.rb). But, `activerecord-shard_for` adopt pluggable structer. This rule can be easy to change!
|
27
|
-
|
28
15
|
## Installation
|
29
16
|
|
30
17
|
Add this line to your application's Gemfile:
|
@@ -43,8 +30,6 @@ Or install it yourself as:
|
|
43
30
|
|
44
31
|
# Getting Started
|
45
32
|
|
46
|
-
More example to see [wiki](https://github.com/yuemori/activerecord-shard_for/wiki).
|
47
|
-
|
48
33
|
Add additional database connection config to database.yml.
|
49
34
|
|
50
35
|
```yaml
|
@@ -105,6 +90,11 @@ alice.save!
|
|
105
90
|
User.all_shards.flat_map {|m| m.find_by(name: 'alice') }.compact
|
106
91
|
```
|
107
92
|
|
93
|
+
## Wiki
|
94
|
+
|
95
|
+
More imformation and example to see [wiki](https://github.com/yuemori/activerecord-shard_for/wiki)!
|
96
|
+
|
97
|
+
|
108
98
|
## Contributing with ActiveRecord::ShardFor
|
109
99
|
|
110
100
|
Contributors are welcome! This is what you need to setup your Octopus development environment:
|
@@ -50,11 +50,11 @@ module ActiveRecord
|
|
50
50
|
# @raise [ActiveRecord::ShardFor::MissingDistkeyAttribute]
|
51
51
|
def put!(attributes)
|
52
52
|
raise '`distkey` is not defined. Use `def_distkey`.' unless distkey
|
53
|
-
|
54
53
|
@before_put_callback.call(attributes) if defined?(@before_put_callback) && @before_put_callback
|
55
|
-
key = attributes[distkey]
|
56
54
|
|
57
|
-
|
55
|
+
key = fetch_distkey_from_attributes(attributes)
|
56
|
+
|
57
|
+
raise ActiveRecord::ShardFor::MissingDistkeyAttribute unless key
|
58
58
|
|
59
59
|
shard_for(key).create!(attributes)
|
60
60
|
end
|
@@ -139,6 +139,20 @@ module ActiveRecord
|
|
139
139
|
def switch(role_name, &block)
|
140
140
|
replication_mapping.switch(self, role_name, &block)
|
141
141
|
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
# @param [Hash] attributes
|
146
|
+
# @return [Object or nil] distkey
|
147
|
+
def fetch_distkey_from_attributes(attributes)
|
148
|
+
key = attributes[distkey] || attributes[distkey.to_s]
|
149
|
+
return key if key
|
150
|
+
|
151
|
+
instance = all_shards.first.new(attributes)
|
152
|
+
return unless instance.respond_to?(distkey)
|
153
|
+
|
154
|
+
instance.send(distkey)
|
155
|
+
end
|
142
156
|
end
|
143
157
|
end
|
144
158
|
end
|