-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
39 additions
and
2 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
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,22 @@ | ||
module Sequel | ||
module StatementTimeout | ||
def with_statement_timeout(timeout_seconds = 20) | ||
# Might not have postgres class loaded, use class name | ||
if self.class.name == "Sequel::Postgres::Database" | ||
# Don't want to use a transaction because this will often be a read and a transaction is unnecessary. | ||
# Also, when using it for clean, want to control the transactions outside this. | ||
current_statement_timeout = execute("show statement_timeout") { |r| r.first.values.first } | ||
run("SET statement_timeout = '#{timeout_seconds}s'") | ||
begin | ||
yield | ||
ensure | ||
run("SET statement_timeout = '#{current_statement_timeout}'") | ||
end | ||
else | ||
yield | ||
end | ||
end | ||
end | ||
|
||
Database.register_extension(:statement_timeout){|db| db.extend(StatementTimeout) } | ||
end |