clickhouse-activerecord 0.6.1 → 0.6.2
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 +4 -4
- data/clickhouse-activerecord.gemspec +1 -1
- data/lib/active_record/connection_adapters/clickhouse_adapter.rb +64 -51
- data/lib/clickhouse-activerecord/tasks.rb +2 -3
- data/lib/clickhouse-activerecord/version.rb +1 -1
- data/lib/core_extensions/arel/nodes/select_statement.rb +1 -1
- data/lib/tasks/clickhouse.rake +26 -24
- metadata +5 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6533de4ab9b2415a4df22e72e7ef551d4fb58c5bdc6713ec355b85dbad1174fd
|
4
|
+
data.tar.gz: 1c0a898f494c6dc7511897617ea86c2f12aa42461cf693e28e78124bde3638c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6c5c2eba18dce09211cc6b8e5448aaaf2c40ca3eb5cc411aa76874ad6efc00ae257880e5997256808d02771aec7947559edfca941187c219b6724f09b9488f0
|
7
|
+
data.tar.gz: 7b7e5f520c4189b4b5a1587789d0ea2ef8b74e86c077611ac18515c45105306c6e17cfd824623ab574420beeb45d205a9f301e2db9f3befd3c3b92c4d303b978
|
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.require_paths = ['lib']
|
25
25
|
|
26
26
|
spec.add_runtime_dependency 'bundler', '>= 1.13.4'
|
27
|
-
spec.add_runtime_dependency 'activerecord', '
|
27
|
+
spec.add_runtime_dependency 'activerecord', '~> 7.0.0'
|
28
28
|
|
29
29
|
spec.add_development_dependency 'rake', '~> 13.0'
|
30
30
|
spec.add_development_dependency 'rspec', '~> 3.4'
|
@@ -159,66 +159,73 @@ module ActiveRecord
|
|
159
159
|
!native_database_types[type].nil?
|
160
160
|
end
|
161
161
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
162
|
+
class << self
|
163
|
+
def extract_limit(sql_type) # :nodoc:
|
164
|
+
case sql_type
|
165
|
+
when /(Nullable)?\(?String\)?/
|
166
|
+
super('String')
|
167
|
+
when /(Nullable)?\(?U?Int8\)?/
|
168
|
+
1
|
169
|
+
when /(Nullable)?\(?U?Int16\)?/
|
170
|
+
2
|
171
|
+
when /(Nullable)?\(?U?Int32\)?/
|
172
|
+
nil
|
173
|
+
when /(Nullable)?\(?U?Int64\)?/
|
174
|
+
8
|
175
|
+
else
|
176
|
+
super
|
177
|
+
end
|
176
178
|
end
|
177
|
-
end
|
178
179
|
|
179
|
-
|
180
|
-
|
180
|
+
# `extract_scale` and `extract_precision` are the same as in the Rails abstract base class,
|
181
|
+
# except this permits a space after the comma
|
181
182
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
183
|
+
def extract_scale(sql_type)
|
184
|
+
case sql_type
|
185
|
+
when /\((\d+)\)/ then 0
|
186
|
+
when /\((\d+)(,\s?(\d+))\)/ then $3.to_i
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def extract_precision(sql_type)
|
191
|
+
$1.to_i if sql_type =~ /\((\d+)(,\s?\d+)?\)/
|
186
192
|
end
|
187
|
-
end
|
188
193
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
# register_class_with_limit m, %r(Array), Clickhouse::OID::Array
|
213
|
-
m.register_type(%r(Array)) do |sql_type|
|
214
|
-
Clickhouse::OID::Array.new(sql_type)
|
194
|
+
def initialize_type_map(m) # :nodoc:
|
195
|
+
super
|
196
|
+
register_class_with_limit m, %r(String), Type::String
|
197
|
+
register_class_with_limit m, 'Date', Clickhouse::OID::Date
|
198
|
+
register_class_with_precision m, %r(datetime)i, Clickhouse::OID::DateTime
|
199
|
+
|
200
|
+
register_class_with_limit m, %r(Int8), Type::Integer
|
201
|
+
register_class_with_limit m, %r(Int16), Type::Integer
|
202
|
+
register_class_with_limit m, %r(Int32), Type::Integer
|
203
|
+
register_class_with_limit m, %r(Int64), Type::Integer
|
204
|
+
register_class_with_limit m, %r(Int128), Type::Integer
|
205
|
+
register_class_with_limit m, %r(Int256), Type::Integer
|
206
|
+
|
207
|
+
register_class_with_limit m, %r(UInt8), Type::UnsignedInteger
|
208
|
+
register_class_with_limit m, %r(UInt16), Type::UnsignedInteger
|
209
|
+
register_class_with_limit m, %r(UInt32), Type::UnsignedInteger
|
210
|
+
register_class_with_limit m, %r(UInt64), Type::UnsignedInteger
|
211
|
+
#register_class_with_limit m, %r(UInt128), Type::UnsignedInteger #not implemnted in clickhouse
|
212
|
+
register_class_with_limit m, %r(UInt256), Type::UnsignedInteger
|
213
|
+
# register_class_with_limit m, %r(Array), Clickhouse::OID::Array
|
214
|
+
m.register_type(%r(Array)) do |sql_type|
|
215
|
+
Clickhouse::OID::Array.new(sql_type)
|
216
|
+
end
|
215
217
|
end
|
216
218
|
end
|
217
219
|
|
218
|
-
|
220
|
+
# In Rails 7 used constant TYPE_MAP, we need redefine method
|
221
|
+
def type_map
|
222
|
+
@type_map ||= Type::TypeMap.new.tap { |m| ClickhouseAdapter.initialize_type_map(m) }
|
223
|
+
end
|
224
|
+
|
225
|
+
def quote(value)
|
219
226
|
case value
|
220
227
|
when Array
|
221
|
-
'[' + value.map { |v|
|
228
|
+
'[' + value.map { |v| quote(v) }.join(', ') + ']'
|
222
229
|
else
|
223
230
|
super
|
224
231
|
end
|
@@ -334,7 +341,13 @@ module ActiveRecord
|
|
334
341
|
end
|
335
342
|
|
336
343
|
def drop_table(table_name, options = {}) # :nodoc:
|
337
|
-
|
344
|
+
query = "DROP TABLE"
|
345
|
+
query = "#{query} IF EXISTS " if options[:if_exists]
|
346
|
+
query = "#{query} #{quote_table_name(table_name)}"
|
347
|
+
query = apply_cluster(query)
|
348
|
+
query = "#{query} SYNC" if options[:sync]
|
349
|
+
|
350
|
+
do_execute(query)
|
338
351
|
|
339
352
|
if options[:with_distributed]
|
340
353
|
distributed_table_name = options.delete(:with_distributed)
|
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
module ClickhouseActiverecord
|
4
4
|
class Tasks
|
5
|
-
|
6
5
|
delegate :connection, :establish_connection, :clear_active_connections!, to: ActiveRecord::Base
|
7
6
|
|
8
7
|
def initialize(configuration)
|
@@ -11,7 +10,7 @@ module ClickhouseActiverecord
|
|
11
10
|
|
12
11
|
def create
|
13
12
|
establish_master_connection
|
14
|
-
connection.create_database @configuration[
|
13
|
+
connection.create_database @configuration['database']
|
15
14
|
rescue ActiveRecord::StatementInvalid => e
|
16
15
|
if e.cause.to_s.include?('already exists')
|
17
16
|
raise ActiveRecord::DatabaseAlreadyExists
|
@@ -22,7 +21,7 @@ module ClickhouseActiverecord
|
|
22
21
|
|
23
22
|
def drop
|
24
23
|
establish_master_connection
|
25
|
-
connection.drop_database @configuration[
|
24
|
+
connection.drop_database @configuration['database']
|
26
25
|
end
|
27
26
|
|
28
27
|
def purge
|
data/lib/tasks/clickhouse.rake
CHANGED
@@ -1,68 +1,70 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
namespace :clickhouse do
|
4
|
-
|
5
4
|
task prepare_schema_migration_table: :environment do
|
6
|
-
ClickhouseActiverecord::SchemaMigration.create_table unless ENV['simple'] || ARGV.
|
5
|
+
ClickhouseActiverecord::SchemaMigration.create_table unless ENV['simple'] || ARGV.any? { |a| a.include?('--simple') }
|
7
6
|
end
|
8
7
|
|
9
8
|
task prepare_internal_metadata_table: :environment do
|
10
|
-
ClickhouseActiverecord::InternalMetadata.create_table unless ENV['simple'] || ARGV.
|
9
|
+
ClickhouseActiverecord::InternalMetadata.create_table unless ENV['simple'] || ARGV.any? { |a| a.include?('--simple') }
|
11
10
|
end
|
12
11
|
|
13
12
|
task load_config: :environment do
|
14
|
-
ENV['SCHEMA'] =
|
15
|
-
ActiveRecord::Migrator.migrations_paths = [
|
16
|
-
ActiveRecord::Base.establish_connection(:
|
13
|
+
ENV['SCHEMA'] = 'db/clickhouse_schema.rb'
|
14
|
+
ActiveRecord::Migrator.migrations_paths = %w[db/migrate_clickhouse]
|
15
|
+
ActiveRecord::Base.establish_connection(:clickhouse)
|
17
16
|
end
|
18
17
|
|
19
18
|
namespace :schema do
|
20
|
-
|
21
|
-
# todo not testing
|
19
|
+
# TODO: not testing
|
22
20
|
desc 'Load database schema'
|
23
|
-
task load: [
|
24
|
-
simple = ENV['simple'] || ARGV.
|
21
|
+
task load: %i[load_config prepare_internal_metadata_table] do
|
22
|
+
simple = ENV['simple'] || ARGV.any? { |a| a.include?('--simple') } ? '_simple' : nil
|
25
23
|
ClickhouseActiverecord::SchemaMigration.drop_table
|
26
|
-
load(
|
24
|
+
load(Rails.root.join("db/clickhouse_schema#{simple}.rb"))
|
27
25
|
end
|
28
26
|
|
29
27
|
desc 'Dump database schema'
|
30
|
-
task dump: :environment do |
|
31
|
-
simple = ENV['simple'] || args[:simple] || ARGV.
|
32
|
-
filename =
|
28
|
+
task dump: :environment do |_, args|
|
29
|
+
simple = ENV['simple'] || args[:simple] || ARGV.any? { |a| a.include?('--simple') } ? '_simple' : nil
|
30
|
+
filename = Rails.root.join("db/clickhouse_schema#{simple}.rb")
|
33
31
|
File.open(filename, 'w:utf-8') do |file|
|
34
|
-
ActiveRecord::Base.establish_connection(:
|
35
|
-
ClickhouseActiverecord::SchemaDumper.dump(ActiveRecord::Base.connection, file, ActiveRecord::Base,
|
32
|
+
ActiveRecord::Base.establish_connection(:clickhouse)
|
33
|
+
ClickhouseActiverecord::SchemaDumper.dump(ActiveRecord::Base.connection, file, ActiveRecord::Base, simple.present?)
|
36
34
|
end
|
37
35
|
end
|
38
|
-
|
39
36
|
end
|
40
37
|
|
41
38
|
namespace :structure do
|
42
39
|
desc 'Load database structure'
|
43
40
|
task load: [:load_config, 'db:check_protected_environments'] do
|
44
|
-
|
41
|
+
config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'clickhouse')
|
42
|
+
ClickhouseActiverecord::Tasks.new(config).structure_load(Rails.root.join('db/clickhouse_structure.sql'))
|
45
43
|
end
|
46
44
|
|
47
45
|
desc 'Dump database structure'
|
48
46
|
task dump: [:load_config, 'db:check_protected_environments'] do
|
49
|
-
|
47
|
+
config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'clickhouse')
|
48
|
+
ClickhouseActiverecord::Tasks.new(config).structure_dump(Rails.root.join('db/clickhouse_structure.sql'))
|
50
49
|
end
|
51
50
|
end
|
52
51
|
|
53
52
|
desc 'Creates the database from DATABASE_URL or config/database.yml'
|
54
53
|
task create: [:load_config] do
|
55
|
-
ActiveRecord::
|
54
|
+
config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'clickhouse')
|
55
|
+
ActiveRecord::Tasks::DatabaseTasks.create(config)
|
56
56
|
end
|
57
57
|
|
58
58
|
desc 'Drops the database from DATABASE_URL or config/database.yml'
|
59
59
|
task drop: [:load_config, 'db:check_protected_environments'] do
|
60
|
-
ActiveRecord::
|
60
|
+
config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'clickhouse')
|
61
|
+
ActiveRecord::Tasks::DatabaseTasks.drop(config)
|
61
62
|
end
|
62
63
|
|
63
64
|
desc 'Empty the database from DATABASE_URL or config/database.yml'
|
64
65
|
task purge: [:load_config, 'db:check_protected_environments'] do
|
65
|
-
ActiveRecord::
|
66
|
+
config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'clickhouse')
|
67
|
+
ActiveRecord::Tasks::DatabaseTasks.purge(config)
|
66
68
|
end
|
67
69
|
|
68
70
|
# desc 'Resets your database using your migrations for the current environment'
|
@@ -72,7 +74,7 @@ namespace :clickhouse do
|
|
72
74
|
end
|
73
75
|
|
74
76
|
desc 'Migrate the clickhouse database'
|
75
|
-
task migrate: [
|
77
|
+
task migrate: %i[load_config prepare_schema_migration_table prepare_internal_metadata_table] do
|
76
78
|
Rake::Task['db:migrate'].execute
|
77
79
|
if File.exists? "#{Rails.root}/db/clickhouse_schema_simple.rb"
|
78
80
|
Rake::Task['clickhouse:schema:dump'].execute(simple: true)
|
@@ -80,7 +82,7 @@ namespace :clickhouse do
|
|
80
82
|
end
|
81
83
|
|
82
84
|
desc 'Rollback the clickhouse database'
|
83
|
-
task rollback: [
|
85
|
+
task rollback: %i[load_config prepare_schema_migration_table prepare_internal_metadata_table] do
|
84
86
|
Rake::Task['db:rollback'].execute
|
85
87
|
end
|
86
88
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clickhouse-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Odintsov
|
@@ -28,22 +28,16 @@ dependencies:
|
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '5.2'
|
34
|
-
- - "<"
|
31
|
+
- - "~>"
|
35
32
|
- !ruby/object:Gem::Version
|
36
|
-
version:
|
33
|
+
version: 7.0.0
|
37
34
|
type: :runtime
|
38
35
|
prerelease: false
|
39
36
|
version_requirements: !ruby/object:Gem::Requirement
|
40
37
|
requirements:
|
41
|
-
- - "
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '5.2'
|
44
|
-
- - "<"
|
38
|
+
- - "~>"
|
45
39
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
40
|
+
version: 7.0.0
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: rake
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|