clickhouse-rails 0.1.0
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 +7 -0
- data/.gitignore +52 -0
- data/.rspec +1 -0
- data/.rubocop.yml +14 -0
- data/.travis.yml +12 -0
- data/Dockerfile.test +11 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +158 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/clickhouse-rails.gemspec +23 -0
- data/docker-compose.yml +17 -0
- data/lib/clickhouse/rails/config.rb +33 -0
- data/lib/clickhouse/rails/migrations/base.rb +89 -0
- data/lib/clickhouse/rails/version.rb +5 -0
- data/lib/clickhouse/rails.rb +37 -0
- data/lib/clickhouse-rails.rb +2 -0
- data/lib/generators/clickhouse/install/install_generator.rb +23 -0
- data/lib/generators/clickhouse/install/templates/config/clickhouse.yml +3 -0
- data/lib/generators/clickhouse/install/templates/db/clickhouse/migrate/001_init.rb +9 -0
- data/lib/generators/clickhouse/migration/migration_generator.rb +36 -0
- data/lib/generators/clickhouse/migration/templates/db/migrate/template.rb.tt +4 -0
- data/lib/tasks/clickhouse/db/migrate.rake +14 -0
- data/spec/config_spec.rb +22 -0
- data/spec/fixtures/files/001_init.rb +9 -0
- data/spec/fixtures/files/clickhouse.yml +3 -0
- data/spec/generators/install/install_generator_spec.rb +16 -0
- data/spec/generators/migration/migration_generator_spec.rb +11 -0
- data/spec/migrations/base_spec.rb +65 -0
- data/spec/rails_helper.rb +18 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/support/generators.rb +24 -0
- data/spec/support/init.rb +15 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 53465e63150b708d9a01b7508e09a91e6ed599e6a9436d56b65576499984d483
|
4
|
+
data.tar.gz: fb517f02559a4f1d8d7140a4e0842f8c634941dadc73bcb67dff9d8de773a5de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45503b480904734621b91fd5a3177f045998aedcd8712b3144351cfdad0f56c7dba68797c7cc2139760d8bd3ac0247d0e2337568fbc1a46ab2181ffd1fd5c9e7
|
7
|
+
data.tar.gz: ddfa5b82e2e061de0ace2786f140bf1c5d00637e10fff34e1230304ddba52a74fa1e2e8a0a39ca2ba84f62844b969d1c57c3adbc3b74c25010a2d188bd0f39b4
|
data/.gitignore
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/spec/tmp
|
10
|
+
/test/tmp/
|
11
|
+
/test/version_tmp/
|
12
|
+
/tmp/
|
13
|
+
/tmp/db/clickhouse
|
14
|
+
|
15
|
+
# Used by dotenv library to load environment variables.
|
16
|
+
# .env
|
17
|
+
|
18
|
+
## Specific to RubyMotion:
|
19
|
+
.dat*
|
20
|
+
.repl_history
|
21
|
+
build/
|
22
|
+
*.bridgesupport
|
23
|
+
build-iPhoneOS/
|
24
|
+
build-iPhoneSimulator/
|
25
|
+
|
26
|
+
## Specific to RubyMotion (use of CocoaPods):
|
27
|
+
#
|
28
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
29
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
30
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
31
|
+
#
|
32
|
+
# vendor/Pods/
|
33
|
+
|
34
|
+
## Documentation cache and generated files:
|
35
|
+
/.yardoc/
|
36
|
+
/_yardoc/
|
37
|
+
/doc/
|
38
|
+
/rdoc/
|
39
|
+
|
40
|
+
## Environment normalization:
|
41
|
+
/.bundle/
|
42
|
+
/vendor/bundle
|
43
|
+
/lib/bundler/man/
|
44
|
+
|
45
|
+
# for a library or gem, you might want to ignore these files since the code is
|
46
|
+
# intended to run in multiple environments; otherwise, check them in:
|
47
|
+
# Gemfile.lock
|
48
|
+
# .ruby-version
|
49
|
+
# .ruby-gemset
|
50
|
+
|
51
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
52
|
+
.rvmrc
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
rvm:
|
4
|
+
- 2.5
|
5
|
+
services:
|
6
|
+
- docker
|
7
|
+
before_install:
|
8
|
+
- bundle install
|
9
|
+
- docker run -e "TZ=Europe/Moscow" -d -p 127.0.0.1:8123:8123 --name test-clickhouse-server --ulimit nofile=262144:262144 yandex/clickhouse-server
|
10
|
+
script:
|
11
|
+
- rubocop
|
12
|
+
- rspec
|
data/Dockerfile.test
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
clickhouse-rails (0.1.0)
|
5
|
+
clickhouse
|
6
|
+
railties
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (5.2.3)
|
12
|
+
actionview (= 5.2.3)
|
13
|
+
activesupport (= 5.2.3)
|
14
|
+
rack (~> 2.0)
|
15
|
+
rack-test (>= 0.6.3)
|
16
|
+
rails-dom-testing (~> 2.0)
|
17
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
18
|
+
actionview (5.2.3)
|
19
|
+
activesupport (= 5.2.3)
|
20
|
+
builder (~> 3.1)
|
21
|
+
erubi (~> 1.4)
|
22
|
+
rails-dom-testing (~> 2.0)
|
23
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
24
|
+
activesupport (5.2.3)
|
25
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
26
|
+
i18n (>= 0.7, < 2)
|
27
|
+
minitest (~> 5.1)
|
28
|
+
tzinfo (~> 1.1)
|
29
|
+
addressable (2.6.0)
|
30
|
+
public_suffix (>= 2.0.2, < 4.0)
|
31
|
+
ammeter (1.1.4)
|
32
|
+
activesupport (>= 3.0)
|
33
|
+
railties (>= 3.0)
|
34
|
+
rspec-rails (>= 2.2)
|
35
|
+
ast (2.4.0)
|
36
|
+
builder (3.2.3)
|
37
|
+
clickhouse (0.1.10)
|
38
|
+
activesupport (>= 4.1.8)
|
39
|
+
bundler (>= 1.13.4)
|
40
|
+
erubis
|
41
|
+
faraday
|
42
|
+
launchy
|
43
|
+
pond
|
44
|
+
sinatra
|
45
|
+
thor
|
46
|
+
codecov (0.1.14)
|
47
|
+
json
|
48
|
+
simplecov
|
49
|
+
url
|
50
|
+
coderay (1.1.2)
|
51
|
+
concurrent-ruby (1.1.5)
|
52
|
+
crass (1.0.4)
|
53
|
+
diff-lcs (1.3)
|
54
|
+
docile (1.3.1)
|
55
|
+
erubi (1.8.0)
|
56
|
+
erubis (2.7.0)
|
57
|
+
faraday (0.15.4)
|
58
|
+
multipart-post (>= 1.2, < 3)
|
59
|
+
i18n (1.6.0)
|
60
|
+
concurrent-ruby (~> 1.0)
|
61
|
+
jaro_winkler (1.5.2)
|
62
|
+
json (2.2.0)
|
63
|
+
launchy (2.4.3)
|
64
|
+
addressable (~> 2.3)
|
65
|
+
loofah (2.2.3)
|
66
|
+
crass (~> 1.0.2)
|
67
|
+
nokogiri (>= 1.5.9)
|
68
|
+
method_source (0.9.2)
|
69
|
+
mini_portile2 (2.4.0)
|
70
|
+
minitest (5.11.3)
|
71
|
+
multipart-post (2.0.0)
|
72
|
+
mustermann (1.0.3)
|
73
|
+
nokogiri (1.10.2)
|
74
|
+
mini_portile2 (~> 2.4.0)
|
75
|
+
parallel (1.17.0)
|
76
|
+
parser (2.6.2.1)
|
77
|
+
ast (~> 2.4.0)
|
78
|
+
pond (0.3.0)
|
79
|
+
pry (0.12.2)
|
80
|
+
coderay (~> 1.1.0)
|
81
|
+
method_source (~> 0.9.0)
|
82
|
+
psych (3.1.0)
|
83
|
+
public_suffix (3.0.3)
|
84
|
+
rack (2.0.7)
|
85
|
+
rack-protection (2.0.5)
|
86
|
+
rack
|
87
|
+
rack-test (1.1.0)
|
88
|
+
rack (>= 1.0, < 3)
|
89
|
+
rails-dom-testing (2.0.3)
|
90
|
+
activesupport (>= 4.2.0)
|
91
|
+
nokogiri (>= 1.6)
|
92
|
+
rails-html-sanitizer (1.0.4)
|
93
|
+
loofah (~> 2.2, >= 2.2.2)
|
94
|
+
railties (5.2.3)
|
95
|
+
actionpack (= 5.2.3)
|
96
|
+
activesupport (= 5.2.3)
|
97
|
+
method_source
|
98
|
+
rake (>= 0.8.7)
|
99
|
+
thor (>= 0.19.0, < 2.0)
|
100
|
+
rainbow (3.0.0)
|
101
|
+
rake (12.3.0)
|
102
|
+
rspec-core (3.8.0)
|
103
|
+
rspec-support (~> 3.8.0)
|
104
|
+
rspec-expectations (3.8.2)
|
105
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
106
|
+
rspec-support (~> 3.8.0)
|
107
|
+
rspec-mocks (3.8.0)
|
108
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
109
|
+
rspec-support (~> 3.8.0)
|
110
|
+
rspec-rails (3.8.2)
|
111
|
+
actionpack (>= 3.0)
|
112
|
+
activesupport (>= 3.0)
|
113
|
+
railties (>= 3.0)
|
114
|
+
rspec-core (~> 3.8.0)
|
115
|
+
rspec-expectations (~> 3.8.0)
|
116
|
+
rspec-mocks (~> 3.8.0)
|
117
|
+
rspec-support (~> 3.8.0)
|
118
|
+
rspec-support (3.8.0)
|
119
|
+
rubocop (0.67.2)
|
120
|
+
jaro_winkler (~> 1.5.1)
|
121
|
+
parallel (~> 1.10)
|
122
|
+
parser (>= 2.5, != 2.5.1.1)
|
123
|
+
psych (>= 3.1.0)
|
124
|
+
rainbow (>= 2.2.2, < 4.0)
|
125
|
+
ruby-progressbar (~> 1.7)
|
126
|
+
unicode-display_width (>= 1.4.0, < 1.6)
|
127
|
+
ruby-progressbar (1.10.0)
|
128
|
+
simplecov (0.16.1)
|
129
|
+
docile (~> 1.1)
|
130
|
+
json (>= 1.8, < 3)
|
131
|
+
simplecov-html (~> 0.10.0)
|
132
|
+
simplecov-html (0.10.2)
|
133
|
+
sinatra (2.0.5)
|
134
|
+
mustermann (~> 1.0)
|
135
|
+
rack (~> 2.0)
|
136
|
+
rack-protection (= 2.0.5)
|
137
|
+
tilt (~> 2.0)
|
138
|
+
thor (0.20.3)
|
139
|
+
thread_safe (0.3.6)
|
140
|
+
tilt (2.0.9)
|
141
|
+
tzinfo (1.2.5)
|
142
|
+
thread_safe (~> 0.1)
|
143
|
+
unicode-display_width (1.5.0)
|
144
|
+
url (0.3.2)
|
145
|
+
|
146
|
+
PLATFORMS
|
147
|
+
ruby
|
148
|
+
|
149
|
+
DEPENDENCIES
|
150
|
+
ammeter (~> 1.1.2)
|
151
|
+
clickhouse-rails!
|
152
|
+
codecov
|
153
|
+
pry
|
154
|
+
rspec-rails
|
155
|
+
rubocop
|
156
|
+
|
157
|
+
BUNDLED WITH
|
158
|
+
2.0.1
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Avramov Vsevolod
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Clickhouse rails
|
2
|
+
|
3
|
+
[](https://travis-ci.com/vsevolod/clickhouse-rails)
|
4
|
+
[](https://codecov.io/gh/vsevolod/clickhouse-rails)
|
5
|
+
|
6
|
+
## Install
|
7
|
+
|
8
|
+
1. Add to Gemfile
|
9
|
+
```ruby
|
10
|
+
gem 'clickhouse-rails'
|
11
|
+
```
|
12
|
+
|
13
|
+
2. Run bundle
|
14
|
+
```bash
|
15
|
+
$ bundle install
|
16
|
+
```
|
17
|
+
|
18
|
+
3. Init config files and folders
|
19
|
+
```bash
|
20
|
+
$ rails g clickhouse:install
|
21
|
+
```
|
22
|
+
|
23
|
+
4. Change clickhouse.yml at `config/clickhouse.yml` path
|
24
|
+
|
25
|
+
5. Create migrations
|
26
|
+
```bash
|
27
|
+
$ rails g clickhouse:migration add_tmp_table
|
28
|
+
create db/clickhouse/migrate/002_add_tmp_table.rb
|
29
|
+
```
|
30
|
+
|
31
|
+
6. Edit file like this:
|
32
|
+
```ruby
|
33
|
+
# db/clickhouse/migrate/002_add_tmp_table.rb
|
34
|
+
class AddTmpTable < Clickhouse::Rails::Migrations::Base
|
35
|
+
def self.up
|
36
|
+
create_table :tmp do |t|
|
37
|
+
t.date :date
|
38
|
+
t.uint16 :id
|
39
|
+
|
40
|
+
t.engine "MergeTree(date, (date), 8192)"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
## TODO:
|
47
|
+
|
48
|
+
1. Rollback migrations
|
49
|
+
2. Alter table
|
50
|
+
|
51
|
+
## Additional clickhouse links
|
52
|
+
|
53
|
+
- [Base gem](https://github.com/archan937/clickhouse)
|
54
|
+
- [List of data types](https://clickhouse.yandex/docs/en/data_types/)
|
55
|
+
- [Table engines](https://clickhouse.yandex/docs/en/operations/table_engines/)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('lib/clickhouse/rails/version', __dir__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ['Vsevolod Avramov']
|
5
|
+
gem.email = ['gsevka@gmail.com']
|
6
|
+
gem.summary = 'A Rails database driver for ClickHouse'
|
7
|
+
gem.description = 'A Rails database driver for ClickHouse'
|
8
|
+
gem.homepage = 'https://github.com/vsevolod/clickhouse-rails'
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split("\n")
|
11
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split('\n')
|
12
|
+
gem.name = 'clickhouse-rails'
|
13
|
+
gem.require_paths = ['lib']
|
14
|
+
gem.version = Clickhouse::Rails::VERSION
|
15
|
+
gem.licenses = ['MIT']
|
16
|
+
|
17
|
+
gem.add_dependency 'clickhouse'
|
18
|
+
gem.add_dependency 'railties'
|
19
|
+
|
20
|
+
gem.add_development_dependency 'ammeter', '~> 1.1.2'
|
21
|
+
gem.add_development_dependency 'pry'
|
22
|
+
gem.add_development_dependency 'rspec-rails'
|
23
|
+
end
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
version: '3'
|
2
|
+
services:
|
3
|
+
ruby:
|
4
|
+
build:
|
5
|
+
dockerfile: ./Dockerfile.test
|
6
|
+
context: .
|
7
|
+
environment:
|
8
|
+
- "CLICKHOUSE_HOSTS=clickhouse:8123"
|
9
|
+
- "CLICKHOUSE_USER=default"
|
10
|
+
- "CLICKHOUSE_ENV=test"
|
11
|
+
command: bash -c "bundle install && rspec"
|
12
|
+
volumes:
|
13
|
+
- ./:/app
|
14
|
+
links:
|
15
|
+
- "clickhouse:clickhouse"
|
16
|
+
clickhouse:
|
17
|
+
image: yandex/clickhouse-server
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Clickhouse
|
4
|
+
module Rails
|
5
|
+
module Config
|
6
|
+
class ConfigurationNotFound < NameError; end
|
7
|
+
|
8
|
+
CLICKHOUSE_ROOT = File.expand_path(::Rails.root.present? ? ::Rails.root : '.')
|
9
|
+
DEFAULT_CONFIG_PATH = File.join(CLICKHOUSE_ROOT, 'config', 'clickhouse.yml')
|
10
|
+
|
11
|
+
def self.init(config_path = nil)
|
12
|
+
config_path ||= DEFAULT_CONFIG_PATH
|
13
|
+
exists = config_path && File.exist?(config_path)
|
14
|
+
unless exists
|
15
|
+
raise ConfigurationNotFound, "could not find the \"#{config_path}\" configuration file"
|
16
|
+
end
|
17
|
+
|
18
|
+
content = File.read(config_path)
|
19
|
+
data = defined?(ERB) ? ERB.new(content).result : content
|
20
|
+
source = YAML.safe_load(data)[defined?(::Rails) ? ::Rails.env : ENV['CLICKHOUSE_ENV']]
|
21
|
+
config_mapper(source)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.config_mapper(source)
|
25
|
+
{
|
26
|
+
urls: source['hosts'].split(','),
|
27
|
+
username: source['username'],
|
28
|
+
password: source['password']
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Clickhouse
|
2
|
+
module Rails
|
3
|
+
module Migrations
|
4
|
+
class Base
|
5
|
+
MIGRATION_TABLE = 'schema_migrations'.freeze
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :migrations_list
|
9
|
+
|
10
|
+
def inherited(child)
|
11
|
+
@migrations_list ||= []
|
12
|
+
@migrations_list.push(child)
|
13
|
+
end
|
14
|
+
|
15
|
+
def run_up
|
16
|
+
return unless @migrations_list
|
17
|
+
|
18
|
+
@migrations_list.each do |migration|
|
19
|
+
next if migration.passed?
|
20
|
+
|
21
|
+
migration.up
|
22
|
+
migration.add_version
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def passed?
|
27
|
+
return false unless table_exists?(MIGRATION_TABLE)
|
28
|
+
|
29
|
+
@rows ||= connection.select_row(select: 'version', from: MIGRATION_TABLE)
|
30
|
+
@rows.include?(name)
|
31
|
+
rescue Clickhouse::QueryError
|
32
|
+
false
|
33
|
+
end
|
34
|
+
|
35
|
+
def up; end
|
36
|
+
|
37
|
+
def add_version
|
38
|
+
connection.insert_rows(MIGRATION_TABLE) do |row|
|
39
|
+
row << { version: name }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def table_exists?(table_name)
|
44
|
+
connection.execute("EXISTS TABLE #{table_name}").strip == '1'
|
45
|
+
end
|
46
|
+
|
47
|
+
def soft_create_table(table_name, &block)
|
48
|
+
return if table_exists?(table_name)
|
49
|
+
|
50
|
+
create_table(table_name, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
def soft_drop_table(table_name)
|
54
|
+
return unless table_exists?(table_name)
|
55
|
+
|
56
|
+
drop_table(table_name)
|
57
|
+
end
|
58
|
+
|
59
|
+
delegate :create_table, to: :connection
|
60
|
+
delegate :drop_table, to: :connection
|
61
|
+
|
62
|
+
def alter_table(table_name)
|
63
|
+
@table_info = connection.describe_table(table_name)
|
64
|
+
@table_name = table_name
|
65
|
+
|
66
|
+
yield(table_name)
|
67
|
+
end
|
68
|
+
|
69
|
+
def fetch_column(column, type)
|
70
|
+
return if @table_info.find { |c_info| c_info.first == column.to_s }
|
71
|
+
|
72
|
+
type = type.to_s
|
73
|
+
.gsub(/(^.|_\w)/) do
|
74
|
+
Regexp.last_match(1).upcase
|
75
|
+
end
|
76
|
+
.gsub('Uint', 'UInt')
|
77
|
+
.delete('_')
|
78
|
+
|
79
|
+
connection.execute("ALTER TABLE #{@table_name} ADD COLUMN #{column} #{type}")
|
80
|
+
end
|
81
|
+
|
82
|
+
def connection
|
83
|
+
Clickhouse.connection
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'clickhouse/rails/config'
|
3
|
+
|
4
|
+
module Clickhouse
|
5
|
+
module Rails
|
6
|
+
def self.init!
|
7
|
+
Clickhouse.establish_connection(config)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.config(config_path = nil)
|
11
|
+
@config ||= Clickhouse::Rails::Config.init(config_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Railtie < ::Rails::Railtie
|
15
|
+
generators =
|
16
|
+
if config.respond_to?(:app_generators)
|
17
|
+
config.app_generators
|
18
|
+
else
|
19
|
+
config.generators
|
20
|
+
end
|
21
|
+
generators.integration_tool :clickhouse
|
22
|
+
generators.test_framework :clickhouse
|
23
|
+
|
24
|
+
generators do
|
25
|
+
require 'generators/clickhouse/install/install_generator'
|
26
|
+
end
|
27
|
+
|
28
|
+
rake_tasks do
|
29
|
+
load 'tasks/clickhouse/db/migrate.rake'
|
30
|
+
end
|
31
|
+
|
32
|
+
config.to_prepare do
|
33
|
+
Clickhouse::Rails.init!
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Clickhouse
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
desc <<-DESC
|
5
|
+
Description:
|
6
|
+
Copy clickhouse file to your application
|
7
|
+
DESC
|
8
|
+
|
9
|
+
def self.source_root
|
10
|
+
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
11
|
+
end
|
12
|
+
|
13
|
+
def copy_install_files
|
14
|
+
copy_file 'config/clickhouse.yml'
|
15
|
+
directory db_migrate_path
|
16
|
+
end
|
17
|
+
|
18
|
+
def db_migrate_path
|
19
|
+
'db/clickhouse/migrate'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Clickhouse
|
2
|
+
module Generators
|
3
|
+
class MigrationGenerator < ::Rails::Generators::NamedBase
|
4
|
+
include ::Rails::Generators::Migration
|
5
|
+
|
6
|
+
desc <<-DESC
|
7
|
+
Description:
|
8
|
+
Add migration for clickhouse database
|
9
|
+
DESC
|
10
|
+
|
11
|
+
def self.source_root
|
12
|
+
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.next_migration_number(dirname)
|
16
|
+
number = current_migration_number(dirname) + 1
|
17
|
+
format('%03d', number)
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_migration_file
|
21
|
+
set_local_assign!
|
22
|
+
migration_template @migration_template, File.join(db_migrate_path, "#{file_name}.rb")
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def set_local_assign!
|
28
|
+
@migration_template = 'db/migrate/template.rb'
|
29
|
+
end
|
30
|
+
|
31
|
+
def db_migrate_path
|
32
|
+
'db/clickhouse/migrate'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require_relative '../../../clickhouse/rails'
|
2
|
+
require_relative '../../../clickhouse/rails/migrations/base'
|
3
|
+
|
4
|
+
namespace :clickhouse do
|
5
|
+
namespace :db do
|
6
|
+
desc 'Migrate clickhouse migrations'
|
7
|
+
task :migrate do
|
8
|
+
path_to_migrations = './db/clickhouse/migrate/*.rb'
|
9
|
+
Dir[path_to_migrations].sort.each { |file| require file }
|
10
|
+
|
11
|
+
Clickhouse::Rails::Migrations::Base.run_up
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative 'rails_helper'
|
2
|
+
|
3
|
+
describe Clickhouse::Rails::Config do
|
4
|
+
describe '.init' do
|
5
|
+
subject(:init) { described_class.init }
|
6
|
+
|
7
|
+
context 'when config file does not exists' do
|
8
|
+
let(:error_class) { Clickhouse::Rails::Config::ConfigurationNotFound }
|
9
|
+
it { expect { init }.to raise_error(error_class) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when config file exists' do
|
13
|
+
let(:path) { File.dirname(__FILE__) + '/fixtures/files/clickhouse.yml' }
|
14
|
+
|
15
|
+
before do
|
16
|
+
stub_const('Clickhouse::Rails::Config::DEFAULT_CONFIG_PATH', path)
|
17
|
+
end
|
18
|
+
|
19
|
+
it { expect { init }.not_to raise_error }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative '../../rails_helper'
|
2
|
+
require_relative '../../../lib/generators/clickhouse/install/install_generator'
|
3
|
+
|
4
|
+
RSpec.describe Clickhouse::Generators::InstallGenerator, type: :generator do
|
5
|
+
setup_default_destination
|
6
|
+
|
7
|
+
it 'generates config/clickhouse.yml' do
|
8
|
+
run_generator
|
9
|
+
expect(file('config/clickhouse.yml')).to exist
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'generates db/clickhouse/migrate/001_init.rb' do
|
13
|
+
run_generator
|
14
|
+
expect(file('db/clickhouse/migrate/001_init.rb')).to exist
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require_relative '../../rails_helper'
|
2
|
+
require_relative '../../../lib/generators/clickhouse/migration/migration_generator'
|
3
|
+
|
4
|
+
RSpec.describe Clickhouse::Generators::MigrationGenerator, type: :generator do
|
5
|
+
setup_default_destination
|
6
|
+
|
7
|
+
it 'generates db/clickhouse/migrate/temp.rb' do
|
8
|
+
run_generator(['temp'])
|
9
|
+
expect(file('db/clickhouse/migrate/001_temp.rb')).to exist
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require_relative '../rails_helper'
|
2
|
+
require_relative '../../lib/clickhouse/rails/migrations/base'
|
3
|
+
|
4
|
+
RSpec.describe Clickhouse::Rails::Migrations::Base do
|
5
|
+
describe '.run_up' do
|
6
|
+
subject(:run_up) { described_class.run_up }
|
7
|
+
|
8
|
+
include_context 'with init migration' do
|
9
|
+
it 'executes up and add version' do
|
10
|
+
expect(Init).to receive(:up)
|
11
|
+
expect(Init).to receive(:add_version)
|
12
|
+
|
13
|
+
subject
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when two Init in @migrations_list' do
|
17
|
+
before do
|
18
|
+
described_class.migrations_list << Init
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'skips the second time' do
|
22
|
+
expect(Init).to receive(:up).once
|
23
|
+
|
24
|
+
subject
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '.create_table' do
|
31
|
+
subject(:create_table) { described_class.create_table(table_name, &block) }
|
32
|
+
|
33
|
+
let(:config_file) { file_fixture('clickhouse.yml') }
|
34
|
+
let(:table_name) { 'example' }
|
35
|
+
let(:block) do
|
36
|
+
lambda do |t|
|
37
|
+
t.string 'field'
|
38
|
+
|
39
|
+
t.engine 'File(TabSeparated)'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
before do
|
44
|
+
Clickhouse::Rails.config(config_file.realpath.to_s)
|
45
|
+
Clickhouse::Rails.init!
|
46
|
+
described_class.soft_drop_table(table_name)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'adds table to clickhouse' do
|
50
|
+
create_table
|
51
|
+
|
52
|
+
expect(described_class).to be_table_exists(table_name)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '.add_version' do
|
57
|
+
include_context 'with init migration'
|
58
|
+
|
59
|
+
it 'executes up and add version' do
|
60
|
+
described_class.add_version
|
61
|
+
|
62
|
+
expect(Clickhouse.connection.count(from: migration_table)).to eq(1)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'test'
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start
|
5
|
+
|
6
|
+
require 'codecov'
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::Codecov
|
8
|
+
|
9
|
+
require 'rails'
|
10
|
+
require 'action_view/railtie'
|
11
|
+
require 'action_controller/railtie'
|
12
|
+
require 'rails/generators'
|
13
|
+
|
14
|
+
require 'ammeter/init'
|
15
|
+
|
16
|
+
require_relative '../lib/clickhouse-rails'
|
17
|
+
require_relative 'support/generators'
|
18
|
+
require_relative 'support/init'
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
config.expect_with :rspec do |expectations|
|
3
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
4
|
+
end
|
5
|
+
|
6
|
+
config.mock_with :rspec do |mocks|
|
7
|
+
mocks.verify_partial_doubles = true
|
8
|
+
end
|
9
|
+
|
10
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
11
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Clickhouse
|
2
|
+
module Rails
|
3
|
+
module Generators
|
4
|
+
module Macros
|
5
|
+
def set_default_destination
|
6
|
+
destination File.expand_path('../../tmp', __dir__)
|
7
|
+
end
|
8
|
+
|
9
|
+
def setup_default_destination
|
10
|
+
set_default_destination
|
11
|
+
before { prepare_destination }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.included(klass)
|
16
|
+
klass.extend(Macros)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
RSpec.configure do |config|
|
23
|
+
config.include Clickhouse::Rails::Generators, type: :generator
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
RSpec.shared_context 'with init migration' do
|
2
|
+
let(:config_file) { file_fixture('clickhouse.yml') }
|
3
|
+
let(:file) { file_fixture('001_init.rb') }
|
4
|
+
let(:migration_table) { described_class::MIGRATION_TABLE }
|
5
|
+
|
6
|
+
before do
|
7
|
+
require file.realpath
|
8
|
+
|
9
|
+
Clickhouse::Rails.config(config_file.realpath.to_s)
|
10
|
+
Clickhouse::Rails.init!
|
11
|
+
|
12
|
+
described_class.soft_drop_table(migration_table)
|
13
|
+
Init.up
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clickhouse-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vsevolod Avramov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: clickhouse
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: railties
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: ammeter
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.1.2
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.1.2
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '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'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A Rails database driver for ClickHouse
|
84
|
+
email:
|
85
|
+
- gsevka@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Dockerfile.test
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE
|
98
|
+
- README.md
|
99
|
+
- clickhouse-rails.gemspec
|
100
|
+
- docker-compose.yml
|
101
|
+
- lib/clickhouse-rails.rb
|
102
|
+
- lib/clickhouse/rails.rb
|
103
|
+
- lib/clickhouse/rails/config.rb
|
104
|
+
- lib/clickhouse/rails/migrations/base.rb
|
105
|
+
- lib/clickhouse/rails/version.rb
|
106
|
+
- lib/generators/clickhouse/install/install_generator.rb
|
107
|
+
- lib/generators/clickhouse/install/templates/config/clickhouse.yml
|
108
|
+
- lib/generators/clickhouse/install/templates/db/clickhouse/migrate/001_init.rb
|
109
|
+
- lib/generators/clickhouse/migration/migration_generator.rb
|
110
|
+
- lib/generators/clickhouse/migration/templates/db/migrate/template.rb.tt
|
111
|
+
- lib/tasks/clickhouse/db/migrate.rake
|
112
|
+
- spec/config_spec.rb
|
113
|
+
- spec/fixtures/files/001_init.rb
|
114
|
+
- spec/fixtures/files/clickhouse.yml
|
115
|
+
- spec/generators/install/install_generator_spec.rb
|
116
|
+
- spec/generators/migration/migration_generator_spec.rb
|
117
|
+
- spec/migrations/base_spec.rb
|
118
|
+
- spec/rails_helper.rb
|
119
|
+
- spec/spec_helper.rb
|
120
|
+
- spec/support/generators.rb
|
121
|
+
- spec/support/init.rb
|
122
|
+
homepage: https://github.com/vsevolod/clickhouse-rails
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.7.6
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: A Rails database driver for ClickHouse
|
146
|
+
test_files: []
|