Class: Sequel::ConnectionGuard::DatabaseGuard

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/extensions/connection_guard/database_guard.rb

Overview

An abstraction for safely accessing Sequel models.

Examples:

Creating a database guard

DB = Sequel::DatabaseGuard.new('postgres://localhost/mydb')

Safely accessing the database

users = DB.safe_execute do
  alive do |db|
    db[:users].all
  end

  dead do
    []
  end
end

Unsafely accessing the database (raises an exception if connection fails)

DB.force_execute { |db| db[:users].insert(email: '[email protected]', role: 'admin') }

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(config, &initializer) ⇒ DatabaseGuard

Returns a new instance of DatabaseGuard.

Parameters:

  • config (String, Hash)

    database configuration

  • initializer (Proc)

    code to run upon successful connection

Since:

  • 0.1.0



32
33
34
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 32

def initialize(config, &initializer)
  @connection_guard = ConnectionGuard.new(config, &initializer)
end

Instance Method Details

#[](table_name) ⇒ Sequel::ConnectionGuard::Dataset

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • table_name (Symbol)

Returns:

Since:

  • 0.1.0



85
86
87
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 85

def [](table_name)
  Dataset.new(@connection_guard, table_name)
end

#disconnectvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Since:

  • 0.1.0



93
94
95
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 93

def disconnect
  @connection_guard.force_execute(&:disconnect)
end

#force_execute(&block) ⇒ Object

Unsafely access the database. Will fail if connection fails.

Examples:

DB.force_execute { |db| db[:users].insert(email: '[email protected]') }

Parameters:

  • block (Proc)

Raises:

  • (Sequel::DatabaseConnectionError)

    connection failure

Since:

  • 0.1.0



66
67
68
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 66

def force_execute(&block)
  @connection_guard.force_execute(&block)
end

#raw_handleObject

A raw connection handle. Intended for use in test environments (e.x. with DatabaseCleaner)

Raises:

  • (Sequel::DatabaseConnectionError)

    if connection is not established

Since:

  • 0.1.0



76
77
78
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 76

def raw_handle
  @connection_guard.raw_handle
end

#safe_execute(&block) ⇒ Object

Safely access the database.

Examples:

users = DB.safe_execute do
  alive { |db| db[:users].all }
  dead { [] }
end

Parameters:

  • block (Proc)

Since:

  • 0.1.0



48
49
50
51
52
53
54
# File 'lib/sequel/extensions/connection_guard/database_guard.rb', line 48

def safe_execute(&block)
  executor = Executor.new
  executor.instance_eval(&block)
  @connection_guard.force_execute(&executor.on_alive)
rescue Sequel::DatabaseConnectionError
  executor.on_dead&.call
end