forked from ParentSquare/faulty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from tedaford/add_postgres_patch
Experiment to add a patch for postgres
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'pg' | ||
|
||
class Faulty | ||
module Patch | ||
# Patch for the Postgres gem | ||
module Postgres | ||
include Base | ||
|
||
Patch.define_circuit_errors(self, ::PG::ConnectionBad) | ||
|
||
QUERY_WHITELIST = [ | ||
%r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i, | ||
%r{\A(?:/\*.*?\*/)?\s*COMMIT}i, | ||
%r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i | ||
].freeze | ||
|
||
def initialize(opts = {}) | ||
@faulty_circuit = Patch.circuit_from_hash( | ||
'pg', | ||
opts[:faulty], | ||
errors: [ | ||
::PG::ConnectionBad, | ||
::PG::UnableToSend | ||
], | ||
patched_error_mapper: Faulty::Patch::Postgres | ||
) | ||
|
||
super | ||
end | ||
|
||
def ping | ||
faulty_run { super } | ||
rescue Faulty::Patch::Postgres::FaultyError | ||
false | ||
end | ||
|
||
def connect(*args) | ||
faulty_run { super } | ||
end | ||
|
||
def query(*args) | ||
return super if QUERY_WHITELIST.any? { |r| !r.match(args.first).nil? } | ||
|
||
faulty_run { super } | ||
end | ||
end | ||
end | ||
end | ||
|
||
module PG | ||
class Connection | ||
prepend Faulty::Patch::Postgres | ||
end | ||
end |