clickhouse-activerecord 0.4.3 → 0.4.4

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: b584e48174f94997dc9dd04d3ffde3ff7c4b24535117c565e580c646f2f2581c
4
- data.tar.gz: 9e7c01a185d23ac6e5ac16da635b118a59e7a7c7cdf86ce1c28e92aebacd3baa
3
+ metadata.gz: 1cf76e043406ab83146104d257da665657dc9905b6517ea02956f9034ae52ebf
4
+ data.tar.gz: 21da146747adec65a6e61bd09dc1ded2be7fd025ecea23736d0985708bafa70b
5
5
  SHA512:
6
- metadata.gz: 8b3132522dbc7abaffa180049e350d4ceea82863ab3c208611c4b3227e6ebece1f706262daebec562bd13822fdcd31d44e6321ec97083543484e238e3e07ed2f
7
- data.tar.gz: 44f3c58ff30e78b4e83066fbf9609bd656bf9c3e64f56a6113713068b98393c680ec949b5d8832da533ea6cc12216584a9fcf7b34279efc8c499ec9ff376a166
6
+ metadata.gz: bee84efbe4e210107575a1121a4adc8df4da2f85349abfec2f27c964917a7655438b01cceed36d83fe49f2f70e8f0d8ba32f7e92bb26b5ac5beceddf5243e626
7
+ data.tar.gz: bb1576674cbec042f2f2d949a6e35e8ce4ce22615a0bb0aac2d0ee56b35e200cdb28d72a31010e1143c95b47d397821568e709aa6fdd3c0ee9f8f76605de356c
@@ -1,4 +1,4 @@
1
- ### Version 0.4.0 (Sep 18, 2020)
1
+ ### Version 0.4.4 (Sep 23, 2020)
2
2
 
3
3
  * Full support migration and rollback database
4
4
  * Support cluster and replica. Auto inject to SQL queries.
@@ -6,15 +6,17 @@ module ActiveRecord
6
6
  class SchemaCreation < AbstractAdapter::SchemaCreation# :nodoc:
7
7
 
8
8
  def visit_AddColumnDefinition(o)
9
- +"ADD COLUMN #{accept(o.column)}"
9
+ sql = +"ADD COLUMN #{accept(o.column)}"
10
+ sql << " AFTER " + quote_column_name(o.column.options[:after]) if o.column.options.key?(:after)
11
+ sql
10
12
  end
11
13
 
12
14
  def add_column_options!(sql, options)
13
- sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
14
15
  if options[:null] || options[:null].nil?
15
16
  sql.gsub!(/\s+(.*)/, ' Nullable(\1)')
16
17
  end
17
18
  sql.gsub!(/(\sString)\(\d+\)/, '\1')
19
+ sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
18
20
  sql
19
21
  end
20
22
 
@@ -47,6 +49,25 @@ module ActiveRecord
47
49
  " TEMPORARY" if o.temporary
48
50
  " MATERIALIZED" if o.materialized
49
51
  end
52
+
53
+ def visit_ChangeColumnDefinition(o)
54
+ column = o.column
55
+ column.sql_type = type_to_sql(column.type, column.options)
56
+ options = column_options(column)
57
+
58
+ quoted_column_name = quote_column_name(o.name)
59
+ type = column.sql_type
60
+ type = "Nullable(#{type})" if options[:null]
61
+ change_column_sql = +"MODIFY COLUMN #{quoted_column_name} #{type}"
62
+
63
+ if options.key?(:default)
64
+ quoted_default = quote_default_expression(options[:default], column)
65
+ change_column_sql << " DEFAULT #{quoted_default}"
66
+ end
67
+
68
+ change_column_sql
69
+ end
70
+
50
71
  end
51
72
  end
52
73
  end
@@ -28,7 +28,7 @@ module ActiveRecord
28
28
  @if_not_exists = if_not_exists
29
29
  @options = options
30
30
  @as = as
31
- @name = @conn.apply_cluster(name)
31
+ @name = name
32
32
  @comment = comment
33
33
  @view = view || materialized
34
34
  @materialized = materialized
@@ -205,7 +205,7 @@ module ActiveRecord
205
205
  def create_database(name)
206
206
  sql = apply_cluster "CREATE DATABASE #{quote_table_name(name)}"
207
207
  log_with_debug(sql, adapter_name) do
208
- res = @connection.post("/?#{@config.except(:database).to_param}", "CREATE DATABASE #{quote_table_name(name)}")
208
+ res = @connection.post("/?#{@config.except(:database).to_param}", sql)
209
209
  process_response(res)
210
210
  end
211
211
  end
@@ -213,7 +213,7 @@ module ActiveRecord
213
213
  def create_view(table_name, **options)
214
214
  options.merge!(view: true)
215
215
  options = apply_replica(table_name, options)
216
- td = create_table_definition(table_name, options)
216
+ td = create_table_definition(apply_cluster(table_name), options)
217
217
  yield td if block_given?
218
218
 
219
219
  if options[:force]
@@ -225,7 +225,7 @@ module ActiveRecord
225
225
 
226
226
  def create_table(table_name, **options)
227
227
  options = apply_replica(table_name, options)
228
- td = create_table_definition(table_name, options)
228
+ td = create_table_definition(apply_cluster(table_name), options)
229
229
  yield td if block_given?
230
230
 
231
231
  if options[:force]
@@ -252,6 +252,21 @@ module ActiveRecord
252
252
  do_execute apply_cluster "DROP TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}"
253
253
  end
254
254
 
255
+ def change_column(table_name, column_name, type, options = {})
256
+ result = do_execute "ALTER TABLE #{quote_table_name(table_name)} #{change_column_for_alter(table_name, column_name, type, options)}"
257
+ raise "Error parse json response: #{result}" if result.presence && !result.is_a?(Hash)
258
+ end
259
+
260
+ def change_column_null(table_name, column_name, null, default = nil)
261
+ structure = table_structure(table_name).select{|v| v[0] == column_name.to_s}.first
262
+ raise "Column #{column_name} not found in table #{table_name}" if structure.nil?
263
+ change_column table_name, column_name, structure[1].gsub(/(Nullable\()?(.*?)\)?/, '\2'), {null: null, default: default}.compact
264
+ end
265
+
266
+ def change_column_default(table_name, column_name, default)
267
+ change_column table_name, column_name, nil, {default: default}.compact
268
+ end
269
+
255
270
  def cluster
256
271
  @full_config[:cluster_name]
257
272
  end
@@ -274,6 +289,12 @@ module ActiveRecord
274
289
  result
275
290
  end
276
291
 
292
+ def change_column_for_alter(table_name, column_name, type, options = {})
293
+ td = create_table_definition(table_name)
294
+ cd = td.new_column_definition(column_name, type, options)
295
+ schema_creation.accept(ChangeColumnDefinition.new(cd, column_name))
296
+ end
297
+
277
298
  private
278
299
 
279
300
  def connect
@@ -118,7 +118,7 @@ HEADER
118
118
 
119
119
  def format_colspec(colspec)
120
120
  if simple
121
- super.gsub(/CAST\(([^,]+),.*?\)/, "\\1")
121
+ super.gsub(/CAST\('?([^,']*)'?,\s?'.*?'\)/, "\\1")
122
122
  else
123
123
  super
124
124
  end
@@ -1,3 +1,3 @@
1
1
  module ClickhouseActiverecord
2
- VERSION = '0.4.3'
2
+ VERSION = '0.4.4'
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: 0.4.3
4
+ version: 0.4.4
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-09-22 00:00:00.000000000 Z
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler