Sha256: bcab7dd1fa2284e78ceabbd32a96fc9d36929a3fda6d9b9940ef2565ed0e8f29

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module SaferRailsConsole
  module Patches
    module Sandbox
      module AutoRollback
        extend SaferRailsConsole::Colors

        def self.rollback_and_begin_new_transaction
          connection = ::ActiveRecord::Base.connection
          connection.rollback_db_transaction
          connection.begin_db_transaction
        end

        def self.handle_and_reraise_exception(e)
          if e.message.include?('PG::ReadOnlySqlTransaction')
            puts color_text('An operation could not be completed due to read-only mode.', RED) # rubocop:disable Rails/Output
          else
            rollback_and_begin_new_transaction
          end

          raise e
        end

        module ActiveRecord
          module ConnectionAdapters
            module PostgreSQLAdapter5
              def execute_and_clear(sql, name, binds, prepare: false)
                super
              rescue => e
                SaferRailsConsole::Patches::Sandbox::AutoRollback.handle_and_reraise_exception(e)
              end
            end
          end
        end
      end
    end
  end
end

if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
  ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(SaferRailsConsole::Patches::Sandbox::AutoRollback::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter5)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
safer_rails_console-0.4.1 lib/safer_rails_console/patches/sandbox/auto_rollback.rb
safer_rails_console-0.4.0 lib/safer_rails_console/patches/sandbox/auto_rollback.rb