Class: SqlMigrate::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



15
16
17
# File 'lib/sql_migrate/cli.rb', line 15

def initialize
  @config = Config.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/sql_migrate/cli.rb', line 7

def config
  @config
end

Class Method Details

.run(argv = ARGV) ⇒ Object



9
10
11
12
13
# File 'lib/sql_migrate/cli.rb', line 9

def self.run(argv = ARGV)
  cli = new
  cli.parse(argv)
  cli.execute
end

Instance Method Details

#executeObject



44
45
46
47
# File 'lib/sql_migrate/cli.rb', line 44

def execute
  migrator = Migrator.new(config)
  migrator.migrate
end

#parse(argv) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sql_migrate/cli.rb', line 19

def parse(argv)
  args = argv.dup
  options = {}
  OptionParser.new do |opt|
    opt.version = VERSION
    opt.banner = "sql_migrate [options] MIGRATIONS_PATH"
    opt.on("-h HOST")         { |v| options[:host]     = v }
    opt.on("-p PORT")         { |v| options[:port]     = v }
    opt.on("-d DATABASE")     { |v| options[:database] = v }
    opt.on("-u USER")         { |v| options[:user]     = v }
    opt.on("-p PASSWORD")     { |v| options[:password] = v }
    opt.on("-v", "--verbose") { |v| options[:verbose]  = v }
    opt.on("-n", "--dry-run") { |v| options[:dryrun]   = v }
    opt.on("--applied")       { |v| options[:applied]  = v }
    opt.on("-f CONFIG")       { |v| options[:config]   = v }
    opt.parse!(args)
  end

  load_from_config(options.delete(:config)) if options.has_key?(:config)
  config.merge(options)
  config.migration_paths = args unless args.empty?

  args
end