clickhouse-activerecord 0.3.8 → 0.3.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 +5 -5
- data/README.md +40 -8
- data/lib/active_record/connection_adapters/clickhouse/schema_statements.rb +14 -40
- data/lib/active_record/connection_adapters/clickhouse_adapter.rb +22 -9
- data/lib/clickhouse-activerecord/version.rb +1 -1
- data/lib/generators/clickhouse_migration_generator.rb +15 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 69358e69c13cdf1d70ec417e4ef9e4374578eb63144bc713bdd3e934702d8118
|
4
|
+
data.tar.gz: 2a52857c0080ad2959c8d34b52a43185f911ff215ac52c1584dacc52e3047f1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7a6c0845166b79e719e1fff667de0dfb38e132154a6acdf862d50cb82d7281dbecd53f4c4e756b5bc5e30ad3b6b3aa710eebb8220f0f496991fd4e01513238f
|
7
|
+
data.tar.gz: f3987a2a0a77233a37ecb1805b6e4bd0dce76ce2bf2c18fc58e62ee62bb9a573ac8851cf06f56d0b1bf80e8fe3a14eda1105fdd3f472c4f54694fd0811fd2bcd
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Clickhouse::Activerecord
|
2
2
|
|
3
3
|
A Ruby database ActiveRecord driver for ClickHouse. Support Rails >= 5.2.
|
4
|
+
Tested on ClickHouse version 18.14.
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -17,8 +18,22 @@ And then execute:
|
|
17
18
|
Or install it yourself as:
|
18
19
|
|
19
20
|
$ gem install clickhouse-activerecord
|
21
|
+
|
22
|
+
## Available database connection parameters
|
23
|
+
```yml
|
24
|
+
default: &default
|
25
|
+
adapter: clickhouse
|
26
|
+
database: database
|
27
|
+
host: localhost
|
28
|
+
port: 8123
|
29
|
+
username: username
|
30
|
+
password: password
|
31
|
+
ssl: true # optional for using ssl connection
|
32
|
+
debug: true # use for showing in to log technical information
|
33
|
+
migrations_paths: db/clickhouse # optional, default: db/migrate_clickhouse
|
34
|
+
```
|
20
35
|
|
21
|
-
## Usage
|
36
|
+
## Usage in Rails 5
|
22
37
|
|
23
38
|
Add your `database.yml` connection information with postfix `_clickhouse` for you environment:
|
24
39
|
|
@@ -26,10 +41,6 @@ Add your `database.yml` connection information with postfix `_clickhouse` for yo
|
|
26
41
|
development_clickhouse:
|
27
42
|
adapter: clickhouse
|
28
43
|
database: database
|
29
|
-
host: localhost
|
30
|
-
username: username
|
31
|
-
password: password
|
32
|
-
debug: true # use for showing in to log technical information
|
33
44
|
```
|
34
45
|
|
35
46
|
Add to your model:
|
@@ -54,13 +65,34 @@ Or global connection:
|
|
54
65
|
development:
|
55
66
|
adapter: clickhouse
|
56
67
|
database: database
|
57
|
-
|
58
|
-
|
59
|
-
|
68
|
+
```
|
69
|
+
|
70
|
+
## Usage in Rails 6 with second database
|
71
|
+
|
72
|
+
Add your `database.yml` connection information for you environment:
|
73
|
+
|
74
|
+
```yml
|
75
|
+
development:
|
76
|
+
primary:
|
77
|
+
...
|
78
|
+
|
79
|
+
clickhouse:
|
80
|
+
adapter: clickhouse
|
81
|
+
database: database
|
82
|
+
```
|
83
|
+
|
84
|
+
Connection [Multiple Databases with Active Record](https://guides.rubyonrails.org/active_record_multiple_databases.html) or short example:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class Action < ActiveRecord::Base
|
88
|
+
connects_to database: { writing: :clickhouse, reading: :clickhouse }
|
89
|
+
end
|
60
90
|
```
|
61
91
|
|
62
92
|
### Rake tasks
|
63
93
|
|
94
|
+
**Note!** For Rails 6 you can use default rake tasks if you configure `migrations_paths` in your `database.yml`, for example: `rake db:migrate`
|
95
|
+
|
64
96
|
Create / drop / purge / reset database:
|
65
97
|
|
66
98
|
$ rake clickhouse:create
|
@@ -38,37 +38,6 @@ module ActiveRecord
|
|
38
38
|
{ options: sql.gsub(/^(?:.*?)ENGINE = (.*?)$/, '\\1') }
|
39
39
|
end
|
40
40
|
|
41
|
-
# @todo copied from ActiveRecord::ConnectionAdapters::SchemaStatements v5.2.2
|
42
|
-
# Why version column type of String, but insert to Integer?
|
43
|
-
def assume_migrated_upto_version(version, migrations_paths)
|
44
|
-
migrations_paths = Array(migrations_paths)
|
45
|
-
version = version.to_i
|
46
|
-
sm_table = quote_table_name(ActiveRecord::SchemaMigration.table_name)
|
47
|
-
|
48
|
-
migrated = ActiveRecord::SchemaMigration.all_versions.map(&:to_i)
|
49
|
-
versions = migration_context.migration_files.map do |file|
|
50
|
-
migration_context.parse_migration_filename(file).first.to_i
|
51
|
-
end
|
52
|
-
|
53
|
-
unless migrated.include?(version)
|
54
|
-
do_execute( "INSERT INTO #{sm_table} (version) VALUES (#{quote(version.to_s)})", 'SchemaMigration', format: nil)
|
55
|
-
end
|
56
|
-
|
57
|
-
inserting = (versions - migrated).select { |v| v < version }
|
58
|
-
if inserting.any?
|
59
|
-
if (duplicate = inserting.detect { |v| inserting.count(v) > 1 })
|
60
|
-
raise "Duplicate migration #{duplicate}. Please renumber your migrations to resolve the conflict."
|
61
|
-
end
|
62
|
-
if supports_multi_insert?
|
63
|
-
do_system_execute insert_versions_sql(inserting.map(&:to_s))
|
64
|
-
else
|
65
|
-
inserting.each do |v|
|
66
|
-
do_system_execute insert_versions_sql(v)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
41
|
# Not indexes on clickhouse
|
73
42
|
def indexes(table_name, name = nil)
|
74
43
|
[]
|
@@ -86,21 +55,22 @@ module ActiveRecord
|
|
86
55
|
end
|
87
56
|
end
|
88
57
|
|
89
|
-
|
90
|
-
|
91
|
-
def apply_format(sql, format)
|
92
|
-
format ? "#{sql} FORMAT #{format}" : sql
|
93
|
-
end
|
94
|
-
|
95
|
-
def do_execute(sql, name = nil, format: 'JSONCompact')
|
58
|
+
def do_execute(sql, name = nil, format: 'JSONCompact', settings: {})
|
96
59
|
log(sql, "#{adapter_name} #{name}") do
|
97
60
|
formatted_sql = apply_format(sql, format)
|
98
|
-
|
61
|
+
request_params = @config || {}
|
62
|
+
res = @connection.post("/?#{request_params.merge(settings).to_param}", formatted_sql)
|
99
63
|
|
100
64
|
process_response(res)
|
101
65
|
end
|
102
66
|
end
|
103
67
|
|
68
|
+
private
|
69
|
+
|
70
|
+
def apply_format(sql, format)
|
71
|
+
format ? "#{sql} FORMAT #{format}" : sql
|
72
|
+
end
|
73
|
+
|
104
74
|
def process_response(res)
|
105
75
|
case res.code.to_i
|
106
76
|
when 200
|
@@ -121,7 +91,11 @@ module ActiveRecord
|
|
121
91
|
end
|
122
92
|
|
123
93
|
def create_table_definition(*args)
|
124
|
-
|
94
|
+
if ActiveRecord::version >= Gem::Version.new('6')
|
95
|
+
Clickhouse::TableDefinition.new(self, *args)
|
96
|
+
else
|
97
|
+
Clickhouse::TableDefinition.new(*args)
|
98
|
+
end
|
125
99
|
end
|
126
100
|
|
127
101
|
def new_column_from_field(table_name, field)
|
@@ -18,6 +18,7 @@ module ActiveRecord
|
|
18
18
|
config = config.symbolize_keys
|
19
19
|
host = config[:host] || 'localhost'
|
20
20
|
port = config[:port] || 8123
|
21
|
+
ssl = config[:ssl].present? ? config[:ssl] : port == 443
|
21
22
|
|
22
23
|
if config.key?(:database)
|
23
24
|
database = config[:database]
|
@@ -25,7 +26,7 @@ module ActiveRecord
|
|
25
26
|
raise ArgumentError, 'No database specified. Missing argument: database.'
|
26
27
|
end
|
27
28
|
|
28
|
-
ConnectionAdapters::ClickhouseAdapter.new(
|
29
|
+
ConnectionAdapters::ClickhouseAdapter.new(logger, [host, port, ssl], { user: config[:username], password: config[:password], database: database }.compact, config)
|
29
30
|
end
|
30
31
|
end
|
31
32
|
end
|
@@ -92,21 +93,34 @@ module ActiveRecord
|
|
92
93
|
include Clickhouse::SchemaStatements
|
93
94
|
|
94
95
|
# Initializes and connects a Clickhouse adapter.
|
95
|
-
def initialize(
|
96
|
-
super(
|
96
|
+
def initialize(logger, connection_parameters, config, full_config)
|
97
|
+
super(nil, logger)
|
97
98
|
@connection_parameters = connection_parameters
|
98
99
|
@config = config
|
99
|
-
@debug = debug
|
100
|
+
@debug = full_config[:debug] || false
|
101
|
+
@full_config = full_config
|
100
102
|
|
101
|
-
|
103
|
+
@prepared_statements = false
|
104
|
+
if ActiveRecord::version == Gem::Version.new('6.0.0')
|
102
105
|
@prepared_statement_status = Concurrent::ThreadLocalVar.new(false)
|
103
|
-
else
|
104
|
-
@prepared_statements = false
|
105
106
|
end
|
106
107
|
|
107
108
|
connect
|
108
109
|
end
|
109
110
|
|
111
|
+
# Support SchemaMigration from v5.2.2 to v6+
|
112
|
+
def schema_migration # :nodoc:
|
113
|
+
if ActiveRecord::version >= Gem::Version.new('6')
|
114
|
+
super
|
115
|
+
else
|
116
|
+
ActiveRecord::SchemaMigration
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def migrations_paths
|
121
|
+
@full_config[:migrations_paths] || 'db/migrate_clickhouse'
|
122
|
+
end
|
123
|
+
|
110
124
|
def arel_visitor # :nodoc:
|
111
125
|
ClickhouseActiverecord::Arel::Visitors::ToSql.new(self)
|
112
126
|
end
|
@@ -217,8 +231,7 @@ module ActiveRecord
|
|
217
231
|
private
|
218
232
|
|
219
233
|
def connect
|
220
|
-
|
221
|
-
@connection = Net::HTTP.start(@connection_parameters[0], @connection_parameters[1], use_ssl: @connection_parameters[1] == 443)
|
234
|
+
@connection = Net::HTTP.start(@connection_parameters[0], @connection_parameters[1], use_ssl: @connection_parameters[2], verify_mode: OpenSSL::SSL::VERIFY_NONE)
|
222
235
|
end
|
223
236
|
end
|
224
237
|
end
|
@@ -6,6 +6,20 @@ class ClickhouseMigrationGenerator < ActiveRecord::Generators::MigrationGenerato
|
|
6
6
|
def create_migration_file
|
7
7
|
set_local_assigns!
|
8
8
|
validate_file_name!
|
9
|
-
migration_template @migration_template, "
|
9
|
+
migration_template @migration_template, File.join(db_migrate_path, "#{file_name}.rb")
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def db_migrate_path
|
15
|
+
if defined?(Rails.application) && Rails.application
|
16
|
+
configured_migrate_path || default_migrate_path
|
17
|
+
else
|
18
|
+
default_migrate_path
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def default_migrate_path
|
23
|
+
"db/migrate_clickhouse"
|
10
24
|
end
|
11
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clickhouse-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Odintsov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -147,8 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
147
|
- !ruby/object:Gem::Version
|
148
148
|
version: '0'
|
149
149
|
requirements: []
|
150
|
-
|
151
|
-
rubygems_version: 2.5.2.3
|
150
|
+
rubygems_version: 3.0.1
|
152
151
|
signing_key:
|
153
152
|
specification_version: 4
|
154
153
|
summary: ClickHouse ActiveRecord
|