rails-pg-adapter 0.1.1
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +334 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +12 -0
- data/docker-compose.yml +10 -0
- data/lib/rails_pg_adapter/configuration.rb +42 -0
- data/lib/rails_pg_adapter/patch.rb +75 -0
- data/lib/rails_pg_adapter.rb +8 -0
- data/lib/version.rb +5 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8577c301e9dc09d6bcc0bc452be125fe2791f57d47be5fc1f03dbeac36ef504f
|
4
|
+
data.tar.gz: 017a545ce842f2834e742185a081d27929e4135ff77c15d6b639d0b609a1bc21
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0cd3b0dab08d6ac37d2cb82c0c39e153757e193a1b4057ecf5d57ad78aab94b3f5347f2489218462ccee81ea0c36d61523312fbf8a404a16c8a997f9552f6b34
|
7
|
+
data.tar.gz: 3c0e6b010aec4416941808df63676f779c51e3986299e772ed8fb873509c5f7dab45fcd833dd06b68e567b7dcebd8814ac50f7de4b756651a4929df83e335c68
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,334 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rails
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 2.6
|
7
|
+
NewCops: enable
|
8
|
+
Exclude:
|
9
|
+
- "**/.git/**/*"
|
10
|
+
- vendor/**/*
|
11
|
+
# - vendor/**/*.rb
|
12
|
+
|
13
|
+
Style/StringLiterals:
|
14
|
+
Enabled: true
|
15
|
+
EnforcedStyle: double_quotes
|
16
|
+
|
17
|
+
Style/StringLiteralsInInterpolation:
|
18
|
+
Enabled: true
|
19
|
+
EnforcedStyle: double_quotes
|
20
|
+
|
21
|
+
Layout/LineLength:
|
22
|
+
Max: 120
|
23
|
+
|
24
|
+
Bundler/OrderedGems:
|
25
|
+
Include:
|
26
|
+
- "**/Gemfile"
|
27
|
+
|
28
|
+
Style/FrozenStringLiteralComment:
|
29
|
+
EnforcedStyle: always
|
30
|
+
|
31
|
+
Style/MutableConstant:
|
32
|
+
EnforcedStyle: literals
|
33
|
+
|
34
|
+
Style/MethodCallWithArgsParentheses:
|
35
|
+
Enabled: true
|
36
|
+
EnforcedStyle: require_parentheses
|
37
|
+
AllowedMethods:
|
38
|
+
- yield
|
39
|
+
- raise
|
40
|
+
- fail
|
41
|
+
- puts
|
42
|
+
- require
|
43
|
+
- require_relative
|
44
|
+
- render
|
45
|
+
- redirect_to
|
46
|
+
- head
|
47
|
+
- throw
|
48
|
+
# RSpec
|
49
|
+
- to
|
50
|
+
- not_to
|
51
|
+
- to_not
|
52
|
+
- and
|
53
|
+
- or
|
54
|
+
Exclude:
|
55
|
+
- "**/Gemfile"
|
56
|
+
- "**/db/migrate/*"
|
57
|
+
- "**/db/schema.rb"
|
58
|
+
|
59
|
+
Style/RedundantInitialize:
|
60
|
+
Enabled: false
|
61
|
+
|
62
|
+
RSpec/MultipleMemoizedHelpers:
|
63
|
+
Max: 0
|
64
|
+
Exclude:
|
65
|
+
- "**/spec/migrations/**/*"
|
66
|
+
|
67
|
+
Layout:
|
68
|
+
Enabled: false
|
69
|
+
|
70
|
+
Metrics:
|
71
|
+
Enabled: false
|
72
|
+
|
73
|
+
Naming/AccessorMethodName:
|
74
|
+
Enabled: false
|
75
|
+
|
76
|
+
Naming/MethodParameterName:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
Naming/PredicateName:
|
80
|
+
Enabled: false
|
81
|
+
|
82
|
+
Naming/VariableNumber:
|
83
|
+
Enabled: false
|
84
|
+
|
85
|
+
Style/AsciiComments:
|
86
|
+
Enabled: false
|
87
|
+
|
88
|
+
Style/BlockDelimiters:
|
89
|
+
Enabled: false
|
90
|
+
|
91
|
+
Style/CaseLikeIf:
|
92
|
+
Enabled: false
|
93
|
+
|
94
|
+
Style/ClassAndModuleChildren:
|
95
|
+
Enabled: false
|
96
|
+
|
97
|
+
Style/CommentAnnotation:
|
98
|
+
Enabled: false
|
99
|
+
|
100
|
+
Style/Documentation:
|
101
|
+
Enabled: false
|
102
|
+
|
103
|
+
Style/IfUnlessModifier:
|
104
|
+
Enabled: false
|
105
|
+
|
106
|
+
Style/Lambda:
|
107
|
+
Enabled: false
|
108
|
+
|
109
|
+
Style/ModuleFunction:
|
110
|
+
Enabled: false
|
111
|
+
|
112
|
+
Style/MultilineBlockChain:
|
113
|
+
Enabled: false
|
114
|
+
|
115
|
+
Style/NumericLiterals:
|
116
|
+
Enabled: false
|
117
|
+
|
118
|
+
Style/NumericPredicate:
|
119
|
+
Enabled: false
|
120
|
+
|
121
|
+
Style/ParallelAssignment:
|
122
|
+
Enabled: false
|
123
|
+
|
124
|
+
Style/PerlBackrefs:
|
125
|
+
Enabled: false
|
126
|
+
|
127
|
+
Style/QuotedSymbols:
|
128
|
+
EnforcedStyle: double_quotes
|
129
|
+
Enabled: false
|
130
|
+
|
131
|
+
Style/RaiseArgs:
|
132
|
+
Enabled: false
|
133
|
+
|
134
|
+
Style/RescueStandardError:
|
135
|
+
Enabled: false
|
136
|
+
|
137
|
+
Style/SingleArgumentDig:
|
138
|
+
Enabled: false
|
139
|
+
|
140
|
+
Style/SymbolArray:
|
141
|
+
EnforcedStyle: brackets
|
142
|
+
|
143
|
+
Style/TrailingCommaInArguments:
|
144
|
+
Enabled: false
|
145
|
+
|
146
|
+
Style/TrailingCommaInArrayLiteral:
|
147
|
+
Enabled: false
|
148
|
+
EnforcedStyleForMultiline: consistent_comma
|
149
|
+
|
150
|
+
Style/TrailingCommaInHashLiteral:
|
151
|
+
Enabled: false
|
152
|
+
|
153
|
+
Style/TrailingUnderscoreVariable:
|
154
|
+
Enabled: false
|
155
|
+
|
156
|
+
Style/WordArray:
|
157
|
+
EnforcedStyle: brackets
|
158
|
+
|
159
|
+
Style/ZeroLengthPredicate:
|
160
|
+
Enabled: false
|
161
|
+
|
162
|
+
Style/DateTime:
|
163
|
+
Enabled: true
|
164
|
+
|
165
|
+
RSpec/ExpectChange:
|
166
|
+
EnforcedStyle: block
|
167
|
+
|
168
|
+
Gemspec/RequireMFA:
|
169
|
+
# Our Gemspec files are internal, MFA isn't needed
|
170
|
+
Enabled: false
|
171
|
+
|
172
|
+
# Temporary Rubocop exclusions
|
173
|
+
Style/OpenStructUse:
|
174
|
+
Enabled: false
|
175
|
+
|
176
|
+
# Ruby 3 migration exclusions
|
177
|
+
Style/HashSyntax:
|
178
|
+
Enabled: false
|
179
|
+
|
180
|
+
Naming/BlockForwarding:
|
181
|
+
Enabled: false
|
182
|
+
|
183
|
+
Lint/RedundantDirGlobSort:
|
184
|
+
Enabled: false
|
185
|
+
|
186
|
+
# Temporary Rubocop/Rails exclusions
|
187
|
+
Rails/TimeZone:
|
188
|
+
Enabled: false
|
189
|
+
|
190
|
+
Rails/Pick:
|
191
|
+
Enabled: false
|
192
|
+
|
193
|
+
Rails/SkipsModelValidations:
|
194
|
+
Enabled: false
|
195
|
+
|
196
|
+
Rails/Output:
|
197
|
+
Enabled: false
|
198
|
+
|
199
|
+
Rails/PluralizationGrammar:
|
200
|
+
Enabled: false
|
201
|
+
|
202
|
+
Rails/FilePath:
|
203
|
+
Enabled: false
|
204
|
+
|
205
|
+
Rails/Present:
|
206
|
+
Enabled: false
|
207
|
+
|
208
|
+
Rails/Blank:
|
209
|
+
Enabled: false
|
210
|
+
|
211
|
+
Rails/Pluck:
|
212
|
+
Enabled: false
|
213
|
+
|
214
|
+
Rails/WhereExists:
|
215
|
+
Enabled: false
|
216
|
+
|
217
|
+
Rails/NegateInclude:
|
218
|
+
Enabled: false
|
219
|
+
|
220
|
+
Rails/DynamicFindBy:
|
221
|
+
Enabled: false
|
222
|
+
|
223
|
+
Rails/Delegate:
|
224
|
+
Enabled: false
|
225
|
+
|
226
|
+
Rails/Presence:
|
227
|
+
Enabled: false
|
228
|
+
|
229
|
+
Rails/Validation:
|
230
|
+
Enabled: false
|
231
|
+
|
232
|
+
Rails/ActiveRecordCallbacksOrder:
|
233
|
+
Enabled: false
|
234
|
+
|
235
|
+
Rails/HasManyOrHasOneDependent:
|
236
|
+
Enabled: false
|
237
|
+
|
238
|
+
Rails/InverseOf:
|
239
|
+
Enabled: false
|
240
|
+
|
241
|
+
Rails/ApplicationController:
|
242
|
+
Enabled: false
|
243
|
+
|
244
|
+
Rails/HttpStatus:
|
245
|
+
Enabled: false
|
246
|
+
|
247
|
+
Rails/LexicallyScopedActionFilter:
|
248
|
+
Enabled: false
|
249
|
+
|
250
|
+
Rails/WhereEquals:
|
251
|
+
Enabled: false
|
252
|
+
|
253
|
+
Rails/FindById:
|
254
|
+
Enabled: false
|
255
|
+
|
256
|
+
Rails/OutputSafety:
|
257
|
+
Enabled: false
|
258
|
+
|
259
|
+
Rails/SquishedSQLHeredocs:
|
260
|
+
Enabled: false
|
261
|
+
|
262
|
+
Rails/UnusedIgnoredColumns:
|
263
|
+
Enabled: false
|
264
|
+
|
265
|
+
Rails/RedundantForeignKey:
|
266
|
+
Enabled: false
|
267
|
+
|
268
|
+
Rails/FindEach:
|
269
|
+
Enabled: false
|
270
|
+
|
271
|
+
# Temporary Rubocop/RSpec exclusions
|
272
|
+
|
273
|
+
RSpec/ExampleLength:
|
274
|
+
Enabled: false
|
275
|
+
|
276
|
+
RSpec/MultipleExpectations:
|
277
|
+
Enabled: false
|
278
|
+
|
279
|
+
RSpec/VerifiedDoubles:
|
280
|
+
Enabled: false
|
281
|
+
|
282
|
+
RSpec/ContextWording:
|
283
|
+
Enabled: false
|
284
|
+
|
285
|
+
RSpec/AnyInstance:
|
286
|
+
Enabled: false
|
287
|
+
|
288
|
+
RSpec/MessageSpies:
|
289
|
+
Enabled: false
|
290
|
+
|
291
|
+
RSpec/RepeatedDescription:
|
292
|
+
Enabled: false
|
293
|
+
|
294
|
+
RSpec/RepeatedExample:
|
295
|
+
Enabled: false
|
296
|
+
|
297
|
+
RSpec/HookArgument:
|
298
|
+
Enabled: false
|
299
|
+
|
300
|
+
RSpec/DescribeClass:
|
301
|
+
Enabled: false
|
302
|
+
|
303
|
+
RSpec/DescribedClass:
|
304
|
+
Enabled: false
|
305
|
+
|
306
|
+
RSpec/FilePath:
|
307
|
+
Enabled: false
|
308
|
+
|
309
|
+
RSpec/IdenticalEqualityAssertion:
|
310
|
+
Enabled: false
|
311
|
+
|
312
|
+
RSpec/InstanceVariable:
|
313
|
+
Enabled: false
|
314
|
+
|
315
|
+
RSpec/MissingExampleGroupArgument:
|
316
|
+
Enabled: false
|
317
|
+
|
318
|
+
RSpec/MultipleDescribes:
|
319
|
+
Enabled: false
|
320
|
+
|
321
|
+
RSpec/NestedGroups:
|
322
|
+
Enabled: false
|
323
|
+
|
324
|
+
RSpec/PredicateMatcher:
|
325
|
+
Enabled: false
|
326
|
+
|
327
|
+
RSpec/Rails/HttpStatus:
|
328
|
+
Enabled: false
|
329
|
+
|
330
|
+
RSpec/RepeatedExampleGroupDescription:
|
331
|
+
Enabled: false
|
332
|
+
|
333
|
+
RSpec/StubbedMock:
|
334
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at smukherjee@tines.io. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Shayon Mukherjee
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# RailsPgAdapter
|
2
|
+
|
3
|
+
This project allows you to monkey patch `ActiveRecord` (PostgreSQL) and auto-heal applications in production when PostgreSQL database fails over or when a cached column (in `ActiveRecord` schema cache) is removed from the database from a migration in another process.
|
4
|
+
|
5
|
+
## How does it work
|
6
|
+
|
7
|
+
During a database failover in production, the `ActiveRecord` connection pool can become exhausted as queries are made against the database during the failover process. This can leave the `ActiveRecord` connection pools with stale or bad connections, even after the database has successfully recovered. Recovering from this issue usually requires a rolling restart of the application processes or containers.
|
8
|
+
|
9
|
+
`RailsPgAdapter` addresses this problem by resetting the connection pool and re-raises the original exception from an `ActiveRecord` monkey patch. This allows the application to auto-heal from stale connections on its own (after database recovery) when performing queries for a new request, without requiring manual intervention.
|
10
|
+
|
11
|
+
Another issue with `ActiveRecord` queries is `PG::UndefinedColumn`, which occurs when an `ActiveRecord` model includes a `SELECT` query with the name of a column that has been dropped from a Rails migration. This can happen even if the column isn't being referenced anywhere in the code. It occurs when a model is using `ignored_columns`, which prompts `ActiveRecord` to perform a dedicated lookup of the allowed columns in a select, such as `SELECT "users".name, "users".template_id...."`, instead of `SELECT "users".*`. When a column like `template_id` is dropped, PostgreSQL throws an undefined column error, which is bubbled up by `ActiveRecord` into `PG::UndefinedColumn`. Recovering from this issue also usually requires a rolling restart of the application processes or containers.
|
12
|
+
|
13
|
+
`RailsPgAdapter` solves this second issue by resetting the `ActiveRecord` schema cache and memoized model column information when it detects a `PG::UndefinedColumn` raised from a monkey patch. Resetting the column information forces `ActiveRecord` to refresh its schema cache by loading the table information from the database and no longer reference the dropped column for new queries, without requiring manual intervention.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Install the gem and add to the application's Gemfile by executing:
|
18
|
+
|
19
|
+
$ bundle add rails-pg-adapter
|
20
|
+
|
21
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
22
|
+
|
23
|
+
$ gem install rails-pg-adapter
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Auto healing connections when PostgreSQL database fails over
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# config/initializer/rails_pg_adapter.rb
|
31
|
+
|
32
|
+
RailsPgAdapter.configure do |c|
|
33
|
+
c.add_failover_patch = true
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
This will add the monkey patch which resets the `ActiveRecord` connections in the connection pool when the database fails over. The patch will reset the connection and re-raise the error each time it detects that an exception related to a database failover is detected.
|
38
|
+
|
39
|
+
### Refresh model column information on the fly after an existing column is dropped
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# config/initializer/rails_pg_adapter.rb
|
43
|
+
|
44
|
+
RailsPgAdapter.configure do |c|
|
45
|
+
c.add_reset_column_information_patch = true
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
This will clear the `ActiveRecord` schema cache and reset the `ActiveRecord` column information memoized on the model. The patch will reset the relevant information and re-raise the error each time it detects that an exception related to a dropped column is raised.
|
50
|
+
|
51
|
+
## Development
|
52
|
+
|
53
|
+
- Install ruby 3.0
|
54
|
+
|
55
|
+
```
|
56
|
+
\curl -sSL https://get.rvm.io | bash
|
57
|
+
|
58
|
+
rvm install 3.0.0
|
59
|
+
|
60
|
+
rvm use 3.0.0
|
61
|
+
```
|
62
|
+
|
63
|
+
- `docker compose up -d` - to spin up postgres locally
|
64
|
+
- `bundle exec rspec` to run the tests.
|
65
|
+
- You can also run `bin/console` for an interactive prompt.
|
66
|
+
|
67
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tines/rails-pg-adapter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tines/rails-pg-adapter/blob/main/CODE_OF_CONDUCT.md).
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
76
|
+
|
77
|
+
## Code of Conduct
|
78
|
+
|
79
|
+
Everyone interacting in the `RailsPgAdapter` project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tines/rails-pg-adapter/blob/main/CODE_OF_CONDUCT.md).
|
80
|
+
|
81
|
+
## Releasing a new version
|
82
|
+
|
83
|
+
- Bump version in `version.rb`
|
84
|
+
- Update `CHANGELOG.md`
|
85
|
+
- Push the changes to `main`
|
86
|
+
- Run the release script with the new version `./bin/release.sh 0.2.0`
|
87
|
+
- Note: It will ask for MFA.
|
88
|
+
- Create a new release - https://github.com/tines/rails-pg-adapter/releases/new
|
data/Rakefile
ADDED
data/docker-compose.yml
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsPgAdapter
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :add_failover_patch, :add_reset_column_information_patch
|
6
|
+
|
7
|
+
def initialize(attrs)
|
8
|
+
self.add_failover_patch = attrs[:add_failover_patch]
|
9
|
+
self.add_reset_column_information_patch = attrs[:add_reset_column_information_patch]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.configuration
|
14
|
+
@configuration ||= Configuration.new({
|
15
|
+
add_failover_patch: false,
|
16
|
+
add_reset_column_information_patch: false,
|
17
|
+
})
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.configure
|
21
|
+
yield(configuration)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.failover_patch?
|
25
|
+
RailsPgAdapter.configuration.add_failover_patch || false
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.reset_column_information_patch?
|
29
|
+
RailsPgAdapter.configuration.add_reset_column_information_patch || false
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.enabled?
|
33
|
+
failover_patch? || reset_column_information_patch?
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.reset_configuration
|
37
|
+
@configuration = Configuration.new({
|
38
|
+
add_failover_patch: false,
|
39
|
+
add_reset_column_information_patch: false,
|
40
|
+
})
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsPgAdapter
|
4
|
+
module Patch
|
5
|
+
CONNECTION_ERROR = [
|
6
|
+
"Lost connection",
|
7
|
+
"gone away",
|
8
|
+
"read-only",
|
9
|
+
"PG::ReadOnlySqlTransaction",
|
10
|
+
"PG::UnableToSend",
|
11
|
+
"PG::ConnectionBad",
|
12
|
+
"the database system is starting up",
|
13
|
+
"connection is closed",
|
14
|
+
].freeze
|
15
|
+
CONNECTION_ERROR_RE = /#{CONNECTION_ERROR.map { |w| Regexp.escape(w) }.join("|")}/.freeze
|
16
|
+
|
17
|
+
CONNECTION_SCHEMA_ERROR = ["PG::UndefinedColumn"].freeze
|
18
|
+
CONNECTION_SCHEMA_RE = /#{CONNECTION_SCHEMA_ERROR.map { |w| Regexp.escape(w) }.join("|")}/.freeze
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def exec_cache(*args)
|
23
|
+
super(*args)
|
24
|
+
rescue ::ActiveRecord::StatementInvalid => e
|
25
|
+
handle_error(e) || raise
|
26
|
+
end
|
27
|
+
|
28
|
+
def exec_no_cache(*args)
|
29
|
+
super(*args)
|
30
|
+
rescue ::ActiveRecord::StatementInvalid => e
|
31
|
+
handle_error(e) || raise
|
32
|
+
end
|
33
|
+
|
34
|
+
def handle_error(e)
|
35
|
+
if failover_error?(e.message) && RailsPgAdapter.failover_patch?
|
36
|
+
warn("clearing connections due to #{e} - #{e.message}")
|
37
|
+
disconnect_and_remove_conn!
|
38
|
+
raise
|
39
|
+
end
|
40
|
+
|
41
|
+
return unless missing_column_error?(e.message) && RailsPgAdapter.reset_column_information_patch?
|
42
|
+
|
43
|
+
warn("clearing column information due to #{e} - #{e.message}")
|
44
|
+
|
45
|
+
internal_clear_schema_cache!
|
46
|
+
raise
|
47
|
+
end
|
48
|
+
|
49
|
+
def failover_error?(error_message)
|
50
|
+
CONNECTION_ERROR_RE.match?(error_message)
|
51
|
+
end
|
52
|
+
|
53
|
+
def missing_column_error?(error_message)
|
54
|
+
CONNECTION_SCHEMA_RE.match?(error_message)
|
55
|
+
end
|
56
|
+
|
57
|
+
def disconnect_and_remove_conn!
|
58
|
+
disconnect!
|
59
|
+
::ActiveRecord::Base.connection_pool.remove(::ActiveRecord::Base.connection)
|
60
|
+
end
|
61
|
+
|
62
|
+
def internal_clear_schema_cache!
|
63
|
+
::ActiveRecord::Base.connection_pool.connections.each { |conn| conn.schema_cache.clear! }
|
64
|
+
::ActiveRecord::Base.descendants.each(&:reset_column_information)
|
65
|
+
end
|
66
|
+
|
67
|
+
def warn(msg)
|
68
|
+
return unless defined?(Rails)
|
69
|
+
::Rails.logger.warn("[RailsPgAdapter::Patch] #{msg}")
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(RailsPgAdapter::Patch) if RailsPgAdapter.enabled?
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-pg-adapter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tines Engineering
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "<"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "<"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7.0'
|
27
|
+
description: This project allows you to monkey patch ActiveRecord (PostgreSQL) and
|
28
|
+
auto-heal applications in production when PostgreSQL database fails over or when
|
29
|
+
a cached column (in ActiveRecord schema cache) is removed from the database from
|
30
|
+
a migration in another process.
|
31
|
+
email:
|
32
|
+
- engineering@tines.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- ".rspec"
|
38
|
+
- ".rubocop.yml"
|
39
|
+
- CHANGELOG.md
|
40
|
+
- CODE_OF_CONDUCT.md
|
41
|
+
- Gemfile
|
42
|
+
- LICENSE.txt
|
43
|
+
- README.md
|
44
|
+
- Rakefile
|
45
|
+
- docker-compose.yml
|
46
|
+
- lib/rails_pg_adapter.rb
|
47
|
+
- lib/rails_pg_adapter/configuration.rb
|
48
|
+
- lib/rails_pg_adapter/patch.rb
|
49
|
+
- lib/version.rb
|
50
|
+
homepage: http://github.com/tines/rails-pg-adapter
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
rubygems_mfa_required: 'true'
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.6.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.2.3
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Rails Postgres ActiveRecord patches for common production workloads
|
74
|
+
test_files: []
|