Sha256: 5397c4c56c59eb1e3ca35119673eb6c4490f4545a086ef6c25ca1e62a6161d50

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module Dbcp
  class EnvironmentNotFound < StandardError; end
  class ExecutionError      < StandardError; end

  class Environment
    ENVIRONMENT_PROVIDERS = [
      DatabaseYamlEnvironmentProvider.new('config/database.yml'),
      UriEnvironmentProvider.new,
      (Capistrano3EnvironmentProvider.new if defined?(Capistrano3EnvironmentProvider.new))
    ].compact

    class << self
      def find(environment_name)
        ENVIRONMENT_PROVIDERS.each do |provider|
          environment = provider.find environment_name
          return environment if environment
        end

        raise EnvironmentNotFound.new "Could not locate '#{environment_name}' environment"
      end
    end

    # coersion causes issues when assigning rspec doubles
    include Virtus.value_object(coerce: false)
    values do
      attribute :environment_name, String
      attribute :database,         Database
      attribute :execution_host,   ExecutionHost
    end

    def export
      DatabaseSnapshotFile.new(self).tap do |snapshot_file|
        execution_host.execute database.export_command(snapshot_file)
      end
    end

    def import(snapshot_file)
      execution_host.execute database.import_command(snapshot_file)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dbcp-0.2.1 lib/dbcp/environment.rb
dbcp-0.2.0 lib/dbcp/environment.rb