samidare 0.0.8 → 0.0.9

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: aa6eff2457ffc6b011ac5964a02aee5808f22178
4
- data.tar.gz: 2c9a55067831fad943ad9900894deaacd8f0dc71
3
+ metadata.gz: ec15b952226e1fe811ab80393aa6e23d078cbf0d
4
+ data.tar.gz: ea9f9a470b9c13b05891bc7c5672b36c1d1abf68
5
5
  SHA512:
6
- metadata.gz: 4cb4b1a5ab045538a6b936e11820c53df438d356528e0ca79c4b220b7997de560e0b35cf071b7bdb3f31a18f584f0810d9d83e8ecf17a6572abf1c2e5fdefc4a
7
- data.tar.gz: 622a00ae3eb3f3566d99a3415bccbfe2f67ed83d33068519d97aefe5eca591d92dc0d674748acc9716a6b4c8691328238ae09bddaf93348688cd3b97b1e2331b
6
+ metadata.gz: e1cb9d8ffacf52b9673ca9f6f192650c7202e8593aca20b2474687867326f9dcc6e8e417ad6733a43cd0c34a155dd26874eba9da0e48ba251fcb5aa3bee0807c
7
+ data.tar.gz: e4c0e30a4e533f51d0a54cb8fc41f332b16a0dbf4868470d897967fb64a39e0cf93a698fc93ad64669880cc3953e05ccff5abff6ef6131b73ba58ac01f13d9f3
@@ -2,6 +2,7 @@ require 'json'
2
2
  require 'erb'
3
3
  require 'big_query'
4
4
  require 'unindent'
5
+ require 'date'
5
6
 
6
7
  module Samidare
7
8
  class BigQueryUtility
@@ -35,6 +36,7 @@ module Samidare
35
36
 
36
37
  def initialize(config)
37
38
  @config = config.dup
39
+ @current_date = Date.today
38
40
  end
39
41
 
40
42
  def self.generate_schema(column_infos)
@@ -58,7 +60,7 @@ module Samidare
58
60
  p12_keyfile_path = @config['key']
59
61
  service_account_email = @config['service_email']
60
62
  dataset = db_info['bq_dataset']
61
- table_name = table_info.name
63
+ table_name = actual_table_name(table_info.name, db_info['daily_snapshot'] || table_info.daily_snapshot)
62
64
  schema_path = "#{@config['schema_dir']}/#{db_name}/#{table_info.name}.json"
63
65
  path_prefix = "/var/tmp/embulk_#{db_name}_#{table_info.name}"
64
66
 
@@ -71,5 +73,10 @@ module Samidare
71
73
  bq = BigQuery::Client.new(@config)
72
74
  bq.delete_table(table_name)
73
75
  end
76
+
77
+ def actual_table_name(table_name, daily_snapshot)
78
+ return table_name unless daily_snapshot
79
+ table_name + @current_date.strftime('%Y%m%d')
80
+ end
74
81
  end
75
82
  end
@@ -76,17 +76,6 @@ module Samidare
76
76
  end
77
77
 
78
78
  class DBInfo
79
- attr_reader :name, :host, :username, :password, :database, :bq_dataset
80
-
81
- def initialize(config)
82
- @name = config['name']
83
- @host = config['host']
84
- @username = config['username']
85
- @password = config['password']
86
- @database = config['database']
87
- @bq_dataset = config['bq_dataset']
88
- end
89
-
90
79
  def self.generate_db_infos
91
80
  configs = YAML.load_file('database.yml')
92
81
  configs
@@ -109,6 +98,10 @@ module Samidare
109
98
  def name
110
99
  @config['name']
111
100
  end
101
+
102
+ def daily_snapshot
103
+ @config['daily_snapshot'] || false
104
+ end
112
105
  end
113
106
 
114
107
  class ColumnInfo
@@ -1,3 +1,3 @@
1
1
  module Samidare
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
data/samidare.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'bundler', '~> 1.7'
22
22
  spec.add_development_dependency 'rake', '~> 10.0'
23
23
  spec.add_development_dependency 'rspec', '>= 2.0'
24
+ spec.add_development_dependency 'timecop', '0.8.0'
24
25
 
25
26
  spec.add_dependency 'unindent', '1.0'
26
27
  spec.add_dependency 'mysql2-cs-bind', '0.0.6'
@@ -1,41 +1,57 @@
1
1
  require 'spec_helper'
2
2
  require 'unindent'
3
+ require 'timecop'
3
4
 
4
5
  describe Samidare::BigQueryUtility do
5
6
  describe '.generate_schema' do
6
7
  subject { Samidare::BigQueryUtility.generate_schema(column_infos) }
7
8
 
8
- context '' do
9
- let(:column_infos) { [
10
- Samidare::EmbulkUtility::ColumnInfo.new('id', 'int'),
11
- Samidare::EmbulkUtility::ColumnInfo.new('name', 'varchar'),
12
- Samidare::EmbulkUtility::ColumnInfo.new('created_at', 'datetime')
13
- ] }
14
- let(:schema_json) {
15
- <<-JSON.unindent
16
- [
17
- {"name":"id","type":"integer"},
18
- {"name":"name","type":"string"},
19
- {"name":"created_at","type":"timestamp"}
20
- ]
21
- JSON
22
- }
23
- it { expect(subject).to eq schema_json }
24
- end
9
+ let(:column_infos) { [
10
+ Samidare::EmbulkUtility::ColumnInfo.new('id', 'int'),
11
+ Samidare::EmbulkUtility::ColumnInfo.new('name', 'varchar'),
12
+ Samidare::EmbulkUtility::ColumnInfo.new('created_at', 'datetime')
13
+ ] }
14
+ let(:schema_json) {
15
+ <<-JSON.unindent
16
+ [
17
+ {"name":"id","type":"integer"},
18
+ {"name":"name","type":"string"},
19
+ {"name":"created_at","type":"timestamp"}
20
+ ]
21
+ JSON
22
+ }
23
+ it { expect(subject).to eq schema_json }
25
24
  end
26
25
 
27
26
  describe '.generate_sql' do
28
27
  subject { Samidare::BigQueryUtility.generate_sql(table_name, column_infos) }
29
28
 
30
- context '' do
31
- let(:table_name) { 'simple' }
32
- let(:column_infos) { [
33
- Samidare::EmbulkUtility::ColumnInfo.new('id', 'int'),
34
- Samidare::EmbulkUtility::ColumnInfo.new('name', 'varchar'),
35
- Samidare::EmbulkUtility::ColumnInfo.new('created_at', 'datetime')
36
- ] }
37
- let(:sql) { "SELECT id,name,UNIX_TIMESTAMP(created_at) AS created_at FROM simple\n" }
38
- it { expect(subject).to eq sql }
29
+ let(:table_name) { 'simple' }
30
+ let(:column_infos) { [
31
+ Samidare::EmbulkUtility::ColumnInfo.new('id', 'int'),
32
+ Samidare::EmbulkUtility::ColumnInfo.new('name', 'varchar'),
33
+ Samidare::EmbulkUtility::ColumnInfo.new('created_at', 'datetime')
34
+ ] }
35
+ let(:sql) { "SELECT id,name,UNIX_TIMESTAMP(created_at) AS created_at FROM simple\n" }
36
+ it { expect(subject).to eq sql }
37
+ end
38
+
39
+ describe '#actual_table_name' do
40
+ before { Timecop.freeze(Time.now) }
41
+
42
+ after { Timecop.return }
43
+
44
+ subject { Samidare::BigQueryUtility.new({}).actual_table_name(table_name, daily_snapshot) }
45
+ let(:table_name) { 'users' }
46
+ let(:daily_snapshot) { false }
47
+
48
+ context 'do not use daily snapshot' do
49
+ it { expect(subject).to eq table_name }
50
+ end
51
+
52
+ context 'use daily snapshot' do
53
+ let(:daily_snapshot) { true }
54
+ it { expect(subject).to eq table_name + Time.now.strftime('%Y%m%d') }
39
55
  end
40
56
  end
41
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samidare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryoji Kobori
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: timecop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: unindent
57
71
  requirement: !ruby/object:Gem::Requirement