embulk-output-vertica 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ee9565adfe070ea8678d99eb5d94d328f5fcbe0
4
- data.tar.gz: e801c980bea0733372909d7f1672d03910f0d0f9
3
+ metadata.gz: 758fc8b31ffadedaf028b5775fd8820867e2998b
4
+ data.tar.gz: 8be72ce20cfe83be5f9d7ef827f81a0c9691cfec
5
5
  SHA512:
6
- metadata.gz: 24db1540c0ceaab5bb18af2112df702d8843fcc4c2c0ef9a43ad449e6d69bcb098e35aacf047a1c0bfeec82347f9808793d55b2ee9adadfdfeb6d5aeb09b46c4
7
- data.tar.gz: 39eed0dda265c8da99969ad8e059ee42d90e158609e31bacecefbd2e3f7235cdac0967d83301d09994c9a7cb564f9e9c92faf6da29c6ee5528ed963f6bfeb8a4
6
+ metadata.gz: 08188e9dcc7209b7317e8b4256eca1e262dcd5759d44a43a4223ea5ebb6dac8a7f205140d789816de2b1032dc95e0074a074c6e85f107e43beb7450be855614e
7
+ data.tar.gz: c7184dd4edae3f1a4c988e1ae62442ff634b72765e805e590fa365bb698594fd00e6ef5bf4ea98b39ffc2cec756726c3c2034e171f3f33d65e76446005499736
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.2.7 (2015/11/06)
2
+
3
+ Enhancements:
4
+
5
+ * Get sql schema information from existing table unless REPLACE mode
6
+
1
7
  # 0.2.6 (2015/11/06)
2
8
 
3
9
  Fixes:
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "embulk-output-vertica"
3
- spec.version = "0.2.6"
3
+ spec.version = "0.2.7"
4
4
  spec.authors = ["eiji.sekiya", "Naotoshi Seo"]
5
5
  spec.email = ["eiji.sekiya.0326@gmail.com", "sonots@gmail.com"]
6
6
  spec.summary = "Vertica output plugin for Embulk"
@@ -46,31 +46,38 @@ module Embulk
46
46
  unique_name = "%08x%08x" % [now.tv_sec, now.tv_nsec]
47
47
  task['temp_table'] = "#{task['table']}_LOAD_TEMP_#{unique_name}"
48
48
 
49
- sql_schema = self.to_sql_schema(schema, task['column_options'])
50
-
51
49
  quoted_schema = ::Jvertica.quote_identifier(task['schema'])
52
50
  quoted_table = ::Jvertica.quote_identifier(task['table'])
53
51
  quoted_temp_table = ::Jvertica.quote_identifier(task['temp_table'])
54
52
 
53
+ sql_schema ||= self.existing_sql_schema(task) unless task['mode'] == 'REPLACE'
54
+ sql_schema ||= self.to_sql_schema(schema, task['column_options'])
55
+ sql_schema_expression = sql_schema.map {|name, type| "#{::Jvertica.quote_identifier(name)} #{type}" }.join(',')
56
+
55
57
  connect(task) do |jv|
56
- if task['mode'] == 'REPLACE'
57
- query(jv, %[DROP TABLE IF EXISTS #{quoted_schema}.#{quoted_table}])
58
- end
58
+ # create a temp table
59
59
  query(jv, %[DROP TABLE IF EXISTS #{quoted_schema}.#{quoted_temp_table}])
60
- query(jv, %[CREATE TABLE #{quoted_schema}.#{quoted_temp_table} (#{sql_schema})])
60
+ query(jv, %[CREATE TABLE #{quoted_schema}.#{quoted_temp_table} (#{sql_schema_expression})])
61
61
  end
62
62
 
63
63
  begin
64
- # obtain an array of task_reports where one report is of a task
65
- task_reports = yield(task)
64
+ # insert data into a temp table
65
+ task_reports = yield(task) # obtain an array of task_reports where one report is of a task
66
66
  Embulk.logger.info { "embulk-output-vertica: task_reports: #{task_reports.to_json}" }
67
+
67
68
  connect(task) do |jv|
68
- query(jv, %[CREATE TABLE IF NOT EXISTS #{quoted_schema}.#{quoted_table} (#{sql_schema})])
69
+ # create the target table if not exists or mode == replace
70
+ if task['mode'] == 'REPLACE'
71
+ query(jv, %[DROP TABLE IF EXISTS #{quoted_schema}.#{quoted_table}])
72
+ end
73
+ query(jv, %[CREATE TABLE IF NOT EXISTS #{quoted_schema}.#{quoted_table} (#{sql_schema_expression})])
74
+ # insert select from the temp table
69
75
  query(jv, %[INSERT INTO #{quoted_schema}.#{quoted_table} SELECT * FROM #{quoted_schema}.#{quoted_temp_table}])
70
76
  jv.commit
71
77
  end
72
78
  ensure
73
79
  connect(task) do |jv|
80
+ # clean up the temp table
74
81
  query(jv, %[DROP TABLE IF EXISTS #{quoted_schema}.#{quoted_temp_table}])
75
82
  Embulk.logger.debug { "embulk-output-vertica: #{query(jv, %[SELECT * FROM #{quoted_schema}.#{quoted_table} LIMIT 10]).map {|row| row.to_h }.join("\n") rescue nil}" }
76
83
  end
@@ -166,8 +173,8 @@ module Embulk
166
173
  else
167
174
  sql_type = to_sql_type(type)
168
175
  end
169
- "#{::Jvertica.quote_identifier(column_name)} #{sql_type}"
170
- end.join(',')
176
+ [column_name, sql_type]
177
+ end.to_h
171
178
  end
172
179
 
173
180
  def self.to_sql_type(type)
@@ -181,6 +188,22 @@ module Embulk
181
188
  end
182
189
  end
183
190
 
191
+ # Get sql schema if table exists
192
+ def self.existing_sql_schema(task)
193
+ quoted_schema = Jvertica.quote(task['schema'])
194
+ quoted_table = Jvertica.quote(task['table'])
195
+ sql = "SELECT column_name, data_type FROM v_catalog.columns " \
196
+ "WHERE table_schema = #{quoted_schema} AND table_name = #{quoted_table}"
197
+
198
+ sql_schema = {}
199
+ connect(task) do |jv|
200
+ result = query(jv, sql)
201
+ sql_schema = result.map {|row| [row[0], row[1]] }.to_h
202
+ end
203
+ Embulk.logger.info "embulk-output-vertica: existing_sql_schema: #{sql_schema}"
204
+ sql_schema.empty? ? nil : sql_schema
205
+ end
206
+
184
207
  def self.query(conn, sql)
185
208
  Embulk.logger.info "embulk-output-vertica: #{sql}"
186
209
  conn.query(sql)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-output-vertica
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - eiji.sekiya