Skip to content

Commit

Permalink
support update & delete for clickhouse from version 23.3 and newer #93
Browse files Browse the repository at this point in the history
  • Loading branch information
PNixx committed Jan 10, 2024
1 parent cc3ecfd commit 3b5f651
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ def visit_ChangeColumnDefinition(o)
end

def current_database
if ActiveRecord::version >= Gem::Version.new('6.1')
ActiveRecord::Base.connection_db_config.database
else
ActiveRecord::Base.connection_config[:database]
end
ActiveRecord::Base.connection_db_config.database
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ def exec_insert_all(sql, name)
true
end

# @link https://clickhouse.com/docs/en/sql-reference/statements/alter/update
def exec_update(_sql, _name = nil, _binds = [])
raise ActiveRecord::ActiveRecordError, 'Clickhouse update is not supported'
do_execute(_sql, _name, format: nil)
true
end

# @link https://clickhouse.com/docs/en/sql-reference/statements/delete
def exec_delete(_sql, _name = nil, _binds = [])
raise ActiveRecord::ActiveRecordError, 'Clickhouse delete is not supported'
do_execute(_sql, _name, format: nil)
true
end

def tables(name = nil)
Expand Down Expand Up @@ -143,11 +147,7 @@ def new_column_from_field(table_name, field, _definitions)
default = field[3]
default_value = extract_value_from_default(default)
default_function = extract_default_function(default_value, default)
if ActiveRecord::version >= Gem::Version.new('6')
ClickhouseColumn.new(field[0], default_value, type_metadata, field[1].include?('Nullable'), default_function)
else
ClickhouseColumn.new(field[0], default_value, type_metadata, field[1].include?('Nullable'), table_name, default_function)
end
ClickhouseColumn.new(field[0], default_value, type_metadata, field[1].include?('Nullable'), default_function)
end

protected
Expand Down
21 changes: 3 additions & 18 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ def initialize(logger, connection_parameters, config, full_config)
@full_config = full_config

@prepared_statements = false
if ActiveRecord::version == Gem::Version.new('6.0.0')
@prepared_statement_status = Concurrent::ThreadLocalVar.new(false)
end

connect
end
Expand Down Expand Up @@ -238,30 +235,18 @@ def quote(value)
# Quoting time without microseconds
def quoted_date(value)
if value.acts_like?(:time)
if ActiveRecord::version >= Gem::Version.new('7')
zone_conversion_method = ActiveRecord.default_timezone == :utc ? :getutc : :getlocal
else
zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
end
zone_conversion_method = ActiveRecord.default_timezone == :utc ? :getutc : :getlocal

if value.respond_to?(zone_conversion_method)
value = value.send(zone_conversion_method)
end
end

if ActiveRecord::version >= Gem::Version.new('7')
value.to_fs(:db)
else
value.to_s(:db)
end
value.to_fs(:db)
end

def column_name_for_operation(operation, node) # :nodoc:
if ActiveRecord::version >= Gem::Version.new('6')
visitor.compile(node)
else
column_name_from_arel_node(node)
end
visitor.compile(node)
end

# Executes insert +sql+ statement in the context of this connection using
Expand Down
11 changes: 11 additions & 0 deletions lib/arel/visitors/clickhouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ def visit_Arel_Nodes_SelectOptions(o, collector)
maybe_visit o.settings, super
end

def visit_Arel_Nodes_UpdateStatement(o, collector)
o = prepare_update_statement(o)

collector << 'ALTER TABLE '
collector = visit o.relation, collector
collect_nodes_for o.values, collector, ' UPDATE '
collect_nodes_for o.wheres, collector, ' WHERE ', ' AND '
collect_nodes_for o.orders, collector, ' ORDER BY '
maybe_visit o.limit, collector
end

def visit_Arel_Nodes_Settings(o, collector)
return collector if o.expr.empty?

Expand Down
2 changes: 1 addition & 1 deletion lib/clickhouse-activerecord/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ClickhouseActiverecord
VERSION = '1.0.0'
VERSION = '1.0.1'
end
4 changes: 2 additions & 2 deletions lib/tasks/clickhouse.rake
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ namespace :clickhouse do
desc 'Migrate the clickhouse database'
task migrate: %i[prepare_schema_migration_table prepare_internal_metadata_table] do
Rake::Task['db:migrate:clickhouse'].execute
if File.exists? "#{Rails.root}/db/clickhouse_schema_simple.rb"
if File.exist? "#{Rails.root}/db/clickhouse_schema_simple.rb"
Rake::Task['clickhouse:schema:dump'].execute(simple: true)
end
end

desc 'Rollback the clickhouse database'
task rollback: %i[prepare_schema_migration_table prepare_internal_metadata_table] do
Rake::Task['db:rollback:clickhouse'].execute
if File.exists? "#{Rails.root}/db/clickhouse_schema_simple.rb"
if File.exist? "#{Rails.root}/db/clickhouse_schema_simple.rb"
Rake::Task['clickhouse:schema:dump'].execute(simple: true)
end
end
Expand Down

0 comments on commit 3b5f651

Please sign in to comment.