clickhouse-rails 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +17 -2
- data/lib/clickhouse-rails.rb +1 -0
- data/lib/clickhouse/rails/migrations/base.rb +10 -8
- data/lib/clickhouse/rails/version.rb +1 -1
- data/lib/clickhouse/table.rb +66 -0
- data/spec/rails_helper.rb +3 -1
- data/spec/support/migration.rb +4 -0
- data/spec/table_spec.rb +57 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb4718899c50eee5d790b88331caa67e07cdc38eac66ea865aeee62401db37b0
|
4
|
+
data.tar.gz: 15b86279df212a677bccf8437d1e4e52715fb0e515b3b8fcaaf8a5cad3bcd9d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 887ed3d0538f74d8ea648445d04fcb1d941ee2009a8baf5ad05ff3c4783c9dd2f63b08d4f96b8a41578c8d0016e1799df2bfb5a3d7252ff1788fb0b86a5aebf1
|
7
|
+
data.tar.gz: 2d2dc371a8fa84e164505913a6151dd479d82918ec8b54f4b7b042cee8432e0993d3371109097b65008de0694bcce10a9d28099caf22eb0d531e0984d4823927
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -22,13 +22,15 @@ $ rails g clickhouse:install
|
|
22
22
|
|
23
23
|
4. Change clickhouse.yml at `config/clickhouse.yml` path
|
24
24
|
|
25
|
-
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
1. Create migrations
|
26
28
|
```bash
|
27
29
|
$ rails g clickhouse:migration add_tmp_table
|
28
30
|
create db/clickhouse/migrate/002_add_tmp_table.rb
|
29
31
|
```
|
30
32
|
|
31
|
-
|
33
|
+
2. Edit file like this:
|
32
34
|
```ruby
|
33
35
|
# db/clickhouse/migrate/002_add_tmp_table.rb
|
34
36
|
class AddTmpTable < Clickhouse::Rails::Migrations::Base
|
@@ -43,6 +45,19 @@ class AddTmpTable < Clickhouse::Rails::Migrations::Base
|
|
43
45
|
end
|
44
46
|
```
|
45
47
|
|
48
|
+
3. Run migrations
|
49
|
+
```bash
|
50
|
+
$ rake clickhouse:db:migrate
|
51
|
+
```
|
52
|
+
|
53
|
+
You can create class of clickhouse table:
|
54
|
+
```
|
55
|
+
# app/models/custom_table.rb
|
56
|
+
class CustomTable
|
57
|
+
include Clickhouse::Table
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
46
61
|
## TODO:
|
47
62
|
|
48
63
|
1. Rollback migrations
|
data/lib/clickhouse-rails.rb
CHANGED
@@ -68,30 +68,32 @@ module Clickhouse
|
|
68
68
|
def fetch_column(column, type)
|
69
69
|
return if @table_info.find { |c_info| c_info.first == column.to_s }
|
70
70
|
|
71
|
-
type = type.to_s
|
72
|
-
.gsub(/(^.|_\w)/) do
|
71
|
+
type = type.to_s.gsub(/(^.|_\w)/) do
|
73
72
|
Regexp.last_match(1).upcase
|
74
73
|
end
|
75
|
-
|
76
|
-
.delete('_')
|
74
|
+
type = type.gsub('Uint', 'UInt').delete('_')
|
77
75
|
|
78
76
|
connection.execute("ALTER TABLE #{@table_name} ADD COLUMN #{column} #{type}")
|
79
77
|
end
|
80
78
|
|
81
79
|
def run_migration(migration)
|
82
|
-
|
80
|
+
logger.info "# >========== #{migration.name} ==========="
|
83
81
|
migration.up
|
84
82
|
migration.add_version
|
85
83
|
rescue Clickhouse::QueryError => e
|
86
|
-
|
87
|
-
|
84
|
+
logger.info "# Error #{e.class}:"
|
85
|
+
logger.info "# #{e.message}"
|
88
86
|
ensure
|
89
|
-
|
87
|
+
logger.info "# <========== #{migration.name} ===========\n\n"
|
90
88
|
end
|
91
89
|
|
92
90
|
def connection
|
93
91
|
Clickhouse.connection
|
94
92
|
end
|
93
|
+
|
94
|
+
def logger
|
95
|
+
::Rails.logger
|
96
|
+
end
|
95
97
|
end
|
96
98
|
end
|
97
99
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Clickhouse
|
2
|
+
module Table
|
3
|
+
class WrongTypeRowError < StandardError; end
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def table_name
|
11
|
+
@table_name ||= to_s.tableize
|
12
|
+
end
|
13
|
+
|
14
|
+
def insert_rows(rows)
|
15
|
+
connection.insert_rows(table_name) do |table_rows|
|
16
|
+
rows.each do |row|
|
17
|
+
next if row.nil?
|
18
|
+
|
19
|
+
complete_row = prepare_row(block_given? ? yield(row) : row)
|
20
|
+
|
21
|
+
table_rows << complete_row
|
22
|
+
end
|
23
|
+
|
24
|
+
table_rows
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def table_columns
|
29
|
+
@table_columns ||=
|
30
|
+
connection.select_rows(
|
31
|
+
select: 'name, type',
|
32
|
+
from: 'system.columns',
|
33
|
+
where: "table = '#{table_name}'"
|
34
|
+
).to_h
|
35
|
+
end
|
36
|
+
|
37
|
+
def rows(attributes = {})
|
38
|
+
connection.select_rows(attributes.merge(from: table_name))
|
39
|
+
end
|
40
|
+
|
41
|
+
def empty_row
|
42
|
+
@empty_row ||= table_columns.map do |k, v|
|
43
|
+
value =
|
44
|
+
case v
|
45
|
+
when /UInt/ then 0
|
46
|
+
when /Float/ then 0.0
|
47
|
+
else
|
48
|
+
''
|
49
|
+
end
|
50
|
+
|
51
|
+
[k, value]
|
52
|
+
end.to_h
|
53
|
+
end
|
54
|
+
|
55
|
+
def prepare_row(row)
|
56
|
+
return empty_row.merge(row.stringify_keys) if row.is_a?(Hash)
|
57
|
+
|
58
|
+
raise WrongTypeRowError, "#{row.inspect} has wrong type"
|
59
|
+
end
|
60
|
+
|
61
|
+
def connection
|
62
|
+
::Clickhouse.connection
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/spec/rails_helper.rb
CHANGED
@@ -5,7 +5,6 @@ SimpleCov.start
|
|
5
5
|
|
6
6
|
require 'codecov'
|
7
7
|
SimpleCov.formatter = SimpleCov::Formatter::Codecov
|
8
|
-
|
9
8
|
require 'rails'
|
10
9
|
require 'action_view/railtie'
|
11
10
|
require 'action_controller/railtie'
|
@@ -16,3 +15,6 @@ require 'ammeter/init'
|
|
16
15
|
require_relative '../lib/clickhouse-rails'
|
17
16
|
require_relative 'support/generators'
|
18
17
|
require_relative 'support/init'
|
18
|
+
require_relative 'support/migration'
|
19
|
+
|
20
|
+
Rails.logger = Logger.new('/dev/null')
|
data/spec/table_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative 'rails_helper'
|
2
|
+
|
3
|
+
class CustomTable
|
4
|
+
include Clickhouse::Table
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Clickhouse::Table do
|
8
|
+
before do
|
9
|
+
with_table 'custom_tables' do |t|
|
10
|
+
t.string 'field'
|
11
|
+
|
12
|
+
t.engine 'File(TabSeparated)'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#table_columns' do
|
17
|
+
subject(:method) { CustomTable.table_columns }
|
18
|
+
|
19
|
+
it 'returns all info' do
|
20
|
+
is_expected.to eq('field' => 'String')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#insert_rows' do
|
25
|
+
let(:inserted_rows) { CustomTable.rows.to_a }
|
26
|
+
|
27
|
+
context 'when row is a hash' do
|
28
|
+
let(:rows) { [{ 'field' => 'a' }, { 'field' => 2 }] }
|
29
|
+
|
30
|
+
it 'adds two rows' do
|
31
|
+
CustomTable.insert_rows(rows)
|
32
|
+
|
33
|
+
expect(inserted_rows).to eq([['a'], ['2']])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when row is an array' do
|
38
|
+
# TODO: implement array logic
|
39
|
+
let(:rows) { [['a'], ['2']] }
|
40
|
+
|
41
|
+
it 'raises an error' do
|
42
|
+
expect { CustomTable.insert_rows(rows) }
|
43
|
+
.to raise_error(Clickhouse::Table::WrongTypeRowError)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when row has empty attributes' do
|
48
|
+
let(:rows) { [{}] }
|
49
|
+
|
50
|
+
it 'adds empty row' do
|
51
|
+
CustomTable.insert_rows(rows)
|
52
|
+
|
53
|
+
expect(inserted_rows).to eq([['']])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clickhouse-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vsevolod Avramov
|
@@ -103,6 +103,7 @@ files:
|
|
103
103
|
- lib/clickhouse/rails/config.rb
|
104
104
|
- lib/clickhouse/rails/migrations/base.rb
|
105
105
|
- lib/clickhouse/rails/version.rb
|
106
|
+
- lib/clickhouse/table.rb
|
106
107
|
- lib/generators/clickhouse/install/install_generator.rb
|
107
108
|
- lib/generators/clickhouse/install/templates/config/clickhouse.yml
|
108
109
|
- lib/generators/clickhouse/install/templates/db/clickhouse/migrate/001_init.rb
|
@@ -119,6 +120,8 @@ files:
|
|
119
120
|
- spec/spec_helper.rb
|
120
121
|
- spec/support/generators.rb
|
121
122
|
- spec/support/init.rb
|
123
|
+
- spec/support/migration.rb
|
124
|
+
- spec/table_spec.rb
|
122
125
|
homepage: https://github.com/vsevolod/clickhouse-rails
|
123
126
|
licenses:
|
124
127
|
- MIT
|