Sha256: 302955862cd1dbf7653b17aa44e4a2e0b3a100ee49d77959b54d6fb5f6eb62f9

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

class ApplicationDatabase
  class ConfigurationError < StandardError
    def initialize(message)
      AwsLog.error "#{self.class.name} exception: #{message}"
      super
    end
  end

  class MissingConfigurationKeys < ConfigurationError
    def initialize(missing_keys)
      message = "#{missing_keys.join(' and ')} must be specified in config/database.yml"
      super(message)
    end
  end

  attr_accessor :adapter
  RequiredKeys = %w{username database adapter}

  def initialize
    @adapter = case db_config["adapter"]
               when "postgresql"
                 PostgresAdapter.new(db_config)
               when "mysql"
                 MysqlAdapter.new(db_config)
               end
  rescue ConfigurationError
    @adapter = Struct.new(:contents).new(nil)
  end

  def db_config
    config = ActiveRecord::Base.configurations[Rails.env]
    missing_keys = RequiredKeys - config.keys
    raise MissingConfigurationKeys.new(missing_keys) unless missing_keys.empty?
    config
  end

  delegate :contents, :restore, :to => :adapter
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glacier_on_rails-1.0.0 app/models/application_database.rb