clickhouse-activerecord 1.2.0 → 1.2.1

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
  SHA256:
3
- metadata.gz: c3301f161ab9a3c94507a1d3a989e32710628746eeeac895966e64dd25579dd4
4
- data.tar.gz: 2ab20aabc31a593987fb8905b5c58f023025a24cf447d0d922644ebbcff63397
3
+ metadata.gz: b40a04fe93423fd3469b65a66429ecb41e4c5c25440777c69617e69081e578b4
4
+ data.tar.gz: 43586cb853dec4d3453a6b6938158e5f1197504678c2e1aa824b8521a6a1c628
5
5
  SHA512:
6
- metadata.gz: f2d35a1489031909c0a2b8c2f8feb5624b93854bfbcabd9e39e006874033d0b534d568e72deabb795fcbeb1c0a3416966df8168b6f10d1ef64a3bb36d96b838f
7
- data.tar.gz: 9c6be79ff71e1207cd2b162e909db8b7db8a2276ce17351b2b2fc38097dc47f8c4dd8386cfacdc37bd1ccd3cfc28072dc45bd09b155737ef28b63a3a55f35def
6
+ metadata.gz: 38073e81994dd027caf7fab5463a715eac7f15ec8373c1415d0064611118b0f2b0c391cae0db9b7670ebb8a3141c023879b3d1425c7059f4c00450579bcff64c
7
+ data.tar.gz: 0ce78412004ae3590bc97877141e66f74f9de1ad0aab2cdc1af5a3369157c01e0e29fe10b4921c96487cad66fa16e50163f9c34941381357f1960cc3c7f20fbc
@@ -7,15 +7,16 @@ module ActiveRecord
7
7
  class Map < Type::Value # :nodoc:
8
8
 
9
9
  def initialize(sql_type)
10
- @subtype = case sql_type
11
- when /U?Int\d+/
12
- :integer
13
- when /DateTime/
14
- :datetime
15
- when /Date/
16
- :date
17
- else
18
- :string
10
+ case sql_type
11
+ when /U?Int(\d+)/
12
+ @subtype = :integer
13
+ @limit = bits_to_limit(Regexp.last_match(1)&.to_i)
14
+ when /DateTime/
15
+ @subtype = :datetime
16
+ when /Date/
17
+ @subtype = :date
18
+ else
19
+ @subtype = :string
19
20
  end
20
21
  end
21
22
 
@@ -65,6 +66,19 @@ module ActiveRecord
65
66
  end
66
67
  end
67
68
 
69
+ private
70
+
71
+ def bits_to_limit(bits)
72
+ case bits
73
+ when 8 then 1
74
+ when 16 then 2
75
+ when 32 then 4
76
+ when 64 then 8
77
+ when 128 then 16
78
+ when 256 then 32
79
+ end
80
+ end
81
+
68
82
  end
69
83
  end
70
84
  end
@@ -72,8 +72,14 @@ module ActiveRecord
72
72
  result['data'].flatten
73
73
  end
74
74
 
75
+ def materialized_views(name = nil)
76
+ result = do_system_execute("SHOW TABLES WHERE engine = 'MaterializedView'", name)
77
+ return [] if result.nil?
78
+ result['data'].flatten
79
+ end
80
+
75
81
  def functions
76
- result = do_system_execute("SELECT name FROM system.functions WHERE origin = 'SQLUserDefined'")
82
+ result = do_system_execute("SELECT name FROM system.functions WHERE origin = 'SQLUserDefined' ORDER BY name")
77
83
  return [] if result.nil?
78
84
  result['data'].flatten
79
85
  end
@@ -102,7 +102,7 @@ module ActiveRecord
102
102
  private
103
103
 
104
104
  def valid_column_definition_options
105
- super + [:array, :low_cardinality, :fixed_string, :value, :type, :map, :codec]
105
+ super + [:array, :low_cardinality, :fixed_string, :value, :type, :map, :codec, :unsigned]
106
106
  end
107
107
  end
108
108
 
@@ -191,6 +191,8 @@ module ActiveRecord
191
191
  nil
192
192
  when /(Nullable)?\(?U?Int64\)?/
193
193
  8
194
+ when /(Nullable)?\(?U?Int128\)?/
195
+ 16
194
196
  else
195
197
  super
196
198
  end
@@ -312,7 +314,7 @@ module ActiveRecord
312
314
  end
313
315
  end
314
316
 
315
- def create_view(table_name, **options)
317
+ def create_view(table_name, request_settings: {}, **options)
316
318
  options.merge!(view: true)
317
319
  options = apply_replica(table_name, options)
318
320
  td = create_table_definition(apply_cluster(table_name), **options)
@@ -322,10 +324,10 @@ module ActiveRecord
322
324
  drop_table(table_name, options.merge(if_exists: true))
323
325
  end
324
326
 
325
- do_execute(schema_creation.accept(td), format: nil)
327
+ do_execute(schema_creation.accept(td), format: nil, settings: request_settings)
326
328
  end
327
329
 
328
- def create_table(table_name, **options, &block)
330
+ def create_table(table_name, request_settings: {}, **options, &block)
329
331
  options = apply_replica(table_name, options)
330
332
  td = create_table_definition(apply_cluster(table_name), **options)
331
333
  block.call td if block_given?
@@ -339,7 +341,7 @@ module ActiveRecord
339
341
  drop_table(table_name, options.merge(if_exists: true))
340
342
  end
341
343
 
342
- do_execute(schema_creation.accept(td), format: nil)
344
+ do_execute(schema_creation.accept(td), format: nil, settings: request_settings)
343
345
 
344
346
  if options[:with_distributed]
345
347
  distributed_table_name = options.delete(:with_distributed)
@@ -15,13 +15,16 @@ module ClickhouseActiverecord
15
15
  private
16
16
 
17
17
  def tables(stream)
18
- functions = @connection.functions
18
+ functions = @connection.functions.sort
19
19
  functions.each do |function|
20
20
  function(function, stream)
21
21
  end
22
22
 
23
- sorted_tables = @connection.tables.sort {|a,b| @connection.show_create_table(a).match(/^CREATE\s+(MATERIALIZED\s+)?VIEW/) ? 1 : a <=> b }
24
- sorted_tables.each do |table_name|
23
+ view_tables = @connection.views.sort
24
+ materialized_view_tables = @connection.materialized_views.sort
25
+ sorted_tables = @connection.tables.sort - view_tables - materialized_view_tables
26
+
27
+ (sorted_tables + view_tables + materialized_view_tables).each do |table_name|
25
28
  table(table_name, stream) unless ignored?(table_name)
26
29
  end
27
30
  end
@@ -109,6 +112,15 @@ module ClickhouseActiverecord
109
112
  end
110
113
  end
111
114
 
115
+ def column_spec_for_primary_key(column)
116
+ spec = super
117
+
118
+ id = ActiveRecord::ConnectionAdapters::ClickhouseAdapter::NATIVE_DATABASE_TYPES.invert[{name: column.sql_type.gsub(/\(\d+\)/, "")}]
119
+ spec[:id] = id.inspect if id.present?
120
+
121
+ spec.except!(:limit, :unsigned) # This can be removed at some date, it is only here to clean up existing schemas which have dumped these values already
122
+ end
123
+
112
124
  def function(function, stream)
113
125
  stream.puts " # FUNCTION: #{function}"
114
126
  sql = @connection.show_create_function(function)
@@ -47,12 +47,12 @@ module ClickhouseActiverecord
47
47
  tables.sort_by! {|table| table.match(/^CREATE\s+(MATERIALIZED\s+)?VIEW/) ? 1 : 0}
48
48
 
49
49
  # get all functions
50
- functions = connection.execute("SELECT create_query FROM system.functions WHERE origin = 'SQLUserDefined'")['data'].flatten
50
+ functions = connection.execute("SELECT create_query FROM system.functions WHERE origin = 'SQLUserDefined' ORDER BY name")['data'].flatten
51
51
 
52
52
  # put to file
53
53
  File.open(args.first, 'w:utf-8') do |file|
54
54
  functions.each do |function|
55
- file.puts function + ";\n\n"
55
+ file.puts function.gsub('\\n', "\n") + ";\n\n"
56
56
  end
57
57
 
58
58
  tables.each do |table|
@@ -1,3 +1,3 @@
1
1
  module ClickhouseActiverecord
2
- VERSION = '1.2.0'
2
+ VERSION = '1.2.1'
3
3
  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: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Odintsov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-23 00:00:00.000000000 Z
11
+ date: 2024-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler