fresh_connection 3.1.3 → 3.1.4
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 +190 -152
- data/fresh_connection.gemspec +3 -3
- data/lib/fresh_connection/access_control.rb +140 -21
- data/lib/fresh_connection/connection_manager.rb +6 -2
- data/lib/fresh_connection/connection_specification/rails_70.rb +48 -0
- data/lib/fresh_connection/connection_specification.rb +10 -2
- data/lib/fresh_connection/deprecator.rb +10 -0
- data/lib/fresh_connection/executor_hook.rb +4 -1
- data/lib/fresh_connection/extend/adapters/base_adapter.rb +116 -11
- data/lib/fresh_connection/extend/adapters/m2_adapter.rb +2 -11
- data/lib/fresh_connection/extend/adapters/pg_adapter.rb +2 -14
- data/lib/fresh_connection/extend/adapters/trilogy_adapter.rb +15 -0
- data/lib/fresh_connection/extend/ar_base.rb +115 -3
- data/lib/fresh_connection/extend/ar_connection_handler.rb +42 -9
- data/lib/fresh_connection/extend/ar_relation.rb +105 -13
- data/lib/fresh_connection/extend/ar_relation_merger.rb +1 -1
- data/lib/fresh_connection/extend/ar_statement_cache.rb +39 -2
- data/lib/fresh_connection/extend.rb +49 -3
- data/lib/fresh_connection/rails_replica.rb +559 -0
- data/lib/fresh_connection/railtie.rb +13 -1
- data/lib/fresh_connection/replica_connection_handler.rb +1 -0
- data/lib/fresh_connection/version.rb +1 -1
- data/lib/fresh_connection.rb +46 -2
- metadata +22 -19
- data/.travis.yml +0 -24
- data/Appraisals +0 -12
- data/benchmarks/benchmark.rb +0 -25
- data/bin/test +0 -24
- data/gemfiles/rails52.gemfile +0 -8
- data/gemfiles/rails60.gemfile +0 -7
- data/gemfiles/rails61.gemfile +0 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dc2a007641aef506cf027839b46534c6650b5e7ef0889cbca1f9e7731930843a
|
|
4
|
+
data.tar.gz: 6d164cf1a036428c5ef18db866b58e67ffa261bd46ab4eaaabdf9e9b8d7d58f8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: eb9aac02f3d14ee6111ef17616ab17de452cba58c950ebe478423575b6806cfdbe23a8b47d8980e1cdacac61a24618e0e4e74818437250766eb931602ac44484
|
|
7
|
+
data.tar.gz: 8f0ea9f103ad5d7173930e4f609106a0a8da8c367bc09d75ee1d6c2abd87714568d71bf72ea2cb51cd4a7f9995ca7413eae9cfd0b096a487a382a629564ee7c8
|
data/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# FreshConnection
|
|
2
|
-
[](http://badge.fury.io/rb/fresh_connection)
|
|
2
|
+
[](http://badge.fury.io/rb/fresh_connection)
|
|
3
3
|
|
|
4
|
-
**FreshConnection**
|
|
4
|
+
**FreshConnection** automatically routes ActiveRecord read queries to read replica databases and write/transaction queries to the primary (master) database — without requiring developers to think about master vs. replica in application code.
|
|
5
5
|
|
|
6
6
|
For example:
|
|
7
7
|
|
|
8
8
|
```text
|
|
9
|
-
Rails ------------ DB Master
|
|
9
|
+
Rails ------------ DB Primary (Master)
|
|
10
10
|
|
|
|
11
11
|
+---- DB Replica
|
|
12
12
|
```
|
|
@@ -14,7 +14,7 @@ Rails ------------ DB Master
|
|
|
14
14
|
or
|
|
15
15
|
|
|
16
16
|
```text
|
|
17
|
-
Rails -------+---- DB Master
|
|
17
|
+
Rails -------+---- DB Primary (Master)
|
|
18
18
|
|
|
|
19
19
|
| +------ DB Replica1
|
|
20
20
|
| |
|
|
@@ -23,23 +23,22 @@ Rails -------+---- DB Master
|
|
|
23
23
|
+------ DB Replica2
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
- Within a transaction, all queries go to the DB master.
|
|
26
|
+
- Read queries automatically go to the DB replica (in controllers, ActiveJob, Sidekiq, Rake tasks, and console alike).
|
|
27
|
+
- Write queries automatically go to the DB primary (master).
|
|
28
|
+
- Within a transaction, all queries automatically go to the DB primary (master).
|
|
29
|
+
- On Rails 6.1+, FreshConnection seamlessly builds on top of Rails' native multiple database / read replica support (`connects_to` / `connected_to`).
|
|
31
30
|
|
|
32
31
|
### Failover
|
|
33
|
-
FreshConnection assumes that there is a load balancer in front of multi replica servers.
|
|
34
|
-
When
|
|
32
|
+
FreshConnection assumes that there is a load balancer in front of multi replica servers.
|
|
33
|
+
When one of the replicas is unreachable for any reason, FreshConnection will retry (3 times by default) to access a replica via a load balancer.
|
|
35
34
|
|
|
36
|
-
Removing a
|
|
37
|
-
FreshConnection expects the load balancer to
|
|
35
|
+
Removing a troubled replica from a cluster is the responsibility of the load balancer.
|
|
36
|
+
FreshConnection expects the load balancer to route around the troubled replica during retries.
|
|
38
37
|
|
|
39
|
-
If you would like access
|
|
40
|
-
EbisuConnection has functions of load balancer.
|
|
38
|
+
If you would like to access multi replica servers without a load balancer, you can use [EbisuConnection](https://github.com/tsukasaoishi/ebisu_connection).
|
|
41
39
|
|
|
42
40
|
## Usage
|
|
41
|
+
|
|
43
42
|
### Access to the DB Replica
|
|
44
43
|
Read queries are automatically connected to the DB replica.
|
|
45
44
|
|
|
@@ -49,7 +48,7 @@ Article.where(id: 1)
|
|
|
49
48
|
Account.count
|
|
50
49
|
```
|
|
51
50
|
|
|
52
|
-
### Access to the DB Master
|
|
51
|
+
### Access to the DB Master (Primary)
|
|
53
52
|
If you wish to ensure that queries are directed to the DB master, call `read_master`.
|
|
54
53
|
|
|
55
54
|
```ruby
|
|
@@ -58,6 +57,14 @@ Article.where(id: 1).read_master
|
|
|
58
57
|
Account.read_master.count
|
|
59
58
|
```
|
|
60
59
|
|
|
60
|
+
You can also use `with_master` (or Rails' native `ActiveRecord::Base.connected_to(role: :writing)`):
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
Article.with_master do
|
|
64
|
+
Article.where(id: 1).first
|
|
65
|
+
end
|
|
66
|
+
```
|
|
67
|
+
|
|
61
68
|
Within transactions, all queries are connected to the DB master.
|
|
62
69
|
|
|
63
70
|
```ruby
|
|
@@ -66,7 +73,7 @@ Article.transaction do
|
|
|
66
73
|
end
|
|
67
74
|
```
|
|
68
75
|
|
|
69
|
-
Create, update and delete queries are connected to the DB master.
|
|
76
|
+
Create, update, and delete queries are connected to the DB master.
|
|
70
77
|
|
|
71
78
|
```ruby
|
|
72
79
|
new_article = Article.create(...)
|
|
@@ -76,166 +83,224 @@ new_article.save
|
|
|
76
83
|
old_article.destroy
|
|
77
84
|
```
|
|
78
85
|
|
|
86
|
+
Row-locking queries (`lock` / `SELECT ... FOR UPDATE`) and raw SQL methods (`find_by_sql`, `count_by_sql`) are also directed to the DB master.
|
|
87
|
+
|
|
88
|
+
> **Notes & Known Limitations:**
|
|
89
|
+
> - **`after_find` / `after_initialize` callbacks (Rails Native mode):** Read queries run inside Rails' `connected_to(role: :reading, prevent_writes: true)`. If an `after_find` or `after_initialize` callback performs a database write, wrap it in `Model.with_master { ... }` or `ActiveRecord::Base.connected_to(role: :writing) { ... }`.
|
|
90
|
+
> - **`belongs_to` with `-> { read_master }` scope:** `belongs_to` uses ActiveRecord's `StatementCache`, so a `read_master` inside a `belongs_to` scope lambda is not applied; use `with_master` or `master_db_only!` when master reads are required.
|
|
91
|
+
> - **`connected_to_many` / async calculation helpers (`async_count`, `async_pluck`, etc.):** Use `Model.with_master` or `ActiveRecord::Base.connected_to(role: :writing)` for explicit master blocks (`connected_to_many` is not intercepted). `Relation#load_async` is supported in Rails Native mode; standalone `async_count` / `async_pluck` helpers are not yet automatically routed.
|
|
92
|
+
> - **Rails 6.1–7.1 `uncached`:** Because `ConnectionPool#disable_query_cache` (block form) was introduced in Rails 7.2, `uncached` on Rails 6.1–7.1 uses `disable_query_cache!` on reading pools (which also clears cached entries).
|
|
93
|
+
|
|
94
|
+
### Master-only Models
|
|
95
|
+
It is possible to declare that specific models always use the DB master for all connections, using the `master_db_only!` method:
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
class CustomerState < ApplicationRecord
|
|
99
|
+
master_db_only!
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
All queries generated by methods on the `CustomerState` model (and its subclasses) will be directed to the DB master, even when preloaded via `includes` from a replica query.
|
|
104
|
+
|
|
79
105
|
## ActiveRecord Versions Supported
|
|
80
106
|
|
|
81
|
-
- FreshConnection supports ActiveRecord
|
|
107
|
+
- FreshConnection **3.1.4** supports ActiveRecord versions **5.2 through 8.1** (Rails 5.2, 6.0, 6.1, 7.0, 7.1, 7.2, 8.0, 8.1).
|
|
82
108
|
- If you are using Rails 5.1, you can use FreshConnection version 3.0.3 or before.
|
|
83
109
|
|
|
84
|
-
###
|
|
85
|
-
I haven't tested it in an environment using MultipleDB in Rails 6.
|
|
86
|
-
I plan to enable use with MultipleDB in FreshConnection version 4.0 or later.
|
|
110
|
+
### Deprecation Notice (Removal in FreshConnection 3.2.0)
|
|
87
111
|
|
|
88
|
-
|
|
89
|
-
FreshConnection currently supports MySQL and PostgreSQL.
|
|
112
|
+
In **FreshConnection 3.1.4**, all legacy FreshConnection-specific configuration styles remain supported for backward compatibility, **and will emit deprecation warnings to your Rails deprecation log** (`FreshConnection.deprecator`):
|
|
90
113
|
|
|
91
|
-
|
|
92
|
-
|
|
114
|
+
- `establish_fresh_connection`
|
|
115
|
+
- Nested replica stanzas inside `config/database.yml` (e.g., `replica:` / `replica1:` nested under `production:`)
|
|
116
|
+
- `DATABASE_REPLICA_URL` / `DATABASE_<NAME>_URL` environment variables
|
|
117
|
+
- Custom `FreshConnection.connection_manager=`
|
|
118
|
+
- ActiveRecord / Rails versions older than 7.0 (Rails 5.2, 6.0, 6.1)
|
|
93
119
|
|
|
94
|
-
|
|
95
|
-
gem "fresh_connection"
|
|
96
|
-
```
|
|
120
|
+
In **FreshConnection 3.2.0**, these legacy configuration methods and support for Rails < 7.0 will be **removed**, consolidating replica configuration onto Rails' native 3-tier `config/database.yml` and `connects_to`. Please migrate to Rails' native configuration while on 3.1.4.
|
|
97
121
|
|
|
98
|
-
|
|
122
|
+
#### Migration Guide (Legacy → Rails Native `connects_to`)
|
|
123
|
+
|
|
124
|
+
Before (Legacy — deprecated in 3.1.4, removed in 3.2.0):
|
|
99
125
|
|
|
126
|
+
```yaml
|
|
127
|
+
# config/database.yml
|
|
128
|
+
production:
|
|
129
|
+
adapter: mysql2
|
|
130
|
+
database: blog_production
|
|
131
|
+
host: master_db
|
|
132
|
+
replica1:
|
|
133
|
+
host: replica_db
|
|
100
134
|
```
|
|
101
|
-
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
class ApplicationRecord < ActiveRecord::Base
|
|
138
|
+
self.abstract_class = true
|
|
139
|
+
establish_fresh_connection :replica1
|
|
140
|
+
end
|
|
102
141
|
```
|
|
103
142
|
|
|
104
|
-
|
|
143
|
+
After (Recommended — Rails native):
|
|
105
144
|
|
|
145
|
+
```yaml
|
|
146
|
+
# config/database.yml
|
|
147
|
+
production:
|
|
148
|
+
primary:
|
|
149
|
+
adapter: mysql2 # or trilogy / postgresql
|
|
150
|
+
database: blog_production
|
|
151
|
+
host: master_db
|
|
152
|
+
primary_replica:
|
|
153
|
+
adapter: mysql2 # or trilogy / postgresql
|
|
154
|
+
database: blog_production
|
|
155
|
+
host: replica_db
|
|
156
|
+
replica: true
|
|
106
157
|
```
|
|
107
|
-
|
|
158
|
+
|
|
159
|
+
```ruby
|
|
160
|
+
class ApplicationRecord < ActiveRecord::Base
|
|
161
|
+
primary_abstract_class
|
|
162
|
+
connects_to database: { writing: :primary, reading: :primary_replica }
|
|
163
|
+
end
|
|
108
164
|
```
|
|
109
165
|
|
|
110
|
-
##
|
|
166
|
+
## Databases Supported
|
|
167
|
+
FreshConnection supports MySQL (`mysql2` and `trilogy` on Rails 7.1+) and PostgreSQL (`pg`).
|
|
111
168
|
|
|
112
|
-
|
|
113
|
-
|
|
169
|
+
## Installation
|
|
170
|
+
Add this line to your application's `Gemfile`:
|
|
114
171
|
|
|
115
|
-
|
|
172
|
+
```ruby
|
|
173
|
+
gem "fresh_connection"
|
|
174
|
+
```
|
|
116
175
|
|
|
117
|
-
|
|
176
|
+
And then execute:
|
|
118
177
|
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
encoding: utf8
|
|
123
|
-
pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
|
124
|
-
username: root
|
|
125
|
-
password:
|
|
178
|
+
```bash
|
|
179
|
+
$ bundle install
|
|
180
|
+
```
|
|
126
181
|
|
|
127
|
-
|
|
128
|
-
<<: *default
|
|
129
|
-
database: blog_production
|
|
130
|
-
username: master_db_user
|
|
131
|
-
password: <%= ENV['MASTER_DATABASE_PASSWORD'] %>
|
|
132
|
-
host: master_db
|
|
182
|
+
Or install it manually with:
|
|
133
183
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
password: <%= ENV['REPLICA_DATABASE_PASSWORD'] %>
|
|
137
|
-
host: replica_db
|
|
184
|
+
```bash
|
|
185
|
+
$ gem install fresh_connection
|
|
138
186
|
```
|
|
139
187
|
|
|
140
|
-
|
|
188
|
+
## Configuration (Recommended: Rails Native `connects_to`)
|
|
141
189
|
|
|
142
|
-
|
|
143
|
-
The 'replica' stanza has a special meaning in Rails6.
|
|
144
|
-
In Rails6, use a name other than 'replica', and specify that name using establish_fresh_connection in ApplicationRecord etc.
|
|
190
|
+
On Rails 6.1 / 7.x / 8.x, configure your primary and read replica databases using Rails' standard `config/database.yml` and `connects_to`. FreshConnection automatically detects the `:reading` role and routes read queries to it using Rails' own `connected_to` and connection pools.
|
|
145
191
|
|
|
146
|
-
###
|
|
147
|
-
If you want to use multiple configured DB replicas, the configuration can contain multiple `replica` stanzas in the configuration file `config/database.yml`.
|
|
148
|
-
|
|
149
|
-
For example:
|
|
192
|
+
### `config/database.yml`
|
|
150
193
|
|
|
151
194
|
```yaml
|
|
152
195
|
default: &default
|
|
153
|
-
adapter: mysql2
|
|
154
|
-
encoding:
|
|
155
|
-
pool:
|
|
156
|
-
username: root
|
|
157
|
-
password:
|
|
196
|
+
adapter: trilogy # or mysql2 / postgresql
|
|
197
|
+
encoding: utf8mb4
|
|
198
|
+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
|
158
199
|
|
|
159
200
|
production:
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
201
|
+
primary:
|
|
202
|
+
<<: *default
|
|
203
|
+
database: blog_production
|
|
204
|
+
username: master_db_user
|
|
205
|
+
password: <%= ENV['MASTER_DATABASE_PASSWORD'] %>
|
|
206
|
+
host: master_db
|
|
207
|
+
|
|
208
|
+
primary_replica:
|
|
209
|
+
<<: *default
|
|
210
|
+
database: blog_production
|
|
167
211
|
username: replica_db_user
|
|
168
212
|
password: <%= ENV['REPLICA_DATABASE_PASSWORD'] %>
|
|
169
213
|
host: replica_db
|
|
170
|
-
|
|
171
|
-
admin_replica:
|
|
172
|
-
username: admin_replica_db_user
|
|
173
|
-
password: <%= ENV['ADMIN_REPLICA_DATABASE_PASSWORD'] %>
|
|
174
|
-
host: admin_replica_db
|
|
214
|
+
replica: true
|
|
175
215
|
```
|
|
176
216
|
|
|
177
|
-
|
|
217
|
+
### `app/models/application_record.rb`
|
|
178
218
|
|
|
179
219
|
```ruby
|
|
180
|
-
class
|
|
181
|
-
|
|
220
|
+
class ApplicationRecord < ActiveRecord::Base
|
|
221
|
+
primary_abstract_class
|
|
222
|
+
|
|
223
|
+
connects_to database: { writing: :primary, reading: :primary_replica }
|
|
182
224
|
end
|
|
183
225
|
```
|
|
184
226
|
|
|
185
|
-
|
|
227
|
+
### Multiple DB Replicas / Multiple Databases (Rails Native)
|
|
186
228
|
|
|
187
|
-
|
|
188
|
-
class AdminBase < ActiveRecord::Base
|
|
189
|
-
establish_fresh_connection :admin_replica
|
|
190
|
-
end
|
|
229
|
+
You can configure different replicas or separate databases per abstract model class using standard Rails `connects_to`:
|
|
191
230
|
|
|
192
|
-
|
|
193
|
-
|
|
231
|
+
```ruby
|
|
232
|
+
class AdminRecord < ActiveRecord::Base
|
|
233
|
+
self.abstract_class = true
|
|
194
234
|
|
|
195
|
-
|
|
235
|
+
connects_to database: { writing: :primary, reading: :admin_replica }
|
|
196
236
|
end
|
|
197
237
|
|
|
198
|
-
class
|
|
238
|
+
class AdminUser < AdminRecord
|
|
199
239
|
end
|
|
200
240
|
```
|
|
201
241
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
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.
|
|
205
|
-
|
|
242
|
+
Models whose abstract class has no `:reading` role configured (for example, a secondary database without a replica) will safely stay on their `:writing` connection.
|
|
206
243
|
|
|
207
|
-
|
|
244
|
+
## FreshConnection Options
|
|
208
245
|
|
|
209
|
-
|
|
246
|
+
You can customize FreshConnection's behavior in an initializer (`config/initializers/fresh_connection.rb`):
|
|
210
247
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
248
|
+
```ruby
|
|
249
|
+
FreshConnection.configure do |config|
|
|
250
|
+
# Disconnect replica connections at the end of each request/job so the next
|
|
251
|
+
# request opens a fresh connection through a load balancer.
|
|
252
|
+
# Set to false to let Rails' connection pool keep connections alive (e.g. with RDS Proxy / ProxySQL).
|
|
253
|
+
# Default: true
|
|
254
|
+
config.put_aside = true
|
|
255
|
+
|
|
256
|
+
# Enable automatic failover recovery & retries on replica connection errors.
|
|
257
|
+
# Default: true
|
|
258
|
+
config.recovery = true
|
|
259
|
+
|
|
260
|
+
# Maximum total attempts (1 initial attempt + retries) when recovering from a replica connection error.
|
|
261
|
+
# For example, 3 means 1 initial attempt + up to 2 retries. Set to 0 or 1 to disable retries.
|
|
262
|
+
# Default: 3
|
|
263
|
+
config.retry_limit = 3
|
|
264
|
+
end
|
|
265
|
+
```
|
|
217
266
|
|
|
267
|
+
## Legacy Configuration (Deprecated in 3.1.4 — will be removed in 3.2.0)
|
|
218
268
|
|
|
219
|
-
|
|
220
|
-
DATABASE_REPLICA2_URL='postgresql://localhost:6432/ro_db?pool=5&reconnect=true'
|
|
221
|
-
DATABASE_ADMIN_REPLICA_URL='postgresql://localhost:6432/admin_db?pool=5&reconnect=true'
|
|
269
|
+
For backward compatibility with Rails 5.2 / 6.0 and existing FreshConnection setups, the legacy configuration style continues to work in **3.1.4** across Rails 5.2 through 8.1 and emits deprecation warnings to the Rails deprecation log (`FreshConnection.deprecator`). These legacy configuration options will be removed in **3.2.0**.
|
|
222
270
|
|
|
271
|
+
### Legacy `config/database.yml`
|
|
223
272
|
|
|
224
|
-
|
|
273
|
+
```yaml
|
|
274
|
+
production:
|
|
275
|
+
<<: *default
|
|
276
|
+
database: blog_production
|
|
277
|
+
username: master_db_user
|
|
278
|
+
password: <%= ENV['MASTER_DATABASE_PASSWORD'] %>
|
|
279
|
+
host: master_db
|
|
225
280
|
|
|
226
|
-
|
|
281
|
+
replica1:
|
|
282
|
+
username: replica_db_user
|
|
283
|
+
password: <%= ENV['REPLICA_DATABASE_PASSWORD'] %>
|
|
284
|
+
host: replica_db
|
|
285
|
+
```
|
|
227
286
|
|
|
228
287
|
```ruby
|
|
229
|
-
class
|
|
230
|
-
|
|
288
|
+
class ApplicationRecord < ActiveRecord::Base
|
|
289
|
+
self.abstract_class = true
|
|
290
|
+
establish_fresh_connection :replica1
|
|
231
291
|
end
|
|
232
292
|
```
|
|
233
293
|
|
|
234
|
-
|
|
294
|
+
### Legacy Replica Configuration With Environment Variables
|
|
235
295
|
|
|
236
|
-
|
|
296
|
+
```text
|
|
297
|
+
DATABASE_REPLICA_URL='mysql2://localhost/dbreplica?pool=5'
|
|
298
|
+
DATABASE_REPLICA1_URL='mysql2://localhost/dbreplica1?pool=5'
|
|
299
|
+
DATABASE_REPLICA2_URL='postgresql://localhost:6432/ro_db?pool=5'
|
|
300
|
+
DATABASE_ADMIN_REPLICA_URL='postgresql://localhost:6432/admin_db?pool=5'
|
|
301
|
+
```
|
|
237
302
|
|
|
238
|
-
|
|
303
|
+
### Using FreshConnection With Unicorn / Multi-Process Servers
|
|
239
304
|
|
|
240
305
|
```ruby
|
|
241
306
|
before_fork do |server, worker|
|
|
@@ -245,42 +310,14 @@ before_fork do |server, worker|
|
|
|
245
310
|
end
|
|
246
311
|
```
|
|
247
312
|
|
|
248
|
-
### Replica Connection Manager
|
|
249
|
-
The default replica connection manager is `FreshConnection::ConnectionManager`. If an alternative (custom) replica connection manager is desired, this can be done with a simple assignment within a Rails initializer:
|
|
250
|
-
|
|
251
|
-
`config/initializers/fresh_connection.rb`:
|
|
313
|
+
### Custom Replica Connection Manager (Deprecated)
|
|
252
314
|
|
|
253
315
|
```ruby
|
|
316
|
+
# config/initializers/fresh_connection.rb
|
|
254
317
|
FreshConnection.connection_manager = MyOwnReplicaConnection
|
|
255
318
|
```
|
|
256
319
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
```ruby
|
|
260
|
-
class MyOwnReplicaConnection < FreshConnection::AbstractConnectionManager
|
|
261
|
-
|
|
262
|
-
def replica_connection
|
|
263
|
-
# must return an instance of a subclass of ActiveRecord::ConnectionAdapters
|
|
264
|
-
# eg: ActiveRecord::ConnectionAdapter::Mysql2Adapter
|
|
265
|
-
# or: ActiveRecord::ConnectionAdapter::PostgresqlAdapter
|
|
266
|
-
end
|
|
267
|
-
|
|
268
|
-
def clear_all_connections!
|
|
269
|
-
# called to disconnect all connections
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
def put_aside!
|
|
273
|
-
# called when end of Rails controller action
|
|
274
|
-
end
|
|
275
|
-
|
|
276
|
-
def recovery?
|
|
277
|
-
# called when raising exceptions on access to the DB replica
|
|
278
|
-
# access will be retried when this method returns true
|
|
279
|
-
end
|
|
280
|
-
|
|
281
|
-
end
|
|
282
|
-
```
|
|
283
|
-
|
|
320
|
+
`MyOwnReplicaConnection` should inherit from `FreshConnection::AbstractConnectionManager` and implement `replica_connection`, `clear_all_connections!`, `put_aside!`, and `recovery?`.
|
|
284
321
|
|
|
285
322
|
## Contributing
|
|
286
323
|
|
|
@@ -292,14 +329,9 @@ end
|
|
|
292
329
|
|
|
293
330
|
## Test
|
|
294
331
|
|
|
295
|
-
|
|
296
|
-
To run the test suite, both `mysql` and `postgresql` must be installed.
|
|
297
|
-
|
|
298
|
-
### Test Configuration
|
|
332
|
+
To run the test suite, MySQL and PostgreSQL must be installed.
|
|
299
333
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
Then, run:
|
|
334
|
+
### Setup
|
|
303
335
|
|
|
304
336
|
```bash
|
|
305
337
|
./bin/setup
|
|
@@ -307,8 +339,14 @@ Then, run:
|
|
|
307
339
|
|
|
308
340
|
### Running Tests
|
|
309
341
|
|
|
310
|
-
To run the
|
|
342
|
+
To run the test suite:
|
|
311
343
|
|
|
312
344
|
```bash
|
|
313
345
|
./bin/test
|
|
314
346
|
```
|
|
347
|
+
|
|
348
|
+
Or for a specific Appraisal / Rails version:
|
|
349
|
+
|
|
350
|
+
```bash
|
|
351
|
+
BUNDLE_GEMFILE=gemfiles/rails80.gemfile ./bin/test
|
|
352
|
+
```
|
data/fresh_connection.gemspec
CHANGED
|
@@ -14,18 +14,18 @@ Gem::Specification.new do |spec|
|
|
|
14
14
|
spec.homepage = "https://github.com/tsukasaoishi/fresh_connection"
|
|
15
15
|
spec.license = "MIT"
|
|
16
16
|
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(\.github|\.travis\.yml|Appraisals|benchmarks|bin/test|gemfiles|test|spec|features)(/|$)}) }
|
|
18
18
|
spec.bindir = "exe"
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
20
20
|
spec.require_paths = ["lib"]
|
|
21
21
|
|
|
22
|
-
spec.add_dependency 'activerecord', '>= 5.2.0', '<
|
|
22
|
+
spec.add_dependency 'activerecord', '>= 5.2.0', '< 9.0'
|
|
23
23
|
|
|
24
24
|
spec.add_development_dependency 'mysql2', '>= 0.4.4'
|
|
25
25
|
spec.add_development_dependency 'pg', '>= 0.18', '< 2.0'
|
|
26
26
|
spec.add_development_dependency "rake"
|
|
27
27
|
spec.add_development_dependency 'appraisal'
|
|
28
|
-
spec.add_development_dependency "minitest", "
|
|
28
|
+
spec.add_development_dependency "minitest", ">= 5.10", "< 6.0"
|
|
29
29
|
spec.add_development_dependency "minitest-reporters"
|
|
30
30
|
spec.add_development_dependency "benchmark-ips"
|
|
31
31
|
end
|