naginegi 0.1.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f1407ac3ed8008b5fe9016cc6b5497d7e4670c6a3053e82717bbca7ddcafe31
4
- data.tar.gz: db73972c63544d2ee87ae2df508f38b813cddce5e6e6a8bf8c83012a5fa96a3c
3
+ metadata.gz: 8d04a1b61b8907c2b7d03ec4be2ea36097f44c52fca562681481d154314ac87f
4
+ data.tar.gz: 963141aa4c7ca6144dd9fc553e518af27be576907e8cb7e95865bed87bda8dc3
5
5
  SHA512:
6
- metadata.gz: 96793bf87478c3ae07b1d6338813a2fd1f49e89d9b016c02f94bbfd7d250885ca67e24c2851bb52ed067c62cea4f317f8431577569c7f3dc461c325f549c1eec
7
- data.tar.gz: 5c9d7a190cd1afd1d3561c90ca7d1c5b1491ef886f4a5ddadfdfbfb2371ddcea46241758b7b328569e7d105f400364131258eb65bef389f20f2aac819605d1dc
6
+ metadata.gz: 1368397fccb115f3785c485e8ef9b456e99ea7ca4310c0961a9f83dd67cbcec51295561cfe7d74a87f3b62adbd006a552c31bf99c80b72f7e65abc8dfc9f543d
7
+ data.tar.gz: 9fa489733086e3b59e46b27c9c3aaa48b5ab520a29ddffb3bfc9fb945766d37dd791ae4eabcf7759d7c24e6be4c0596cad67b2d17ec97ad30b94adae380ae30f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- naginegi (0.1.0)
4
+ naginegi (0.3.0)
5
5
  google-cloud-bigquery (= 1.18.1)
6
6
  mysql2 (= 0.5.3)
7
7
  mysql2-cs-bind (= 0.0.7)
@@ -53,7 +53,6 @@ GEM
53
53
  mini_mime (1.0.2)
54
54
  multi_json (1.14.1)
55
55
  multipart-post (2.1.1)
56
- mysql2 (0.5.3)
57
56
  mysql2 (0.5.3-x64-mingw32)
58
57
  mysql2-cs-bind (0.0.7)
59
58
  mysql2
@@ -61,7 +60,6 @@ GEM
61
60
  parallel (1.19.1)
62
61
  parser (2.7.0.1)
63
62
  ast (~> 2.4.0)
64
- pg (1.2.2)
65
63
  pg (1.2.2-x64-mingw32)
66
64
  powerpack (0.1.2)
67
65
  public_suffix (4.0.3)
data/README.md CHANGED
@@ -77,8 +77,10 @@ db02:
77
77
  - name: configs
78
78
  ```
79
79
 
80
+ ### sample
80
81
  Naginegi requires BigQuery parameters like below.
81
82
 
83
+ #### using json key file path
82
84
  ```ruby
83
85
  [sample.rb]
84
86
  require 'naginegi'
@@ -92,7 +94,38 @@ config = {
92
94
  'config_dir' => '/var/tmp/embulk/config'
93
95
  }
94
96
 
95
- client = Naginegi::EmbulkClient.new
97
+ client = Naginegi::EmbulkRunner.new
98
+ client.generate_config(config)
99
+ client.run(config)
100
+ ```
101
+
102
+ ```bash
103
+ ruby sample.rb
104
+ ```
105
+
106
+ #### using key values
107
+ ```ruby
108
+ [sample.rb]
109
+ require 'naginegi'
110
+
111
+ json_key = {
112
+ "type" => "...",
113
+ "project_id" => "...",
114
+ "private_key_id" => "...",
115
+ "private_key" => "...",
116
+ ...
117
+ }
118
+
119
+ config = {
120
+ 'project_id' => 'BIGQUERY_PROJECT_ID',
121
+ 'service_email' => 'SERVICE_ACCOUNT_EMAIL',
122
+ 'auth_method' => 'json_key',
123
+ 'json_key' => json_key,
124
+ 'schema_dir' => '/var/tmp/embulk/schema',
125
+ 'config_dir' => '/var/tmp/embulk/config'
126
+ }
127
+
128
+ client = Naginegi::EmbulkRunner.new
96
129
  client.generate_config(config)
97
130
  client.run(config)
98
131
  ```
@@ -7,13 +7,17 @@ require 'logger'
7
7
 
8
8
  module Naginegi
9
9
  class EmbulkRunner
10
- def initialize
10
+ def initialize(db_configs: nil, log_level: 'warn', embulk_run_option: '')
11
11
  @logger = Logger.new(STDOUT)
12
12
  @logger.datetime_format = '%Y-%m-%d %H:%M:%S'
13
+
14
+ @db_configs = db_configs || YAML.load_file('database.yml')
15
+ @log_level = log_level
16
+ @embulk_run_option = embulk_run_option
13
17
  end
14
18
 
15
19
  def generate_config(bq_config)
16
- Naginegi::EmbulkConfig.new.generate_config(db_configs, bq_config)
20
+ Naginegi::EmbulkConfig.new.generate_config(@db_configs, bq_config)
17
21
  end
18
22
 
19
23
  def run(bq_config, target_table_names = [], retry_max = 0)
@@ -31,8 +35,8 @@ module Naginegi
31
35
  private
32
36
 
33
37
  def run_and_retry(bq_config, target_table_names, retry_max, retry_count)
34
- error_tables = Naginegi::Embulk.new.run(
35
- db_configs,
38
+ error_tables = Naginegi::Embulk.new(@log_level, @embulk_run_option).run(
39
+ @db_configs,
36
40
  table_configs,
37
41
  bq_config,
38
42
  target_table_names
@@ -46,12 +50,8 @@ module Naginegi
46
50
  error_tables
47
51
  end
48
52
 
49
- def db_configs
50
- @db_configs ||= YAML.load_file('database.yml')
51
- end
52
-
53
53
  def table_configs
54
- @table_configs ||= Naginegi::MySQL::TableConfig.generate_table_configs
54
+ @table_configs ||= Naginegi::TableConfig.generate_table_configs
55
55
  end
56
56
  end
57
57
  end
@@ -9,17 +9,20 @@ module Naginegi
9
9
  CONTENTS = <<-EOS.unindent
10
10
  in:
11
11
  type: <%= db_type %>
12
+ host: <%= host %>
12
13
  user: <%= user %>
13
14
  password: <%= password %>
14
15
  database: <%= database %>
15
- host: <%= host %>
16
+ ssl: <%= ssl %>
16
17
  query: |
17
18
  <%= query %>
18
19
  <%= options %>
19
20
  out:
20
21
  type: bigquery
22
+ mode: replace
21
23
  auth_method: <%= auth_method %>
22
24
  json_keyfile: <%= json_keyfile %>
25
+ <%= json_key_content %>
23
26
  project: <%= project %>
24
27
  service_account_email: <%= service_account_email %>
25
28
  dataset: <%= dataset %>
@@ -61,6 +64,7 @@ module Naginegi
61
64
  user = db_config['username']
62
65
  password = db_config['password']
63
66
  database = db_config['database']
67
+ ssl = db_config['embulk_ssl_enable'] || false
64
68
  options = if db_type == 'mysql'
65
69
  "options: {useLegacyDatetimeCode: false, serverTimezone: #{db_config['timezone']}}"
66
70
  else
@@ -69,7 +73,15 @@ module Naginegi
69
73
  query = Naginegi::BigQuery.generate_sql(table_config, columns)
70
74
 
71
75
  auth_method = @config['auth_method']
72
- json_keyfile = @config['json_keyfile']
76
+ if @config['json_key']
77
+ values = @config['json_key'].map do |k, v|
78
+ value = v.gsub("\n", '\\n')
79
+ "\"#{k}\": \"#{value}\""
80
+ end
81
+ json_key_content = "content: |\n {#{values.join(',')}}"
82
+ else
83
+ json_keyfile = @config['json_keyfile']
84
+ end
73
85
  project = @config['project_id']
74
86
  service_account_email = @config['service_email']
75
87
  dataset = db_config['bq_dataset']
@@ -81,9 +93,17 @@ module Naginegi
81
93
  end
82
94
 
83
95
  def delete_table(dataset, table_name)
96
+ keyfile = if @config['json_key']
97
+ value = @config['json_key'].dup
98
+ value['private_key'] = value['private_key'].gsub('\\n', "\n")
99
+ value
100
+ else
101
+ @config['json_keyfile']
102
+ end
103
+
84
104
  bq = Google::Cloud::Bigquery.new(
85
105
  project: @config['project_id'],
86
- keyfile: @config['json_keyfile']
106
+ keyfile: keyfile
87
107
  )
88
108
  bq.service.delete_table(dataset, table_name)
89
109
  end
@@ -2,9 +2,12 @@ require 'logger'
2
2
 
3
3
  module Naginegi
4
4
  class Embulk
5
- def initialize
5
+ def initialize(log_level, embulk_run_option)
6
6
  @logger = Logger.new(STDOUT)
7
7
  @logger.datetime_format = '%Y-%m-%d %H:%M:%S'
8
+
9
+ @log_level = log_level
10
+ @embulk_run_option = embulk_run_option
8
11
  end
9
12
 
10
13
  def run(db_configs, all_table_configs, bq_config, target_table_names = [])
@@ -45,7 +48,7 @@ module Naginegi
45
48
  @logger.warn(e.message)
46
49
  end
47
50
 
48
- cmd = "embulk run #{bq_config['config_dir']}/#{db_name}/#{table_config.name}.yml"
51
+ cmd = "embulk run #{@embulk_run_option} #{bq_config['config_dir']}/#{db_name}/#{table_config.name}.yml --log-level #{@log_level}"
49
52
  @logger.info("cmd: #{cmd}")
50
53
 
51
54
  if system(cmd)
@@ -43,7 +43,31 @@ module Naginegi
43
43
  end
44
44
 
45
45
  def all_table_configs
46
- @all_table_configs ||= MySQL::TableConfig.generate_table_configs
46
+ @all_table_configs ||= Naginegi::TableConfig.generate_table_configs
47
+ end
48
+ end
49
+
50
+ class TableConfig
51
+ attr_reader :name, :daily_snapshot, :condition
52
+
53
+ def initialize(config)
54
+ @name = config['name']
55
+ @daily_snapshot = config['daily_snapshot'] || false
56
+ @condition = config['condition']
57
+ end
58
+
59
+ def self.generate_table_configs(file_path = 'table.yml')
60
+ configs = YAML.load_file(file_path)
61
+ configs.each_with_object({}) do |(db, database_config), table_configs|
62
+ table_configs[db] = database_config['tables'].map { |config| TableConfig.new(config) }
63
+ table_configs
64
+ end
65
+ end
66
+
67
+ def ==(other)
68
+ instance_variables.all? do |v|
69
+ instance_variable_get(v) == other.instance_variable_get(v)
70
+ end
47
71
  end
48
72
  end
49
73
  end
@@ -39,30 +39,6 @@ module Naginegi
39
39
  end
40
40
  end
41
41
 
42
- class TableConfig
43
- attr_reader :name, :daily_snapshot, :condition
44
-
45
- def initialize(config)
46
- @name = config['name']
47
- @daily_snapshot = config['daily_snapshot'] || false
48
- @condition = config['condition']
49
- end
50
-
51
- def self.generate_table_configs(file_path = 'table.yml')
52
- configs = YAML.load_file(file_path)
53
- configs.each_with_object({}) do |(db, database_config), table_configs|
54
- table_configs[db] = database_config['tables'].map { |config| TableConfig.new(config) }
55
- table_configs
56
- end
57
- end
58
-
59
- def ==(other)
60
- instance_variables.all? do |v|
61
- instance_variable_get(v) == other.instance_variable_get(v)
62
- end
63
- end
64
- end
65
-
66
42
  class Column
67
43
  attr_reader :column_name, :data_type
68
44
 
@@ -90,7 +66,7 @@ module Naginegi
90
66
  end
91
67
 
92
68
  def bigquery_data_type
93
- TYPE_MAPPINGS[@data_type]
69
+ TYPE_MAPPINGS[@data_type] || 'STRING'
94
70
  end
95
71
 
96
72
  def converted_value
@@ -38,30 +38,6 @@ module Naginegi
38
38
  end
39
39
  end
40
40
 
41
- class TableConfig
42
- attr_reader :name, :daily_snapshot, :condition
43
-
44
- def initialize(config)
45
- @name = config['name']
46
- @daily_snapshot = config['daily_snapshot'] || false
47
- @condition = config['condition']
48
- end
49
-
50
- def self.generate_table_configs(file_path = 'table.yml')
51
- configs = YAML.load_file(file_path)
52
- configs.each_with_object({}) do |(db, db_config), table_configs|
53
- table_configs[db] = db_config['tables'].map { |config| TableConfig.new(config) }
54
- table_configs
55
- end
56
- end
57
-
58
- def ==(other)
59
- instance_variables.all? do |v|
60
- instance_variable_get(v) == other.instance_variable_get(v)
61
- end
62
- end
63
- end
64
-
65
41
  class Column
66
42
  attr_reader :column_name, :data_type
67
43
 
@@ -91,7 +67,7 @@ module Naginegi
91
67
  end
92
68
 
93
69
  def bigquery_data_type
94
- TYPE_MAPPINGS[@data_type]
70
+ TYPE_MAPPINGS[@data_type] || 'STRING'
95
71
  end
96
72
 
97
73
  def converted_value
@@ -1,3 +1,3 @@
1
1
  module Naginegi
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naginegi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cobot00
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-14 00:00:00.000000000 Z
11
+ date: 2020-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler