active_record_shards 3.11.3 → 3.11.4

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
- SHA1:
3
- metadata.gz: '09b0da024cd5b13020b14dde7e822530b7f82fcc'
4
- data.tar.gz: 0c2d9149aa653176d122c97fe632e6d47e00716a
2
+ SHA256:
3
+ metadata.gz: d4685600c17b38021c0601061601098ee26dd330684fd60bfb16bc40e41248a7
4
+ data.tar.gz: 1afec8f57736de6fd69c005fbfd4eb8bb513f0dab58bbe775fc6b8ea1103ad30
5
5
  SHA512:
6
- metadata.gz: 59ec6f7c8e6c8639beee8aea8ded4c0b8aa910b839c4f0350836077bc745692330a0106df273b7b78e46251c0b3a4fb0a27e6164a17475669eac8afea0444d5c
7
- data.tar.gz: dd24e476ad1c062427b54e5898f5a33393547acfeb1d1faf63024b4b900c2144672d2c67c2c42023225ef63b0e0c69c3722c188f320f6d50fc28e0773f356e21
6
+ metadata.gz: 240eae47a63fec982f10f381bf561b427ecbebb8b1ce744cdf364854b8660130c0d71491cb613e2f0f477a6bfab3b343593a1381ffbb826cccfa3533a09f6a8d
7
+ data.tar.gz: 7b54a60c3c822fd85c774220964d80f576f6749ddf8821d033dab35655ccc65804ade2e5679604bc24fe2c8bf449d9da05da05eb216eb4057739f9c626b99888
data/README.md CHANGED
@@ -7,6 +7,15 @@ switch between database connections. We've made the implementation very small, a
7
7
 
8
8
  ActiveRecord Shards has been used and tested on Rails 3.2, 4.2 and 5.0 and has in some form or another been used in production on a large Rails app for several years.
9
9
 
10
+ - [Installation](#installation)
11
+ - [Configuration](#configuration)
12
+ - [Migrations](#migrations)
13
+ - [Example](#example)
14
+ - [Shared Model](#create-a-table-for-the-shared-not-sharded-model)
15
+ - [Sharded Model](#create-a-table-for-the-sharded-model)
16
+ - [Usage](#usage)
17
+ - [Debugging](#debugging)
18
+
10
19
  ## Installation
11
20
 
12
21
  $ gem install active_record_shards
@@ -17,79 +26,133 @@ and make sure to require 'active\_record\_shards' in some way.
17
26
 
18
27
  Add the slave and shard configuration to config/database.yml:
19
28
 
20
- production:
21
- adapter: mysql
22
- encoding: utf8
23
- database: my_app_main
24
- pool: 5
25
- host: db1
26
- username: root
27
- password:
29
+ ```yaml
30
+ production:
31
+ adapter: mysql
32
+ encoding: utf8
33
+ database: my_app_main
34
+ pool: 5
35
+ host: db1
36
+ username: root
37
+ password:
38
+ slave:
39
+ host: db1_slave
40
+ shards:
41
+ 1:
42
+ host: db_shard1
43
+ database: my_app_shard
44
+ slave:
45
+ host: db_shard1_slave
46
+ 2:
47
+ host: db_shard2
48
+ database: my_app_shard
28
49
  slave:
29
- host: db1_slave
30
- shards:
31
- 1:
32
- host: db_shard1
33
- database: my_app_shard
34
- slave:
35
- host: db_shard1_slave
36
- 2:
37
- host: db_shard2
38
- database: my_app_shard
39
- slave:
40
- host: db_shard2_slave
50
+ host: db_shard2_slave
51
+ ```
41
52
 
42
53
  basically connections inherit configuration from the parent configuration file.
43
54
 
55
+ ## Migrations
56
+
57
+ ActiveRecord Shards also patches migrations to support running migrations on a shared (not sharded) or a sharded database.
58
+ Each migration class has to specify a shard spec indicating where to run the migration.
59
+
60
+ Valid shard specs:
61
+
62
+ * `:none` - Run this migration on the shared database, not any shards
63
+ * `:all` - Run this migration on all of the shards, not the shared database
64
+
65
+ #### Example
66
+
67
+ ###### Create a table for the shared (not sharded) model
68
+
69
+ ```ruby
70
+ class CreateAccounts < ActiveRecord::Migration
71
+ shard :none
72
+
73
+ def change
74
+ create_table :accounts do |t|
75
+ # This is NOT necessary for the gem to work, we just use it in the examples below demonstrating one way to switch shards
76
+ t.integer :shard_id, null: false
77
+
78
+ t.string :name
79
+ end
80
+ end
81
+ end
82
+ ```
83
+
84
+ ###### Create a table for the sharded model
85
+
86
+ ```ruby
87
+ class CreateProjects < ActiveRecord::Migration
88
+ shard :all
89
+
90
+ def change
91
+ create_table :projects do |t|
92
+ t.references :account
93
+ t.string :name
94
+ end
95
+ end
96
+ end
97
+ ```
98
+
44
99
  ## Usage
45
100
 
46
101
  Normally you have some models that live on a shared database, and you might need to query this data in order to know what shard to switch to.
47
102
  All the models that live on the shared database must be marked as not\_sharded:
48
103
 
49
- class Account < ActiveRecord::Base
50
- not_sharded
104
+ ```ruby
105
+ class Account < ActiveRecord::Base
106
+ not_sharded
51
107
 
52
- has_many :projects
53
- end
108
+ has_many :projects
109
+ end
54
110
 
55
- class Project < ActiveRecord::Base
56
- belongs_to :account
57
- end
111
+ class Project < ActiveRecord::Base
112
+ belongs_to :account
113
+ end
114
+ ```
58
115
 
59
116
  So in this setup the accounts live on the shared database, but the projects are sharded. If accounts have a shard\_id column, you could lookup the account
60
117
  in a rack middleware and switch to the right shard:
61
118
 
62
- class AccountMiddleware
63
- def initialize(app)
64
- @app = app
65
- end
119
+ ```ruby
120
+ class AccountMiddleware
121
+ def initialize(app)
122
+ @app = app
123
+ end
66
124
 
67
- def call(env)
68
- account = lookup_account(env)
125
+ def call(env)
126
+ account = lookup_account(env)
69
127
 
70
- if account
71
- ActiveRecord::Base.on_shard(account.shard_id) do
72
- @app.call(env)
73
- end
74
- else
75
- @app.call(env)
76
- end
77
- end
78
-
79
- def lookup_account(env)
80
- ...
128
+ if account
129
+ ActiveRecord::Base.on_shard(account.shard_id) do
130
+ @app.call(env)
81
131
  end
132
+ else
133
+ @app.call(env)
82
134
  end
135
+ end
136
+
137
+ def lookup_account(env)
138
+ # ...
139
+ end
140
+ end
141
+ ```
83
142
 
84
143
  You can switch to the slave databases at any point by wrapping your code in an on\_slave block:
85
144
 
86
- ActiveRecord::Base.on_slave do
87
- Account.find_by_big_expensive_query
88
- end
145
+ ```ruby
146
+ ActiveRecord::Base.on_slave do
147
+ Account.find_by_big_expensive_query
148
+ end
149
+ ```
89
150
 
90
151
  This will perform the query on the slave, and mark the returned instances as read only. There is also a shortcut for this:
91
152
 
92
- Account.on_slave.find_by_big_expensive_query
153
+ ```ruby
154
+ Account.on_slave.find_by_big_expensive_query
155
+ ```
93
156
 
94
157
  ## Debugging
95
158
 
@@ -22,7 +22,7 @@ module ActiveRecordShards
22
22
  spec = configurations[name]
23
23
 
24
24
  if spec.nil?
25
- raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.inspect})"
25
+ raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.keys.inspect})"
26
26
  end
27
27
 
28
28
  # in 3.2 rails is asking for a connection pool in a map of these ConnectionSpecifications. If we want to re-use connections,
@@ -4,7 +4,7 @@ module ActiveRecordShards
4
4
  name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations)
5
5
 
6
6
  unless configurations[name] || name == "primary"
7
- raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.inspect})"
7
+ raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.keys.inspect})"
8
8
  end
9
9
 
10
10
  name
@@ -4,7 +4,7 @@ module ActiveRecordShards
4
4
  name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations)
5
5
 
6
6
  unless configurations[name] || name == "primary"
7
- raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.inspect})"
7
+ raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.keys.inspect})"
8
8
  end
9
9
 
10
10
  name
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_shards
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.11.3
4
+ version: 3.11.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mick Staugaard
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-12-27 00:00:00.000000000 Z
13
+ date: 2018-04-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -229,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
229
229
  version: '0'
230
230
  requirements: []
231
231
  rubyforge_project:
232
- rubygems_version: 2.6.14
232
+ rubygems_version: 2.7.6
233
233
  signing_key:
234
234
  specification_version: 4
235
235
  summary: Simple database switching for ActiveRecord.