Class: Dbcp::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/dbcp/cli.rb

Constant Summary collapse

DEFAULT_DESTINATION =
'development'

Instance Method Summary collapse

Constructor Details

#initialize(stdout = $stdout) ⇒ Cli

Returns a new instance of Cli.



5
6
7
8
9
10
# File 'lib/dbcp/cli.rb', line 5

def initialize(stdout = $stdout)
  @logger = Logger.new stdout
  @logger.formatter = Proc.new do |severity, datetime, progname, msg|
    "#{datetime}: #{msg}\n"
  end
end

Instance Method Details

#start(argv) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dbcp/cli.rb', line 12

def start(argv)
  if argv.length < 1
    usage
    exit 1
  end

  begin
    source = Environment.find(argv.shift)
    destination = Environment.find(argv.shift || DEFAULT_DESTINATION)

    if source == destination
      @logger.fatal "source and destination environments are the same"
      exit 3
    end

    if source.database.adapter != destination.database.adapter
      @logger.fatal "source (#{source.database.adapter}) and destination (#{destination.database.adapter}) environments must be the same database type"
      exit 4
    end
  rescue EnvironmentNotFound => e
    @logger.fatal e.to_s
    exit 2
  end

  @logger.info "exporting #{source.environment_name}..."
  source_snapshot_file = source.export

  @logger.info "transferring data..."
  destination_snapshot_file = source_snapshot_file.transfer_to(destination)

  @logger.info "importing #{destination_snapshot_file.path} to #{destination.environment_name}..."
  destination.import destination_snapshot_file

  source_snapshot_file.delete
  destination_snapshot_file.delete if source_snapshot_file != destination_snapshot_file
end

#usageObject



49
50
51
# File 'lib/dbcp/cli.rb', line 49

def usage
  @logger.fatal "Usage: #{$0} source_environment [destination_environment || #{DEFAULT_DESTINATION}]"
end