clickhouse-rails 0.1.4 → 0.1.5

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
  SHA256:
3
- metadata.gz: eb6df85b3e7c3c3a379067d3cb22e31afaa164f7277defa24362a06e71fea7b3
4
- data.tar.gz: 57f2f1aa8cf5a3a125341944a3eb7d8a2765111a2a2a9bde15dc7766644fda1c
3
+ metadata.gz: 718aac003d13c2483b97702f3f9f3e03bd2e10416c6bec748c7571a189c630f1
4
+ data.tar.gz: 60761d44e0f1c8b2e235212812cf3aabdd36212a8ff99a2d47ba60688f162f06
5
5
  SHA512:
6
- metadata.gz: 387f1a3dfd4822e1a83cad4994429b1a2f0d0716de68c623ddc52b5785b469c50a1e0f0bc602a234ef865bedd4e2c1d99979bb6ee05927522ce9e30ea9c2c5e6
7
- data.tar.gz: 224d2b0a199ddf8ad88a948926c343fe6b66b9f2aa3013b7613259f4222187a3a8a07709bdcc5caccdd4c12423352f89d6426bb96706bf3d2a909ae638313262
6
+ metadata.gz: b29b6bd85c036c8daf286c994c2881c79e1a3765b0c742646f0fcc1e3a64bf84ff39777468d3c2c81ec342d26580867491ab821479f69cf9f13fe2c17ff2dd13
7
+ data.tar.gz: da05288e90ba6c12eeecb43ac196c48dcd711981b7e5c0cd46b600925c13f9ddbdc94a04158df98787e35be54de2d15737b8a61128c179a308d84bacc2e7d5b9
@@ -1,3 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - './tmp/**/*.rb'
5
+
1
6
  Metrics/BlockLength:
2
7
  Max: 25
3
8
  Exclude:
@@ -12,3 +17,6 @@ Style/Documentation:
12
17
 
13
18
  Metrics/LineLength:
14
19
  Max: 97
20
+
21
+ Style/FrozenStringLiteralComment:
22
+ Enabled: false
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- clickhouse-rails (0.1.3)
4
+ clickhouse-rails (0.1.5)
5
5
  clickhouse
6
6
  railties
7
7
 
@@ -80,7 +80,7 @@ GEM
80
80
  coderay (~> 1.1.0)
81
81
  method_source (~> 0.9.0)
82
82
  psych (3.1.0)
83
- public_suffix (3.1.0)
83
+ public_suffix (3.1.1)
84
84
  rack (2.0.7)
85
85
  rack-protection (2.0.5)
86
86
  rack
@@ -55,8 +55,15 @@ module Clickhouse
55
55
  drop_table(table_name)
56
56
  end
57
57
 
58
- delegate :create_table, to: :connection
59
- delegate :drop_table, to: :connection
58
+ def create_table(table_name, &block)
59
+ logger.info "# >======= Create #{table_name} ========"
60
+ connection.create_table(table_name, &block)
61
+ end
62
+
63
+ def drop_table(table_name, &block)
64
+ logger.info "# >======= Drop #{table_name} ========"
65
+ connection.drop_table(table_name, &block)
66
+ end
60
67
 
61
68
  def alter_table(table_name)
62
69
  @table_info = connection.describe_table(table_name)
@@ -73,7 +80,9 @@ module Clickhouse
73
80
  end
74
81
  type = type.gsub('Uint', 'UInt').delete('_')
75
82
 
76
- connection.execute("ALTER TABLE #{@table_name} ADD COLUMN #{column} #{type}")
83
+ query = "ALTER TABLE #{@table_name} ADD COLUMN #{column} #{type}"
84
+ logger.info(query)
85
+ connection.execute(query)
77
86
  end
78
87
 
79
88
  def run_migration(migration)
@@ -92,7 +101,7 @@ module Clickhouse
92
101
  end
93
102
 
94
103
  def logger
95
- ::Rails.logger
104
+ Logger.new(STDOUT)
96
105
  end
97
106
  end
98
107
  end
@@ -1,5 +1,5 @@
1
1
  module Clickhouse
2
2
  module Rails
3
- VERSION = '0.1.4'.freeze
3
+ VERSION = '0.1.5'.freeze
4
4
  end
5
5
  end
@@ -20,7 +20,7 @@ module Clickhouse
20
20
  [complete_row]
21
21
  end
22
22
  end
23
- alias :create :insert_row
23
+ alias create insert_row
24
24
 
25
25
  def insert_rows(batch_rows)
26
26
  connection.insert_rows(table_name) do |table_rows|
@@ -64,14 +64,38 @@ module Clickhouse
64
64
  end
65
65
 
66
66
  def prepare_row(row)
67
- return empty_row.merge(row.stringify_keys) if row.is_a?(Hash)
67
+ validate_row(row)
68
68
 
69
- raise WrongTypeRowError, "#{row.inspect} has wrong type"
69
+ row.stringify_keys!
70
+ crop_row(row)
71
+
72
+ empty_row.merge(row)
70
73
  end
71
74
 
72
75
  def connection
73
76
  ::Clickhouse.connection
74
77
  end
78
+
79
+ def logger
80
+ ::Rails.logger
81
+ end
82
+
83
+ private
84
+
85
+ def validate_row(row)
86
+ return if row.is_a?(Hash)
87
+
88
+ raise WrongTypeRowError, "#{row.inspect} has wrong type"
89
+ end
90
+
91
+ def crop_row(row)
92
+ undefined_columns = row.keys - empty_row.keys
93
+
94
+ return if undefined_columns.empty?
95
+
96
+ logger.warn("Clickhouse: Undefined columns for #{table_name}: #{undefined_columns}")
97
+ row.except!(*undefined_columns)
98
+ end
75
99
  end
76
100
  end
77
101
  end
@@ -45,16 +45,6 @@ describe Clickhouse::Table do
45
45
  end
46
46
  end
47
47
 
48
- context 'when row is an array' do
49
- # TODO: implement array logic
50
- let(:rows) { [['a'], ['2']] }
51
-
52
- it 'raises an error' do
53
- expect { CustomTable.insert_rows(rows) }
54
- .to raise_error(Clickhouse::Table::WrongTypeRowError)
55
- end
56
- end
57
-
58
48
  context 'when row has empty attributes' do
59
49
  let(:rows) { [{}] }
60
50
 
@@ -65,4 +55,25 @@ describe Clickhouse::Table do
65
55
  end
66
56
  end
67
57
  end
58
+
59
+ describe '#prepare_row' do
60
+ subject(:method) { CustomTable.prepare_row(row) }
61
+
62
+ context 'when row has wrong type' do
63
+ # TODO: implement array logic
64
+ let(:row) { ['a'] }
65
+
66
+ it 'raises an error' do
67
+ expect { method }.to raise_error(Clickhouse::Table::WrongTypeRowError)
68
+ end
69
+ end
70
+
71
+ context 'when row has extra fields' do
72
+ let(:row) { { 'field' => 'a', 'extra_field' => 'b' } }
73
+
74
+ it 'leaves only existing fields' do
75
+ is_expected.to eq('field' => 'a')
76
+ end
77
+ end
78
+ end
68
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clickhouse-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vsevolod Avramov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-12 00:00:00.000000000 Z
11
+ date: 2019-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clickhouse