Class: PgMigrate::CommandLine

Inherits:
Thor
  • Object
show all
Defined in:
lib/pg_migrate/command_line.rb

Constant Summary collapse

@@packaged_source =
'.'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CommandLine

Returns a new instance of CommandLine.



6
7
8
# File 'lib/pg_migrate/command_line.rb', line 6

def initialize(*args)
  super
end

Class Method Details

.packaged_sourceObject



10
11
12
# File 'lib/pg_migrate/command_line.rb', line 10

def self.packaged_source
  @@packaged_source
end

.packaged_source=(value) ⇒ Object



14
15
16
# File 'lib/pg_migrate/command_line.rb', line 14

def self.packaged_source=(value)
  @@packaged_source = value
end

Instance Method Details

#buildObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pg_migrate/command_line.rb', line 87

def build
  source = options[:source]

  if source.nil?
    source = @@packaged_source
  end

  method_defaults = {"force" => false, "verbose" => false, "test" => false}
  local_options = set_defaults_from_file(method_defaults, "build", source)
  local_options = local_options.merge(options)

  bootstrap_logger(local_options["verbose"])

  if !local_options["out"]
    puts "error: --out not specified"
    exit 1
  end

  if local_options["test"].to_s == "true"
    if !local_options["oob_connopts"]
      puts "error: --oob_connopts not specified when test = true"
      exit 1
    else
      # type safety; if string is found, convert to hash
      local_options["oob_connopts"] = parse_to_hash(local_options["oob_connopts"])
    end


    if !local_options["connopts"]
      puts "error: --connopts not specified when test = true"
      exit 1
    else
      # type safety; if string is found, convert to hash
      local_options["connopts"] = parse_to_hash(local_options["connopts"])
    end

  end

  manifest_reader = ManifestReader.new
  sql_reader = SqlReader.new
  builder = Builder.new(manifest_reader, sql_reader)

  begin
  builder.build(source, local_options["out"],
                :force => local_options["force"],
                :test => local_options["test"],
                :oob_connopts => local_options["oob_connopts"],
                :connopts => local_options["connopts"])
  rescue PG::Error => pge
    puts "test failure"
    puts pge
    exit 1
  end

end

#downObject



70
71
72
73
74
75
76
# File 'lib/pg_migrate/command_line.rb', line 70

def down
  local_options = options
  options = set_defaults_from_file(location_options)
  bootstrap_logger(options[:verbose])

  raise 'Not implemented'
end

#packageObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pg_migrate/command_line.rb', line 152

def package
  source = options[:source]

  if source.nil?
    source = @@packaged_source
  end

  method_defaults = {"force" => false, "verbose" => false}
  local_options = set_defaults_from_file(method_defaults, "package", source)
  local_options = local_options.merge(options)

  if !local_options["out"]
    puts "error: --out not specified"
    exit 1
  end
  if !local_options["name"]
    puts "error: --version not specified"
    exit 1
  end
  if !local_options["version"]
    puts "error: --version not specified"   
    exit 1
  end

  bootstrap_logger(local_options["verbose"])

  manifest_reader = ManifestReader.new
  builder = Package.new(manifest_reader)
  builder.package(source, local_options["out"], local_options["name"], local_options["version"], :force => local_options["force"])
end

#upObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pg_migrate/command_line.rb', line 24

def up
  source = options[:source]

  if source.nil?
    source = @@packaged_source
  end

  method_defaults = {"verbose" => false}
  local_options = set_defaults_from_file(method_defaults, "up", source)
  local_options = local_options.merge(options)

  bootstrap_logger(local_options["verbose"])

  manifest_reader = ManifestReader.new
  sql_reader = SqlReader.new

  connopts = local_options["connopts"]
  connopts ||= {}

  if !connopts["port"].nil?
    connopts["port"] = connopts["port"].to_i
  end

  migrator = Migrator.new(manifest_reader, sql_reader, connopts)

  begin
    migrator.migrate(source)
  rescue Exception => e
    if !local_options["verbose"]
      # catch common exceptions and make pretty on command-line
      if !e.message.index("ManifestReader: code=unloadable_manifest").nil?
        puts "Unable to load manifest in source directory '#{source}' .  Check -s|--source option and run again."
        exit 1
      else
        raise e
      end
    else
      raise e
    end
  end


end