fresh_connection 3.0.0.rc2 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Appraisals +2 -2
- data/README.md +43 -95
- data/gemfiles/rails52.gemfile +2 -2
- data/lib/fresh_connection/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b67bc1788b59977212ab408786a2c50de89e59834dcb7eabfd45fd5a8b556aa
|
4
|
+
data.tar.gz: '0555593d5a5e54322e767ea4475d86263ea87c48d2aa618156ace835e5881389'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e20fade2abed85117b4dc3e55c7188ee85cdf723a0a65a270f3b53105812ff416d0d4afe94a366f87fd24dc51352d332d766b1a7622d974ec19426b0008e4087
|
7
|
+
data.tar.gz: 3de5c92aac34aa8fa73f556e53e0063857020724578779d8eb0f197865baf230e09c348666f49685281971d2239eb41ad7449fc2d783f16026a250f21a1d14f1
|
data/Appraisals
CHANGED
@@ -13,8 +13,8 @@ appraise "rails51" do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
appraise "rails52" do
|
16
|
-
gem 'activerecord', '5.2.0'
|
17
|
-
gem 'activesupport', '5.2.0'
|
16
|
+
gem 'activerecord', '~> 5.2.0'
|
17
|
+
gem 'activesupport', '~> 5.2.0'
|
18
18
|
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
|
19
19
|
gem 'pg', '>= 0.18', '< 2.0'
|
20
20
|
end
|
data/README.md
CHANGED
@@ -44,8 +44,6 @@ Account.count
|
|
44
44
|
### Access to the DB Master
|
45
45
|
If you wish to ensure that queries are directed to the DB master, call `read_master`.
|
46
46
|
|
47
|
-
> Note: Before version 0.4.3, `readonly(false)` must be used.
|
48
|
-
|
49
47
|
```ruby
|
50
48
|
Article.where(id: 1).read_master
|
51
49
|
|
@@ -72,9 +70,8 @@ old_article.destroy
|
|
72
70
|
|
73
71
|
## ActiveRecord Versions Supported
|
74
72
|
|
75
|
-
- FreshConnection supports ActiveRecord version
|
76
|
-
- If you are using Rails 4.
|
77
|
-
- If you are using Rails 3.2, you can use FreshConnection version 1.0.0 or before.
|
73
|
+
- FreshConnection supports ActiveRecord version 5.0 or later.
|
74
|
+
- If you are using Rails 4.2, you can use FreshConnection version 2.4.4 or before.
|
78
75
|
|
79
76
|
## Databases Supported
|
80
77
|
FreshConnection currently supports MySQL and PostgreSQL.
|
@@ -98,88 +95,68 @@ Or install it manually with:
|
|
98
95
|
$ gem install fresh_connection
|
99
96
|
```
|
100
97
|
|
101
|
-
### Variant Installation For Use With Some Other ActiveRecord Gems
|
102
|
-
|
103
|
-
If you are using NewRelic or other gems that insert themselves into the ActiveRecord call-chain using `method_alias`, then a slight variation on the installation and configuration is required.
|
104
|
-
|
105
|
-
In the `Gemfile`, use:
|
106
|
-
|
107
|
-
```ruby
|
108
|
-
gem "fresh_connection", require: false
|
109
|
-
```
|
110
|
-
|
111
|
-
Then, in `config/application.rb`, add the following:
|
112
|
-
|
113
|
-
```ruby
|
114
|
-
config.after_initialize do
|
115
|
-
require 'fresh_connection'
|
116
|
-
end
|
117
|
-
```
|
118
|
-
|
119
98
|
## Configuration
|
120
99
|
|
121
100
|
The FreshConnection database replica is configured within the standard Rails
|
122
101
|
database configuration file, `config/database.yml`, using a `replica:` stanza.
|
123
102
|
|
124
|
-
*Security Note*:
|
125
|
-
|
126
|
-
> We strongly recommend against using secrets within the `config/database.yml`
|
127
|
-
> file. Instead, it is both convenient and advisable to use ERB substitutions with
|
128
|
-
> environment variables within the file.
|
129
|
-
|
130
|
-
> Using the [`dotenv`](https://github.com/bkeepers/dotenv) gem to keep secrets in a `.env` file that is never committed
|
131
|
-
> to the source management repository will help make secrets manageable.
|
132
|
-
|
133
103
|
Below is a sample such configuration file.
|
134
104
|
|
135
105
|
### `config/database.yml`
|
136
106
|
|
137
107
|
```yaml
|
108
|
+
default: &default
|
109
|
+
adapter: mysql2
|
110
|
+
encoding: utf8
|
111
|
+
pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
112
|
+
username: root
|
113
|
+
password:
|
114
|
+
|
138
115
|
production:
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
username: <%= ENV['DB_MASTER_USER'] %>
|
145
|
-
password: <%= ENV['DB_MASTER_PASS'] %>
|
146
|
-
host: <%= ENV['DB_MASTER_HOST'] %>
|
147
|
-
socket: /var/run/mysqld/mysqld.sock
|
116
|
+
<<: *default
|
117
|
+
database: blog_production
|
118
|
+
username: master_db_user
|
119
|
+
password: <%= ENV['MASTER_DATABASE_PASSWORD'] %>
|
120
|
+
host: master_db
|
148
121
|
|
149
122
|
replica:
|
150
|
-
username:
|
151
|
-
password: <%= ENV['
|
152
|
-
host:
|
123
|
+
username: replica_db_user
|
124
|
+
password: <%= ENV['REPLICA_DATABASE_PASSWORD'] %>
|
125
|
+
host: replica_db
|
153
126
|
```
|
154
127
|
|
155
128
|
`replica` is the configuration used for connecting read-only queries to the database replica. All other connections will use the database master settings.
|
156
129
|
|
130
|
+
|
157
131
|
### Multiple DB Replicas
|
158
132
|
If you want to use multiple configured DB replicas, the configuration can contain multiple `replica` stanzas in the configuration file `config/database.yml`.
|
159
133
|
|
160
134
|
For example:
|
161
135
|
|
162
136
|
```yaml
|
137
|
+
default: &default
|
138
|
+
adapter: mysql2
|
139
|
+
encoding: utf8
|
140
|
+
pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
141
|
+
username: root
|
142
|
+
password:
|
143
|
+
|
163
144
|
production:
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
username: <%= ENV['DB_MASTER_USER'] %>
|
170
|
-
password: <%= ENV['DB_MASTER_PASS'] %>
|
171
|
-
host: <%= ENV['DB_MASTER_HOST'] %>
|
172
|
-
socket: /var/run/mysqld/mysqld.sock
|
145
|
+
<<: *default
|
146
|
+
database: blog_production
|
147
|
+
username: master_db_user
|
148
|
+
password: <%= ENV['MASTER_DATABASE_PASSWORD'] %>
|
149
|
+
host: master_db
|
173
150
|
|
174
151
|
replica:
|
175
|
-
username:
|
176
|
-
password: <%= ENV['
|
177
|
-
host:
|
152
|
+
username: replica_db_user
|
153
|
+
password: <%= ENV['REPLICA_DATABASE_PASSWORD'] %>
|
154
|
+
host: replica_db
|
178
155
|
|
179
156
|
admin_replica:
|
180
|
-
username:
|
181
|
-
password: <%= ENV['
|
182
|
-
host:
|
157
|
+
username: admin_replica_db_user
|
158
|
+
password: <%= ENV['ADMIN_REPLICA_DATABASE_PASSWORD'] %>
|
159
|
+
host: admin_replica_db
|
183
160
|
```
|
184
161
|
|
185
162
|
The custom replica stanza can then be applied as an argument to the `establish_fresh_connection` method in the models that should use it. For example:
|
@@ -211,46 +188,23 @@ The `AdminUser` and `Benefit` models will access the database configured for the
|
|
211
188
|
|
212
189
|
The `Customer` model will use the default connections: read-only queries will connect to the standard DB replica, and state-changing queries will connect to the DB master.
|
213
190
|
|
214
|
-
### Replica Configuration With Environment Variables
|
215
191
|
|
192
|
+
### Replica Configuration With Environment Variables
|
216
193
|
|
217
194
|
Alternative to using a configuration in the `database.yml` file, it is possible to completely specify the replica access components using environment variables.
|
218
195
|
|
219
|
-
The environment variables corresponding to the `:replica` group are `DATABASE_REPLICA_URL
|
220
|
-
|
221
|
-
The URL value is a [URL](https://en.wikipedia.org/wiki/URL) string with the following components:
|
222
|
-
|
223
|
-
_adapter://dbuser:dbpass@dbhost:dbport/dbname?querypath_
|
224
|
-
|
225
|
-
where the components are:
|
226
|
-
|
227
|
-
| Component | Purpose | Examples |
|
228
|
-
| --------- | ------- | --------- |
|
229
|
-
| _adapter_ | Declares the database adapter | `mysql`, `postgresql` |
|
230
|
-
| _dbuser_ | The login for the database | `root`, `postgres` |
|
231
|
-
| _dbpass_ | The password for the database | |
|
232
|
-
| _dbhost_ | The hostname for the database | `db1`, `localhost` |
|
233
|
-
| _dbport_ | The port for the database connection | `3306`, `5432` |
|
234
|
-
| _dbname_ | The name of the database | `mydb`, `appdb`, etc. |
|
235
|
-
| _querypath_ | One or more variable assignments: _var1=val1_, separated by '&' | `pools=5&reconnect=true` |
|
196
|
+
The environment variables corresponding to the `:replica` group are `DATABASE_REPLICA_URL`.
|
197
|
+
The URL string components is the same as Rails' `DATABASE_URL'.
|
236
198
|
|
237
|
-
|
238
|
-
For example, here is a URL appropriate for a connection to a test database on the local host, on a custom port:
|
239
|
-
|
240
|
-
export DATABASE_REPLICA_URL='postgresql://root:somepw@localhost:6432/test_db?pool=5&reconnect=true
|
241
|
-
|
242
|
-
### Multiple Replica Environment Variables
|
199
|
+
#### Multiple Replica Environment Variables
|
243
200
|
|
244
201
|
To specific URLs for multiple replicas, replace the string `REPLICA` in the environment variable name with the replica name, in upper case. See the examples for replicas: `:replica1`, `:replica2`, and `:admin_replica`
|
245
202
|
|
246
203
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
### Special-Case Replica URL
|
204
|
+
DATABASE_REPLICA1_URL='mysql://localhost/dbreplica1?pool=5&reconnect=true'
|
205
|
+
DATABASE_REPLICA2_URL='postgresql://localhost:6432/ro_db?pool=5&reconnect=true'
|
206
|
+
DATABASE_ADMIN_REPLICA_URL='postgresql://localhost:6432/admin_db?pool=5&reconnect=true'
|
252
207
|
|
253
|
-
When `:replica` is used but `DATABASE_REPLICA_URL` is *not* defined, then the value of `DATABASE_REPLICA1_URL` will be used if it is defined.
|
254
208
|
|
255
209
|
### Master-only Models
|
256
210
|
|
@@ -274,12 +228,6 @@ before_fork do |server, worker|
|
|
274
228
|
ActiveRecord::Base.clear_all_replica_connections!
|
275
229
|
...
|
276
230
|
end
|
277
|
-
|
278
|
-
after_fork do |server, worker|
|
279
|
-
...
|
280
|
-
ActiveRecord::Base.establish_fresh_connection
|
281
|
-
...
|
282
|
-
end
|
283
231
|
```
|
284
232
|
|
285
233
|
### Replica Connection Manager
|
data/gemfiles/rails52.gemfile
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fresh_connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tsukasa OISHI
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -251,9 +251,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
251
251
|
version: '2.2'
|
252
252
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
253
253
|
requirements:
|
254
|
-
- - "
|
254
|
+
- - ">="
|
255
255
|
- !ruby/object:Gem::Version
|
256
|
-
version:
|
256
|
+
version: '0'
|
257
257
|
requirements: []
|
258
258
|
rubyforge_project:
|
259
259
|
rubygems_version: 2.7.6
|