diff --git a/lib/td/command/import.rb b/lib/td/command/import.rb index 3dcd5630..fcb7977d 100644 --- a/lib/td/command/import.rb +++ b/lib/td/command/import.rb @@ -387,7 +387,7 @@ def generate_seed_confing(format, arg, options) end when 'mysql' arg = arg[1] unless arg.class == String - {'in' => parse_mysql_args(arg, options), 'out' => {'mode' => 'append'}} + generate_mysql_config(arg, options) else # NOOP end @@ -433,30 +433,33 @@ def normalize_path_prefix(path) path.gsub(/\*.*/, '') end - def parse_mysql_args(arg, options) + def generate_mysql_config(arg, options) mysql_url_regexp = Regexp.new("[jdbc:]*mysql://(?[^:/]*)[:]*(?[^/]*)/(?.*)") + config = if (match = mysql_url_regexp.match(options['db_url'])) { - 'type' => 'mysql', 'host' => match['host'], 'port' => match['port'] == '' ? 3306 : match['port'].to_i, 'database' => match['db_name'], } else { - 'type' => 'mysql', 'host' => '', 'port' => 3306, 'database' => '', } end - config.merge( - 'user' => options['db_user'], - 'password' => options['db_password'], - 'table' => arg, - 'select' => '*', - ) + { + 'in' => config.merge( + 'type' => 'mysql', + 'user' => options['db_user'], + 'password' => options['db_password'], + 'table' => arg, + 'select' => '*', + ), + 'out' => td_output_config, + } end end diff --git a/spec/td/command/import_spec.rb b/spec/td/command/import_spec.rb index 6f36d5dc..7e0b54c1 100644 --- a/spec/td/command/import_spec.rb +++ b/spec/td/command/import_spec.rb @@ -204,6 +204,15 @@ module TreasureData::Command end context 'support format' do + let(:td_output_config) { + { + 'type' => 'td', + 'endpoint' => Config.endpoint, + 'apikey' => Config.apikey, + 'database' => '', + 'table' => '', + } + } before do command.import_config(option) end @@ -224,13 +233,7 @@ module TreasureData::Command 'decorders' => [{'type' => 'gzip'}], 'path_prefix' => path_prefix, }, - 'out' => { - 'type' => 'td', - 'endpoint' => Config.endpoint, - 'apikey' => Config.apikey, - 'database' => '', - 'table' => '', - } + 'out' => td_output_config }) end end @@ -281,9 +284,7 @@ module TreasureData::Command 'table' => table, 'select' => "*", }, - 'out' => { - 'mode' => 'append' - } + 'out' => td_output_config } }