clickhouse-activerecord 0.3.12 → 0.3.13

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: 50dff8ae7219e2f3a04ee530331d19ca0b61396437723819891c1d8b41f0692d
4
- data.tar.gz: 75d50e8e2f32fa84f990bc15da6177082b7a0b0d5a508cba822b3c3afbc60c41
3
+ metadata.gz: 67ea4a63bb140adb5f6de102c38c097ea18f61499a882574952a9982b8d340cf
4
+ data.tar.gz: a1d2a759dae9062a20cc0a53bbd1cb64142062ce9ef84047f7e953b7d957e9b2
5
5
  SHA512:
6
- metadata.gz: ebdeb04ba96eb81eb76efdbd09580b51719656136de38b973cea267bc1405dba03cd417058df1909b9bd1e4279c97d845292947de251792f94fc9f0b30cf65cb
7
- data.tar.gz: 3a0b0feda885ab74f54f6d61b78ff02efed20d403b0bc4bff99b83ee811d4988e8620c92a84e549bc70e0ccb93c662e665a562bb51d49b5ed552bc850c076696
6
+ metadata.gz: 243b4d089d6349d8c12c529fbb80078ffbaa03f0033c3c69be77751846b031398895a1efdd08460699636277695a3e03eb228a398b5887a9426f4f3b63496c31
7
+ data.tar.gz: f790bca5af79492390de98ba2dce363e16207d987093c2e94e9f9ab55b9f769a41bae1c4a12d0b9d3757ee91f92604c1df4db17479dbbc85ced0f48294b297f3
data/README.md CHANGED
@@ -31,6 +31,8 @@ default: &default
31
31
  ssl: true # optional for using ssl connection
32
32
  debug: true # use for showing in to log technical information
33
33
  migrations_paths: db/clickhouse # optional, default: db/migrate_clickhouse
34
+ cluster: 'cluster_name' # optional for creating tables in cluster
35
+ replica: '{shard}' # optional for creating system tables for shards
34
36
  ```
35
37
 
36
38
  ## Usage in Rails 5
@@ -99,6 +101,11 @@ Create / drop / purge / reset database:
99
101
  $ rake clickhouse:drop
100
102
  $ rake clickhouse:purge
101
103
  $ rake clickhouse:reset
104
+
105
+ Prepare system tables for rails:
106
+
107
+ $ rake clickhouse:prepare_schema_migration_table
108
+ $ rake clickhouse:prepare_internal_metadata_table
102
109
 
103
110
  Migration:
104
111
 
@@ -17,7 +17,7 @@ module ActiveRecord
17
17
  def exec_query(sql, name = nil, binds = [], prepare: false)
18
18
  result = do_execute(sql, name)
19
19
  ActiveRecord::Result.new(result['meta'].map { |m| m['name'] }, result['data'])
20
- rescue StandartError => _e
20
+ rescue StandardError => _e
21
21
  raise ActiveRecord::ActiveRecordError, "Response: #{result}"
22
22
  end
23
23
 
@@ -36,7 +36,7 @@ module ActiveRecord
36
36
  end
37
37
 
38
38
  def table_options(table)
39
- sql = do_system_execute("SHOW CREATE TABLE #{table}")['data'].try(:first).try(:first)
39
+ sql = do_system_execute("SHOW CREATE TABLE `#{table}`")['data'].try(:first).try(:first)
40
40
  { options: sql.gsub(/^(?:.*?)ENGINE = (.*?)$/, '\\1') }
41
41
  end
42
42
 
@@ -118,7 +118,7 @@ module ActiveRecord
118
118
  protected
119
119
 
120
120
  def table_structure(table_name)
121
- result = do_system_execute("DESCRIBE TABLE #{table_name}", table_name)
121
+ result = do_system_execute("DESCRIBE TABLE `#{table_name}`", table_name)
122
122
  data = result['data']
123
123
 
124
124
  return data unless data.empty?
@@ -202,16 +202,24 @@ module ActiveRecord
202
202
 
203
203
  # Create a new ClickHouse database.
204
204
  def create_database(name)
205
- sql = "CREATE DATABASE #{quote_table_name(name)}"
205
+ sql = apply_cluster "CREATE DATABASE #{quote_table_name(name)}"
206
206
  log_with_debug(sql, adapter_name) do
207
207
  res = @connection.post("/?#{@config.except(:database).to_param}", "CREATE DATABASE #{quote_table_name(name)}")
208
208
  process_response(res)
209
209
  end
210
210
  end
211
211
 
212
+ def create_table(table_name, comment: nil, **options)
213
+ super(
214
+ apply_cluster(table_name),
215
+ comment: comment,
216
+ **options
217
+ )
218
+ end
219
+
212
220
  # Drops a ClickHouse database.
213
221
  def drop_database(name) #:nodoc:
214
- sql = "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
222
+ sql = apply_cluster "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
215
223
  log_with_debug(sql, adapter_name) do
216
224
  res = @connection.post("/?#{@config.except(:database).to_param}", sql)
217
225
  process_response(res)
@@ -219,7 +227,7 @@ module ActiveRecord
219
227
  end
220
228
 
221
229
  def drop_table(table_name, options = {}) # :nodoc:
222
- do_execute "DROP TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}"
230
+ do_execute apply_cluster "DROP TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}"
223
231
  end
224
232
 
225
233
  protected
@@ -233,6 +241,14 @@ module ActiveRecord
233
241
  def connect
234
242
  @connection = Net::HTTP.start(@connection_parameters[0], @connection_parameters[1], use_ssl: @connection_parameters[2], verify_mode: OpenSSL::SSL::VERIFY_NONE)
235
243
  end
244
+
245
+ def cluster
246
+ @full_config[:cluster]
247
+ end
248
+
249
+ def apply_cluster(sql)
250
+ cluster ? "#{sql} ON CLUSTER #{cluster}" : sql
251
+ end
236
252
  end
237
253
  end
238
254
  end
@@ -3,7 +3,7 @@ module ClickhouseActiverecord
3
3
 
4
4
  def table(table, stream)
5
5
  stream.puts " # TABLE: #{table}"
6
- stream.puts " # SQL: #{@connection.do_system_execute("SHOW CREATE TABLE #{table.gsub(/^\.inner\./, '')}")['data'].try(:first).try(:first)}"
6
+ stream.puts " # SQL: #{@connection.do_system_execute("SHOW CREATE TABLE `#{table.gsub(/^\.inner\./, '')}`")['data'].try(:first).try(:first)}"
7
7
  super(table.gsub(/^\.inner\./, ''), stream)
8
8
  end
9
9
  end
@@ -1,3 +1,3 @@
1
1
  module ClickhouseActiverecord
2
- VERSION = '0.3.12'
2
+ VERSION = '0.3.13'
3
3
  end
@@ -2,6 +2,70 @@
2
2
 
3
3
  namespace :clickhouse do
4
4
 
5
+ task prepare_schema_migration_table: :environment do
6
+ cluster, database, replica = ActiveRecord::Base.connection_config.values_at(:cluster, :database, :replica)
7
+ return if cluster.nil?
8
+
9
+ connection = ActiveRecord::Base.connection
10
+ key_options = connection.internal_string_options_for_primary_key
11
+ block = Proc.new do |t|
12
+ t.string :version, key_options
13
+ end
14
+ distributed_table_name = ".#{ActiveRecord::SchemaMigration.table_name}_distributed"
15
+ unless connection.table_exists?(distributed_table_name)
16
+ options = { id: false }
17
+ if replica
18
+ shard = replica.is_a?(String) ? replica : '{shard}'
19
+ options[:options] = <<-SQL
20
+ ReplicatedMergeTree('/clickhouse/tables/{cluster}/#{shard}/#{database}.`#{distributed_table_name}`', '{replica}')
21
+ PARTITION BY version ORDER BY (version) SETTINGS index_granularity = 8192
22
+ SQL
23
+ end
24
+ connection.create_table("`#{distributed_table_name}`", options, &block)
25
+ end
26
+ unless connection.table_exists?(ActiveRecord::SchemaMigration.table_name)
27
+ connection.create_table(
28
+ ActiveRecord::SchemaMigration.table_name,
29
+ id: false,
30
+ options: "Distributed(#{cluster},#{database},`#{distributed_table_name}`,sipHash64(version))",
31
+ &block
32
+ )
33
+ end
34
+ end
35
+
36
+ task prepare_internal_metadata_table: :environment do
37
+ cluster, database, replica = ActiveRecord::Base.connection_config.values_at(:cluster, :database, :replica)
38
+ return if cluster.nil?
39
+
40
+ connection = ActiveRecord::Base.connection
41
+ key_options = connection.internal_string_options_for_primary_key
42
+ block = Proc.new do |t|
43
+ t.string :key, key_options
44
+ t.string :value
45
+ t.timestamps
46
+ end
47
+ distributed_table_name = ".#{ActiveRecord::InternalMetadata.table_name}_distributed"
48
+ unless connection.table_exists?(distributed_table_name)
49
+ options = { id: false }
50
+ if replica
51
+ shard = replica.is_a?(String) ? replica : '{shard}'
52
+ options[:options] = <<-SQL
53
+ ReplicatedMergeTree('/clickhouse/tables/{cluster}/#{shard}/#{database}.`#{distributed_table_name}`', '{replica}')
54
+ PARTITION BY toDate(created_at) ORDER BY (created_at) SETTINGS index_granularity = 8192
55
+ SQL
56
+ end
57
+ connection.create_table("`#{distributed_table_name}`", options, &block)
58
+ end
59
+ unless connection.table_exists?(ActiveRecord::InternalMetadata.table_name)
60
+ connection.create_table(
61
+ ActiveRecord::InternalMetadata.table_name,
62
+ id: false,
63
+ options: "Distributed(#{cluster},#{database},`#{distributed_table_name}`,sipHash64(created_at))",
64
+ &block
65
+ )
66
+ end
67
+ end
68
+
5
69
  task load_config: :environment do
6
70
  ENV['SCHEMA'] = "db/clickhouse_schema.rb"
7
71
  ActiveRecord::Migrator.migrations_paths = ["db/migrate_clickhouse"]
@@ -61,7 +125,7 @@ namespace :clickhouse do
61
125
  end
62
126
 
63
127
  desc 'Migrate the clickhouse database'
64
- task migrate: :load_config do
128
+ task migrate: [:load_config, :prepare_schema_migration_table, :prepare_internal_metadata_table] do
65
129
  Rake::Task['db:migrate'].execute
66
130
  end
67
131
  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.12
4
+ version: 0.3.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Odintsov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-14 00:00:00.000000000 Z
11
+ date: 2020-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler