Sha256: b3ccd8404149872937fb89c25f972465dfc14d9f358bb7862eeb439f4efdcd6c

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module SaferRailsConsole
  module Patches
    module Sandbox
      module TransactionReadOnly
        module PostgreSQLAdapterPatch
          def begin_db_transaction
            super
            execute 'SET TRANSACTION READ ONLY'
          end
        end

        module MySQLPatch
          def begin_db_transaction
            execute 'SET TRANSACTION READ ONLY'
            super
          end
        end

        if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
          ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(PostgreSQLAdapterPatch)

          # Ensure transaction is read-only if it was began before this patch was loaded
          connection = ::ActiveRecord::Base.connection
          connection.execute 'SET TRANSACTION READ ONLY' if connection.open_transactions > 0
        end

        if defined?(::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter)
          ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.prepend(MySQLPatch)

          # Not possible to change a running transaction to read-only in MySQL
          # https://dev.mysql.com/doc/refman/8.4/en/set-transaction.html
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
safer_rails_console-0.11.0 lib/safer_rails_console/patches/sandbox/transaction_read_only.rb
safer_rails_console-0.10.0 lib/safer_rails_console/patches/sandbox/transaction_read_only.rb
safer_rails_console-0.9.0 lib/safer_rails_console/patches/sandbox/transaction_read_only.rb