Sha256: 2d291b0c9b2a3dfed716c41d088b3aaf424f7b0a10383b5e71ebe84ffe9b91ff

Contents?: true

Size: 1.59 KB

Versions: 11

Compression:

Stored size: 1.59 KB

Contents

require 'rubygems'
require 'highline'
require 'geordi/interaction'
require 'geordi/util'

module Geordi
  class DumpLoader
    include Geordi::Interaction

    def initialize(file)
      @dump_file = file
    end

    def development_database_config
      require 'yaml'

      @config ||= YAML::load(ERB.new(File.read('config/database.yml')).result)
      @config['development']
    end
    alias_method :config, :development_database_config
  
    def mysql_command
      command = 'mysql --silent'
      command << ' -p' << config['password']
      command << ' -u' << config['username']
      command << ' --default-character-set=utf8'
      command << ' ' << config['database']
      command << ' < ' << dump_file
    end
    alias_method :mysql2_command, :mysql_command

    def postgresql_command
      ENV['PGPASSWORD'] = config['password']
      command = 'pg_restore --no-owner --clean'
      command << ' --username=' << config['username']
      command << ' --host=' << config['host']
      command << ' --dbname=' << config['database']
      command << ' ' << dump_file
    end

    def dump_file
      @dump_file ||= begin
        dumps_glob = File.join(File.expand_path('~'), 'dumps', '*.dump')
        available_dumps = Dir.glob(dumps_glob).sort

        HighLine.new.choose(*available_dumps) do |menu|
          menu.hidden('') { fail 'Abort.' }
        end
      end
    end

    def load
      note 'Source file: ' + dump_file

      source_command = send("#{config['adapter']}_command")
      Util.system! source_command, :fail_message => "An error occured loading #{File.basename(dump_file)}"
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
geordi-1.2.1 lib/geordi/dump_loader.rb
geordi-1.2.0 lib/geordi/dump_loader.rb
geordi-1.1.4 lib/geordi/dump_loader.rb
geordi-1.1.3 lib/geordi/dump_loader.rb
geordi-1.1.2 lib/geordi/dump_loader.rb
geordi-1.1.1 lib/geordi/dump_loader.rb
geordi-1.1.0 lib/geordi/dump_loader.rb
geordi-1.0.3 lib/geordi/dump_loader.rb
geordi-1.0.2 lib/geordi/dump_loader.rb
geordi-1.0.1 lib/geordi/dump_loader.rb
geordi-1.0.0 lib/geordi/dump_loader.rb