123456789_123456789_123456789_123456789_123456789_

Top Level Namespace

Relationships & Source Files
Namespace Children
Modules:
Classes:

Instance Method Summary

Instance Method Details

#check_if_transactions_might_be_available!(client)

This method raises an error if the cluster the client is connected to does not support transactions in any case. At the moment this is the case of the standalone topology.

Please note that if this method did not raise, it does not guarantee that transactions are available for the cluster.

Parameters:

  • client (Mongo::Client)

    Client connected to a cluster to be tested.

Raises:

[ GitHub ]

  
# File 'lib/mongoid/railties/console_sandbox.rb', line 16

def check_if_transactions_might_be_available!(client)
  if client.cluster.single?
    raise Mongoid::Errors::TransactionsNotSupported
  end
end

#start_sandbox

Prepares console sandbox mode. This method should be called when a user starts rails console with ‘–sandbox’ flag.

[ GitHub ]

  
# File 'lib/mongoid/railties/console_sandbox.rb', line 36

def start_sandbox
  Mongoid.persistence_context.client.tap do |client|
    check_if_transactions_might_be_available!(client)
    start_sandbox_transaction(client)
  end
end

#start_sandbox_transaction(client)

Starts a transaction that should include all the operations inside the sandboxed console session. This transaction should not be ever committed. When a user end the console session, the client will disconnect, and the transaction will be automatically aborted therefore.

Parameters:

  • client (Mongo::Client)

    Client to start the transaction.

[ GitHub ]

  
# File 'lib/mongoid/railties/console_sandbox.rb', line 28

def start_sandbox_transaction(client)
  session = client.start_session
  ::Mongoid::Threaded.set_session(session, client: client)
  session.start_transaction
end