Module: Stagehand::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/stagehand/database.rb

Defined Under Namespace

Modules: ConnectionStack Classes: InvalidConnectionError, NoRetryError, Probe, ProductionProbe, StagingProbe

Instance Method Summary collapse

Instance Method Details

#connected_to_production?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/stagehand/database.rb', line 38

def connected_to_production?
  current_connection_name == Configuration.production_connection_name
end

#connected_to_staging?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/stagehand/database.rb', line 42

def connected_to_staging?
  current_connection_name == Configuration.staging_connection_name
end

#each(&block) ⇒ Object



33
34
35
36
# File 'lib/stagehand/database.rb', line 33

def each(&block)
  with_production_connection(&block) unless Configuration.single_connection?
  with_staging_connection(&block)
end

#production_connectionObject



46
47
48
# File 'lib/stagehand/database.rb', line 46

def production_connection
  ProductionProbe.connection
end

#production_database_nameObject



54
55
56
# File 'lib/stagehand/database.rb', line 54

def production_database_name
  database_name(Configuration.production_connection_name)
end

#production_database_versionsObject



66
67
68
# File 'lib/stagehand/database.rb', line 66

def production_database_versions
  Stagehand::Database.production_connection.select_values(versions_scope)
end

#staging_connectionObject



50
51
52
# File 'lib/stagehand/database.rb', line 50

def staging_connection
  StagingProbe.connection
end

#staging_database_nameObject



58
59
60
# File 'lib/stagehand/database.rb', line 58

def staging_database_name
  database_name(Configuration.staging_connection_name)
end

#staging_database_versionsObject



62
63
64
# File 'lib/stagehand/database.rb', line 62

def staging_database_versions
  Stagehand::Database.staging_connection.select_values(versions_scope)
end

#transactionObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stagehand/database.rb', line 8

def transaction
  raise InvalidConnectionError, "Calling Stagehand::Database.transaction is not valid unless connected to staging" unless connected_to_staging?

  success = false
  attempts = 0
  output = nil
  ActiveRecord::Base.transaction do
    Production::Record.transaction do
      attempts += 1

      raise NoRetryError, "Retrying is not allowed in Stagehand::Database.transaction" if attempts > 1

      output = yield

      success = true
    end

    raise ActiveRecord::Rollback unless success
  end

  return output
ensure
  Rails.logger.warn "Stagehand::Database transaction was rolled back" unless success
end

#with_connection(connection_name, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/stagehand/database.rb', line 78

def with_connection(connection_name, &block)
  if current_connection_name != connection_name.to_sym
    Rails.logger.debug "Connecting to #{connection_name}"
    output = swap_connection(connection_name, &block)
    Rails.logger.debug "Restoring connection to #{current_connection_name}"
  else
    Rails.logger.debug "Already connected to #{connection_name}"
    output = yield connection_name
  end
  return output
end

#with_production_connection(&block) ⇒ Object



74
75
76
# File 'lib/stagehand/database.rb', line 74

def with_production_connection(&block)
  with_connection(Configuration.production_connection_name, &block)
end

#with_staging_connection(&block) ⇒ Object



70
71
72
# File 'lib/stagehand/database.rb', line 70

def with_staging_connection(&block)
  with_connection(Configuration.staging_connection_name, &block)
end