Class: ProjectdxPipeline::DatabaseConfigurationTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/projectdx_pipeline/database_configuration_template.rb

Overview

Used in the pipeline:commit:configure:database Rake task to transform a YAML file containing common elements from an ActiveRecord database.yml configuration into a YAML file containing a section for each specified database environment. It names the databases according to the environment as well as a client ID. The client ID ensures that multiple build agents can run tests at the same time even when using a central DB server.

Example call:

template = ProjectdxPipeline::DatabaseConfigurationTemplate.new('config/database.pipeline.yml')
template.render_to_file('config/database.yml')

Example template YAML file:

---
adapter: postgresql
username: postgres_user
password: blahblahblah
host: db.example.com
encoding: UTF8
min_messages: warning
database_prefix: an_awesome_app
pool: 5
environments:
  - development
  - test

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_file = nil) ⇒ DatabaseConfigurationTemplate

Instantiates a new configuration template. If a template_file is specified, it will be loaded as a YAML file and used to read in the configuration data for the template.



43
44
45
46
47
48
49
50
51
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 43

def initialize(template_file = nil)
  @config_data = {}
  if template_file
    @config_data = self.class.read_template(template_file)
    @client_id = @config_data.delete('client_id')
    @database_prefix = @config_data.delete('database_prefix')
    @environments = @config_data.delete('environments')
  end
end

Instance Attribute Details

#client_idObject

:nodoc:



38
39
40
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 38

def client_id
  @client_id
end

#config_dataObject

the common database config data shared by all environments



28
29
30
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 28

def config_data
  @config_data
end

#database_prefixObject

a string that will prefix the name of each database



34
35
36
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 34

def database_prefix
  @database_prefix
end

#environmentsObject

an array containing the names of each database environment to be configured



31
32
33
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 31

def environments
  @environments
end

Class Method Details

.read_template(template_file) ⇒ Object

:nodoc:



79
80
81
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 79

def self.read_template(template_file) #:nodoc:
  YAML.load_file(template_file)
end

Instance Method Details

#configurationObject

returns a Hash containing the ActiveRecord configuration data for all environments



55
56
57
58
59
60
61
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 55

def configuration
  environments.inject({}) do |config, env_name|
    config[env_name] = @config_data.dup
    config[env_name]['database'] = database_name(env_name)
    config
  end
end

#database_name(environment) ⇒ Object

:nodoc:



71
72
73
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 71

def database_name(environment) #:nodoc:
  "#{database_prefix}-#{environment}-#{client_id}"
end

#render_to_file(path) ⇒ Object

Writes the ActiveRecord configuration data (i.e. config/database.yml) to the specified file path



65
66
67
68
69
# File 'lib/projectdx_pipeline/database_configuration_template.rb', line 65

def render_to_file(path)
  File.open(path, 'w') do |f|
    YAML.dump(configuration, f)
  end
end