clickhouse-activerecord 0.1.9 → 0.2.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
  SHA1:
3
- metadata.gz: 95e15e250fc477a64d9a5f99faae7a831643c599
4
- data.tar.gz: aea2dd5a8c32635882c94ce49ce2a37b19126835
3
+ metadata.gz: 9a454d1e7bc652ae74ce866ebceab0a6df2d1a0f
4
+ data.tar.gz: 70ccff7d8651cf35ff7dbd274773b42e9a5054a7
5
5
  SHA512:
6
- metadata.gz: c4487476126af3c3025075bd3e8727f7232e163f3bef0ee2fdebf23bed7ca03f40d89601463be151f79caabcfa08540fbe0469f0c43fc94b4a882035642415fb
7
- data.tar.gz: 939d15b666d661e1d395b1c6cba3234503764ec3591ecd897ec304361f328aaddb1adb17cc866a14300efa0871c5a72ef70a85a4d7fac3c6ee851de138005e61
6
+ metadata.gz: c0bac0b7265c1cc259163d423564e151b87eaf37f8d5aee1b1241d4319eb722e8374fa9888285d1db2cee35eae4d4cc85883f0a795ea9d2a3dcf3cd0b614d3db
7
+ data.tar.gz: 922a4a22e3bd3f3bd22e5f68ffbc0151becf125ca6e15b515e8eda2fe3bdf4c62bd6b3fb5b9800529789e1947e0c4a7a32de70fd0f73ac2f99c86350a11f0566
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Clickhouse::Activerecord
2
2
 
3
- A Ruby database ActiveRecord driver for ClickHouse.
3
+ A Ruby database ActiveRecord driver for ClickHouse. Support Rails >= 5.0.
4
4
 
5
5
  ## Installation
6
6
 
@@ -39,14 +39,6 @@ class Action < ActiveRecord::Base
39
39
  end
40
40
  ```
41
41
 
42
- For materialized view model add:
43
- ```ruby
44
- class ActionView < ActiveRecord::Base
45
- establish_connection "#{Rails.env}_clickhouse".to_sym
46
- self.is_view = true
47
- end
48
- ```
49
-
50
42
  Or global connection, but schema dump don't works:
51
43
 
52
44
  ```yml
@@ -62,31 +54,23 @@ Schema dump:
62
54
 
63
55
  $ rake clickhouse:schema:dump
64
56
 
65
- We use schema for emulate development or tests environment on PostgreSQL adapter.
66
-
67
57
  ### Insert and select data
68
58
 
69
59
  ```ruby
70
60
  Action.where(url: 'http://example.com', date: Date.current).where.not(name: nil).order(created_at: :desc).limit(10)
71
- # Clickhouse Action Load (10.3ms) SELECT actions.* FROM actions WHERE actions.date = '2017-11-29' AND actions.url = 'http://example.com' AND (actions.name IS NOT NULL) ORDER BY actions.created_at DESC LIMIT 10
72
- #=> #<ActiveRecord::Relation [#<Action *** >]>
61
+ => #<ActiveRecord::Relation [#<Action *** >]>
73
62
 
74
63
  Action.create(url: 'http://example.com', date: Date.yesterday)
75
- # Clickhouse Action Load (10.8ms) INSERT INTO actions (url, date) VALUES ('http://example.com', '2017-11-28')
76
- #=> true
77
-
78
- ActionView.maximum(:date)
79
- # Clickhouse (10.3ms) SELECT maxMerge(actions.date) FROM actions
80
- #=> 'Wed, 29 Nov 2017'
64
+ => true
81
65
  ```
82
66
 
83
- ## Donations
67
+ NOTE: Creating tables in developing.
68
+
69
+ ## Development
84
70
 
85
- Donations to this project are going directly to [PNixx](https://github.com/PNixx), the original author of this project:
71
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
72
 
87
- * BTC address: `3Dny9T9PTVYoQhnJxgZorRRNycYYiPrYMA`
88
- * ETH address: `0x7eec67c120828334d0e802ce46fc4895f2b386c4`
89
- * XMR address: `42gP71qLB5M43RuDnrQ3vSJFFxis9Kw9VMURhpx9NLQRRwNvaZRjm2TFojAMC8Fk1BQhZNKyWhoyJSn5Ak9kppgZPjE17Zh`
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
90
74
 
91
75
  ## Contributing
92
76
 
@@ -2,11 +2,10 @@
2
2
 
3
3
  lib = File.expand_path('../lib', __FILE__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require File.expand_path('../lib/clickhouse-activerecord/version', __FILE__)
6
5
 
7
6
  Gem::Specification.new do |spec|
8
7
  spec.name = 'clickhouse-activerecord'
9
- spec.version = ClickhouseActiverecord::VERSION
8
+ spec.version = '0.2.0'
10
9
  spec.authors = ['Sergey Odintsov']
11
10
  spec.email = ['nixx.dj@gmail.com']
12
11
 
@@ -22,8 +21,8 @@ Gem::Specification.new do |spec|
22
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
22
  spec.require_paths = ['lib']
24
23
 
25
- spec.add_runtime_dependency 'bundler', '~> 1.13', '>= 1.13.4'
26
- spec.add_runtime_dependency 'rails', '>= 4.1.8', '< 5'
24
+ spec.add_dependency "bundler", ">= 1.13.4"
25
+ spec.add_dependency 'rails', '>= 5.0'
27
26
 
28
27
  spec.add_development_dependency 'bundler', '~> 1.15'
29
28
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -1,4 +1,3 @@
1
- require 'clickhouse-activerecord/arel/visitors/to_sql'
2
1
  require 'active_record/connection_adapters/abstract_adapter'
3
2
  require 'active_record/connection_adapters/clickhouse/oid/date'
4
3
  require 'active_record/connection_adapters/clickhouse/oid/date_time'
@@ -24,28 +23,9 @@ module ActiveRecord
24
23
  end
25
24
  end
26
25
 
27
- module ModelSchema
28
-
29
- module ClassMethods
30
- def is_view
31
- @is_view || false
32
- end
33
-
34
- # @param [Boolean] value
35
- def is_view=(value)
36
- @is_view = value
37
- end
38
- end
39
-
40
- end
41
-
42
26
  module ConnectionAdapters
43
27
 
44
28
  class ClickhouseColumn < Column
45
- def initialize(name, default, cast_type, sql_type = nil, null = true)
46
- super(name, default, cast_type, sql_type, null)
47
- @default_function = 'now'
48
- end
49
29
 
50
30
  private
51
31
 
@@ -105,7 +85,7 @@ module ActiveRecord
105
85
  @connection_parameters = connection_parameters
106
86
  @config = config
107
87
 
108
- @visitor = ClickhouseActiverecord::Arel::Visitors::ToSql.new self
88
+ @prepared_statements = false
109
89
 
110
90
  connect
111
91
  end
@@ -122,10 +102,6 @@ module ActiveRecord
122
102
  case sql_type
123
103
  when 'Nullable(String)'
124
104
  255
125
- when /Nullable\(U?Int(8|16)\)/
126
- 4
127
- when /Nullable\(U?Int(32|64)\)/
128
- 8
129
105
  else
130
106
  super
131
107
  end
@@ -138,18 +114,18 @@ module ActiveRecord
138
114
  register_class_with_limit m, 'Uint8', Type::UnsignedInteger
139
115
  register_class_with_limit m, 'Date', Clickhouse::OID::Date
140
116
  register_class_with_limit m, 'DateTime', Clickhouse::OID::DateTime
141
- m.alias_type 'UInt16', 'uint4'
117
+ m.alias_type 'UInt16', 'uint8'
142
118
  m.alias_type 'UInt32', 'uint8'
143
119
  m.register_type 'UInt64', Clickhouse::OID::BigInteger.new
144
- m.alias_type 'Int8', 'int4'
145
- m.alias_type 'Int16', 'int4'
120
+ m.alias_type 'Int8', 'int8'
121
+ m.alias_type 'Int16', 'int8'
146
122
  m.alias_type 'Int32', 'int8'
147
123
  m.alias_type 'Int64', 'UInt64'
148
124
  end
149
125
 
150
126
  # Queries the database and returns the results in an Array-like object
151
127
  def query(sql, _name = nil)
152
- res = @connection.post("/?#{@config.to_param}", "#{sql} FORMAT JSONCompact")
128
+ res = @connection.get("/?#{@config.merge(query: "#{sql} FORMAT JSONCompact").to_param}")
153
129
  raise ActiveRecord::ActiveRecordError, "Response code: #{res.code}:\n#{res.body}" unless res.code.to_i == 200
154
130
  JSON.parse res.body
155
131
  end
@@ -160,7 +136,7 @@ module ActiveRecord
160
136
  end
161
137
  end
162
138
 
163
- def exec_query(sql, name = nil, _binds = [])
139
+ def exec_query(sql, name = nil, binds = [], prepare: false)
164
140
  result = execute(sql, name)
165
141
  ActiveRecord::Result.new(result['meta'].map { |m| m['name'] }, result['data'])
166
142
  end
@@ -190,10 +166,10 @@ module ActiveRecord
190
166
  query('SHOW TABLES', name)['data'].flatten
191
167
  end
192
168
 
193
- def columns(table_name, _name = nil) #:nodoc:
194
- table_structure(table_name).map do |field|
195
- ClickhouseColumn.new(field[0], field[3].present? ? field[3] : nil, lookup_cast_type(field[1]), field[1], field[1].include?('Nullable'))
196
- end
169
+ def new_column_from_field(table_name, field) # :nondoc:
170
+ sql_type = field[1]
171
+ type_metadata = fetch_type_metadata(sql_type)
172
+ ClickhouseColumn.new(field[0], field[3].present? ? field[3] : nil, type_metadata, field[1].include?('Nullable'), table_name, nil)
197
173
  end
198
174
 
199
175
  def indexes(_table_name, _name = nil) #:nodoc:
@@ -207,6 +183,10 @@ module ActiveRecord
207
183
  false
208
184
  end
209
185
 
186
+ def data_sources
187
+ tables
188
+ end
189
+
210
190
  protected
211
191
 
212
192
  def table_structure(table_name)
@@ -214,6 +194,7 @@ module ActiveRecord
214
194
  raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if data.empty?
215
195
  data
216
196
  end
197
+ alias column_definitions table_structure
217
198
 
218
199
  def last_inserted_id(result)
219
200
  result
@@ -1,9 +1,6 @@
1
1
  require 'active_record/connection_adapters/clickhouse_adapter'
2
2
 
3
- if defined?(Rails::Railtie)
4
- require 'clickhouse-activerecord/railtie'
5
- require 'clickhouse-activerecord/schema_dumper'
6
- end
3
+ require 'clickhouse/railtie' if defined?(Rails::Railtie)
7
4
 
8
5
  module ClickhouseActiverecord
9
6
 
@@ -1,4 +1,4 @@
1
- module ClickhouseActiverecord
1
+ module Clickhouse
2
2
  require 'rails'
3
3
 
4
4
  class Railtie < Rails::Railtie
@@ -14,7 +14,7 @@ namespace :clickhouse do
14
14
  filename = "#{Rails.root}/db/clickhouse_schema.rb"
15
15
  File.open(filename, 'w:utf-8') do |file|
16
16
  ActiveRecord::Base.establish_connection(:"#{Rails.env}_clickhouse")
17
- ClickhouseActiverecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
17
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
18
18
  end
19
19
  end
20
20
 
metadata CHANGED
@@ -1,81 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clickhouse-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Odintsov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-20 00:00:00.000000000 Z
11
+ date: 2017-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.13'
20
- - - ">="
17
+ - - '>='
21
18
  - !ruby/object:Gem::Version
22
19
  version: 1.13.4
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '1.13'
30
- - - ">="
24
+ - - '>='
31
25
  - !ruby/object:Gem::Version
32
26
  version: 1.13.4
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: rails
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
- - - ">="
31
+ - - '>='
38
32
  - !ruby/object:Gem::Version
39
- version: 4.1.8
40
- - - "<"
41
- - !ruby/object:Gem::Version
42
- version: '5'
33
+ version: '5.0'
43
34
  type: :runtime
44
35
  prerelease: false
45
36
  version_requirements: !ruby/object:Gem::Requirement
46
37
  requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: 4.1.8
50
- - - "<"
38
+ - - '>='
51
39
  - !ruby/object:Gem::Version
52
- version: '5'
40
+ version: '5.0'
53
41
  - !ruby/object:Gem::Dependency
54
42
  name: bundler
55
43
  requirement: !ruby/object:Gem::Requirement
56
44
  requirements:
57
- - - "~>"
45
+ - - ~>
58
46
  - !ruby/object:Gem::Version
59
47
  version: '1.15'
60
48
  type: :development
61
49
  prerelease: false
62
50
  version_requirements: !ruby/object:Gem::Requirement
63
51
  requirements:
64
- - - "~>"
52
+ - - ~>
65
53
  - !ruby/object:Gem::Version
66
54
  version: '1.15'
67
55
  - !ruby/object:Gem::Dependency
68
56
  name: rake
69
57
  requirement: !ruby/object:Gem::Requirement
70
58
  requirements:
71
- - - "~>"
59
+ - - ~>
72
60
  - !ruby/object:Gem::Version
73
61
  version: '10.0'
74
62
  type: :development
75
63
  prerelease: false
76
64
  version_requirements: !ruby/object:Gem::Requirement
77
65
  requirements:
78
- - - "~>"
66
+ - - ~>
79
67
  - !ruby/object:Gem::Version
80
68
  version: '10.0'
81
69
  description: ActiveRecord adapter for ClickHouse
@@ -85,8 +73,7 @@ executables: []
85
73
  extensions: []
86
74
  extra_rdoc_files: []
87
75
  files:
88
- - ".gitignore"
89
- - CHANGELOG.md
76
+ - .gitignore
90
77
  - CODE_OF_CONDUCT.md
91
78
  - Gemfile
92
79
  - LICENSE.txt
@@ -100,10 +87,7 @@ files:
100
87
  - lib/active_record/connection_adapters/clickhouse/oid/date_time.rb
101
88
  - lib/active_record/connection_adapters/clickhouse_adapter.rb
102
89
  - lib/clickhouse-activerecord.rb
103
- - lib/clickhouse-activerecord/arel/visitors/to_sql.rb
104
- - lib/clickhouse-activerecord/railtie.rb
105
- - lib/clickhouse-activerecord/schema_dumper.rb
106
- - lib/clickhouse-activerecord/version.rb
90
+ - lib/clickhouse/railtie.rb
107
91
  - lib/tasks/clickhouse.rake
108
92
  homepage: https://github.com/pnixx/clickhouse-activerecord
109
93
  licenses:
@@ -115,19 +99,18 @@ require_paths:
115
99
  - lib
116
100
  required_ruby_version: !ruby/object:Gem::Requirement
117
101
  requirements:
118
- - - ">="
102
+ - - '>='
119
103
  - !ruby/object:Gem::Version
120
104
  version: '0'
121
105
  required_rubygems_version: !ruby/object:Gem::Requirement
122
106
  requirements:
123
- - - ">="
107
+ - - '>='
124
108
  - !ruby/object:Gem::Version
125
109
  version: '0'
126
110
  requirements: []
127
111
  rubyforge_project:
128
- rubygems_version: 2.6.12
112
+ rubygems_version: 2.0.14.1
129
113
  signing_key:
130
114
  specification_version: 4
131
115
  summary: ClickHouse ActiveRecord
132
116
  test_files: []
133
- has_rdoc:
@@ -1,13 +0,0 @@
1
- ### Version 0.1.4 (Nov 29, 2017)
2
-
3
- * Support materialized view
4
- * Aggregated functions for view
5
- * Schema dumper with SQL create table
6
-
7
- ### Version 0.1.2 (Sep 27, 2017)
8
-
9
- * Fix Big Int type
10
-
11
- ### Version 0.1.0 (Aug 31, 2017)
12
-
13
- * Initial release
@@ -1,20 +0,0 @@
1
- require 'arel/visitors/to_sql'
2
-
3
- module ClickhouseActiverecord
4
- module Arel
5
- module Visitors
6
- class ToSql < ::Arel::Visitors::ToSql
7
-
8
- def aggregate(name, o, collector)
9
- # replacing function name for materialized view
10
- if o.expressions.first && o.expressions.first != '*' && !o.expressions.first.is_a?(String) && o.expressions.first.relation && o.expressions.first.relation.engine && o.expressions.first.relation.engine.is_view
11
- super("#{name.downcase}Merge", o, collector)
12
- else
13
- super
14
- end
15
- end
16
-
17
- end
18
- end
19
- end
20
- end
@@ -1,10 +0,0 @@
1
- class ClickhouseActiverecord::SchemaDumper < ActiveRecord::SchemaDumper
2
-
3
- def table(table, stream)
4
-
5
- stream.puts " # TABLE: #{table}"
6
- stream.puts " # SQL: #{@connection.query("SHOW CREATE TABLE #{table.gsub(/^\.inner\./, '')}")['data'].try(:first).try(:first)}"
7
- super(table.gsub(/^\.inner\./, ''), stream)
8
-
9
- end
10
- end
@@ -1,3 +0,0 @@
1
- module ClickhouseActiverecord
2
- VERSION = '0.1.9'
3
- end