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
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/multi_compress/db_deployment.rb', line 113
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 = {
columns: [], as: "payload", output: nil, extension_schema: "multi_compress",
dictionary_table: nil, dictionary_id_column: nil,
registry_id_column: "id", dictionary_sha256_column: "sha256",
dictionary_bytes_column: "bytes"
}
parser = OptionParser.new do |opts|
opts.banner = "Usage: multi_compress db view #{target} --table SCHEMA.TABLE --column COLUMN --view SCHEMA.VIEW --columns ID,CREATED_AT [OPTIONS]"
opts.on("--table NAME", "source table, optionally schema-qualified") { |value| options[:table] = value }
opts.on("--column NAME", "compressed bytea/BLOB column") { |value| options[:column] = value }
opts.on("--view NAME", "destination view, optionally schema-qualified") { |value| options[:view] = value }
opts.on("--columns LIST", "comma-separated plain columns to expose") { |value| options[:columns] = value.split(",").map(&:strip) }
opts.on("--as NAME", "decoded column name (default: payload)") { |value| options[:as] = value }
opts.on("--dictionary-table NAME", "MCDB2 immutable dictionary registry table") { |value| options[:dictionary_table] = value }
opts.on("--dictionary-id-column NAME", "MCDB2 FK column on source table") { |value| options[:dictionary_id_column] = value }
opts.on("--registry-id-column NAME", "registry PK (default: id)") { |value| options[:registry_id_column] = value }
opts.on("--dictionary-sha256-column NAME", "registry SHA-256 column (default: sha256)") { |value| options[:dictionary_sha256_column] = value }
opts.on("--dictionary-bytes-column NAME", "registry dictionary bytes column (default: bytes)") { |value| options[:dictionary_bytes_column] = value }
if target == "postgres"
opts.on("--extension-schema NAME", "extension schema (default: multi_compress)") { |value| options[:extension_schema] = value }
end
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 = ReadableView.new(target, options).to_sql
if options[:output]
write_text_atomically(options[:output], sql)
puts File.expand_path(options[:output])
else
$stdout.write(sql)
end
0
end
|