Class: Backup::DatabaseConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/database_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_name) ⇒ DatabaseConnection

Initializes a database connection

Parameters:

  • connection_name (String)

    the key from ‘database.yml` for multi-database connection configuration



13
14
15
16
17
18
19
# File 'lib/backup/database_connection.rb', line 13

def initialize(connection_name)
  @database_configuration = Backup::DatabaseConfiguration.new(connection_name)
  @backup_model = backup_model
  @snapshot_id = nil

  configure_backup_model
end

Instance Attribute Details

#database_configurationObject (readonly)

Returns the value of attribute database_configuration.



5
6
7
# File 'lib/backup/database_connection.rb', line 5

def database_configuration
  @database_configuration
end

#snapshot_idObject (readonly)

Returns the value of attribute snapshot_id.



5
6
7
# File 'lib/backup/database_connection.rb', line 5

def snapshot_id
  @snapshot_id
end

Instance Method Details

#disable_timeouts!Object



40
41
42
# File 'lib/backup/database_connection.rb', line 40

def disable_timeouts!
  transaction_timeout_settings.disable_timeouts
end

#export_snapshot!String

Start a new transaction and run pg_export_snapshot() Returns the snapshot identifier

Returns:

  • (String)

    snapshot identifier



25
26
27
28
29
30
# File 'lib/backup/database_connection.rb', line 25

def export_snapshot!
  disable_timeouts!

  connection.begin_transaction(isolation: :repeatable_read)
  @snapshot_id = connection.select_value("SELECT pg_export_snapshot()")
end

#release_snapshot!Object

Rollback the transaction to release the effects of pg_export_snapshot()



33
34
35
36
37
38
# File 'lib/backup/database_connection.rb', line 33

def release_snapshot!
  return unless snapshot_id

  connection.rollback_transaction
  @snapshot_id = nil
end

#restore_timeouts!Object



44
45
46
# File 'lib/backup/database_connection.rb', line 44

def restore_timeouts!
  transaction_timeout_settings.restore_timeouts
end