diff options
Diffstat (limited to 'spec/support/helpers/database')
-rw-r--r-- | spec/support/helpers/database/database_helpers.rb | 30 | ||||
-rw-r--r-- | spec/support/helpers/database/table_schema_helpers.rb | 4 |
2 files changed, 8 insertions, 26 deletions
diff --git a/spec/support/helpers/database/database_helpers.rb b/spec/support/helpers/database/database_helpers.rb index f3b2a2a6147..ecc42041e93 100644 --- a/spec/support/helpers/database/database_helpers.rb +++ b/spec/support/helpers/database/database_helpers.rb @@ -4,9 +4,7 @@ module Database module DatabaseHelpers # In order to directly work with views using factories, # we can swapout the view for a table of identical structure. - def swapout_view_for_table(view, connection: nil) - connection ||= ActiveRecord::Base.connection - + def swapout_view_for_table(view, connection:) connection.execute(<<~SQL.squish) CREATE TABLE #{view}_copy (LIKE #{view}); DROP VIEW #{view}; @@ -28,21 +26,20 @@ module Database # with_statement_timeout(0.1) do # model.select('pg_sleep(0.11)') # end - def with_statement_timeout(timeout) + def with_statement_timeout(timeout, connection:) # Force a positive value and a minimum of 1ms for very small values. timeout = (timeout * 1000).abs.ceil raise ArgumentError, 'Using a timeout of `0` means to disable statement timeout.' if timeout == 0 - previous_timeout = ActiveRecord::Base.connection - .exec_query('SHOW statement_timeout')[0].fetch('statement_timeout') + previous_timeout = connection.select_value('SHOW statement_timeout') - set_statement_timeout("#{timeout}ms") + connection.execute(format(%(SET LOCAL statement_timeout = '%s'), timeout)) yield ensure begin - set_statement_timeout(previous_timeout) + connection.execute(format(%(SET LOCAL statement_timeout = '%s'), previous_timeout)) rescue ActiveRecord::StatementInvalid # After a transaction was canceled/aborted due to e.g. a statement # timeout commands are ignored and will raise in PG::InFailedSqlTransaction. @@ -50,22 +47,5 @@ module Database # for the currrent transaction which will be closed anyway. end end - - # Set statement timeout for the current transaction. - # - # Note, that it does not restore the previous statement timeout. - # Use `with_statement_timeout` instead. - # - # @param timeout - Statement timeout in seconds - # - # Example: - # - # set_statement_timeout(0.1) - # model.select('pg_sleep(0.11)') - def set_statement_timeout(timeout) - ActiveRecord::Base.connection.execute( - format(%(SET LOCAL statement_timeout = '%s'), timeout) - ) - end end end diff --git a/spec/support/helpers/database/table_schema_helpers.rb b/spec/support/helpers/database/table_schema_helpers.rb index 472eaa45b4b..815c37e00e5 100644 --- a/spec/support/helpers/database/table_schema_helpers.rb +++ b/spec/support/helpers/database/table_schema_helpers.rb @@ -3,7 +3,9 @@ module Database module TableSchemaHelpers def connection - ActiveRecord::Base.connection + # We use ActiveRecord::Base.connection here because this is mainly used for database migrations + # where we override the connection on ActiveRecord::Base.connection + ActiveRecord::Base.connection # rubocop:disable Database/MultipleDatabases end def expect_table_to_be_replaced(original_table:, replacement_table:, archived_table:) |