Sha256: 2f172d8fd22a5917b1298d18b439350a2640c472548d909b0826b601428b3bd4

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

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.5.1 lib/safer_rails_console/patches/sandbox/auto_rollback.rb
safer_rails_console-0.5.0 lib/safer_rails_console/patches/sandbox/auto_rollback.rb