-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolate statement insurer in its helper
- Loading branch information
Thomas Hareau
committed
Jul 6, 2023
1 parent
e6170a4
commit e1fa09d
Showing
3 changed files
with
47 additions
and
23 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
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module SafePgMigrations | ||
module Helpers | ||
module TimeoutManagement | ||
def with_setting(key, value) | ||
old_value = query_value("SHOW #{key}") | ||
execute("SET #{key} TO #{quote(value)}") | ||
begin | ||
yield | ||
ensure | ||
begin | ||
execute("SET #{key} TO #{quote(old_value)}") | ||
rescue ActiveRecord::StatementInvalid => e | ||
# Swallow `PG::InFailedSqlTransaction` exceptions so as to keep the | ||
# original exception (if any). | ||
raise unless e.cause.is_a?(PG::InFailedSqlTransaction) | ||
end | ||
end | ||
end | ||
|
||
def without_statement_timeout(&block) | ||
with_setting(:statement_timeout, 0, &block) | ||
end | ||
|
||
def without_lock_timeout(&block) | ||
with_setting(:lock_timeout, 0, &block) | ||
end | ||
|
||
def without_timeout(&block) | ||
without_statement_timeout { without_lock_timeout(&block) } | ||
end | ||
end | ||
end | ||
end |
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