Module: Stagehand::Database

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

Defined Under Namespace

Classes: ProductionProbe, StagingProbe

Constant Summary collapse

@@connection_name_stack =
[Rails.env.to_sym]

Instance Method Summary collapse

Instance Method Details

#connected_to_production?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/stagehand/database.rb', line 12

def connected_to_production?
  current_connection_name == Configuration.production_connection_name
end

#connected_to_staging?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/stagehand/database.rb', line 16

def connected_to_staging?
  current_connection_name == Configuration.staging_connection_name
end

#each(&block) ⇒ Object



7
8
9
10
# File 'lib/stagehand/database.rb', line 7

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

#production_connectionObject



20
21
22
# File 'lib/stagehand/database.rb', line 20

def production_connection
  ProductionProbe.connection
end

#production_database_nameObject



28
29
30
# File 'lib/stagehand/database.rb', line 28

def production_database_name
  database_name(Configuration.production_connection_name)
end

#production_database_versionsObject



40
41
42
# File 'lib/stagehand/database.rb', line 40

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

#staging_connectionObject



24
25
26
# File 'lib/stagehand/database.rb', line 24

def staging_connection
  StagingProbe.connection
end

#staging_database_nameObject



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

def staging_database_name
  database_name(Configuration.staging_connection_name)
end

#staging_database_versionsObject



36
37
38
# File 'lib/stagehand/database.rb', line 36

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

#transactionObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/stagehand/database.rb', line 70

def transaction
  success = false
  output = nil
  ActiveRecord::Base.transaction do
    Production::Record.transaction do
      output = yield
      success = true
    end
    raise ActiveRecord::Rollback unless success
  end
  return output
end

#with_connection(connection_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/stagehand/database.rb', line 52

def with_connection(connection_name)
  different = current_connection_name != connection_name.to_sym

  if different
    @@connection_name_stack.push(connection_name.to_sym)
    Rails.logger.debug "Connecting to #{current_connection_name}"
    connect_to(current_connection_name)
  end

  yield connection_name
ensure
  if different
    @@connection_name_stack.pop
    Rails.logger.debug "Restoring connection to #{current_connection_name}"
    connect_to(current_connection_name)
  end
end

#with_production_connection(&block) ⇒ Object



48
49
50
# File 'lib/stagehand/database.rb', line 48

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

#with_staging_connection(&block) ⇒ Object



44
45
46
# File 'lib/stagehand/database.rb', line 44

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