Class: MultiCompress::DBDeployment::RegistryCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_compress/db_deployment.rb

Constant Summary collapse

TARGETS =
%w[postgres mysql].freeze
IDENTIFIER =
/\A[A-Za-z_][A-Za-z0-9_]*\z/.freeze

Instance Method Summary collapse

Instance Method Details

#run(argv) ⇒ Object

Raises:

  • (OptionParser::MissingArgument)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/multi_compress/db_deployment.rb', line 172

def run(argv)
  target = argv.shift
  raise OptionParser::MissingArgument, "TARGET must be postgres or mysql" if target.nil?
  raise OptionParser::InvalidArgument, "TARGET must be postgres or mysql" unless TARGETS.include?(target)

  options = { output: nil, extension_schema: "multi_compress", table: "mcdb_dictionary_versions", heads_table: "mcdb_dictionary_heads" }
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: multi_compress db registry #{target} [OPTIONS]"
    if target == "postgres"
      opts.on("--schema NAME", "application schema") { |value| options[:schema] = value }
      opts.on("--owner ROLE", "NOLOGIN owner for append-only dictionary versions") { |value| options[:owner] = value }
      opts.on("--migration-role ROLE", "role allowed to insert dictionary versions") { |value| options[:migration_role] = value }
      opts.on("--extension-schema NAME", "extension schema (default: multi_compress)") { |value| options[:extension_schema] = value }
    else
      opts.on("--database NAME", "application database") { |value| options[:schema] = value }
    end
    opts.on("--table NAME", "versions table (default: mcdb_dictionary_versions)") { |value| options[:table] = value }
    opts.on("--heads-table NAME", "heads table (default: mcdb_dictionary_heads)") { |value| options[:heads_table] = value }
    opts.on("--payload-table NAME", "MCDB2 source table in this schema/database") { |value| options[:payload_table] = value }
    opts.on("--payload-column NAME", "MCDB2 compressed envelope column on --payload-table") { |value| options[:payload_column] = value }
    opts.on("--payload-dictionary-id-column NAME", "MCDB2 dictionary FK column on --payload-table") { |value| options[:payload_dictionary_id_column] = value }
    opts.on("-o", "--output PATH", "write SQL to PATH instead of stdout") { |value| options[:output] = value }
    opts.on("-h", "--help", "show this help") { puts opts; return 0 }
  end
  parser.parse!(argv)
  raise OptionParser::InvalidOption, argv.join(" ") unless argv.empty?
  sql = DictionaryRegistry.new(target, options).to_sql
  if options[:output]
    ViewCommand.new.send(:write_text_atomically, options[:output], sql)
    puts File.expand_path(options[:output])
  else
    $stdout.write(sql)
  end
  0
end