Skip to content

Commit

Permalink
Integrate updates into rebase.
Browse files Browse the repository at this point in the history
* Restore support for table/database creation with request settings.
* Move improved "DB::Exception" handling into ResponseProcessor.
  • Loading branch information
leboshi committed Nov 22, 2024
1 parent 90082f3 commit 7fb1a7f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module ActiveRecord
module ConnectionAdapters
module Clickhouse
module SchemaStatements
DB_EXCEPTION_REGEXP = /\ACode:\s+\d+\.\s+DB::Exception:/.freeze

def with_settings(**settings)
@block_settings ||= {}
Expand Down Expand Up @@ -129,7 +128,9 @@ def functions
end

def show_create_function(function)
execute("SELECT create_query FROM system.functions WHERE origin = 'SQLUserDefined' AND name = '#{function}'")
result = do_system_execute("SELECT create_query FROM system.functions WHERE origin = 'SQLUserDefined' AND name = '#{function}'")
return if result.nil?
result['data'].flatten.first
end

def table_options(table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ module Clickhouse
class Statement
class ResponseProcessor

DB_EXCEPTION_REGEXP = /\ACode:\s+\d+\.\s+DB::Exception:/.freeze

def initialize(raw_response, format)
@raw_response = raw_response
@body = raw_response.body
@format = format
end

Expand All @@ -18,7 +21,7 @@ def process
raise_database_error!
end
rescue JSON::ParserError
@raw_response.body
@body
end

private
Expand All @@ -28,29 +31,28 @@ def success?
end

def process_successful_response
raise_generic! if @raw_response.body.to_s.include?('DB::Exception')
raise_generic! if @body.include?('DB::Exception') && @body.match?(DB_EXCEPTION_REGEXP)

format_body_response
end

def raise_generic!
raise ActiveRecord::ActiveRecordError, "Response code: #{@raw_response.code}:\n#{@raw_response.body}"
raise ActiveRecord::ActiveRecordError, "Response code: #{@raw_response.code}:\n#{@body}"
end

def format_body_response
body = @raw_response.body
return body if body.blank?
return @body if @body.blank?

case @format
when 'JSONCompact'
format_from_json_compact(body)
format_from_json_compact(@body)
when 'JSONCompactEachRowWithNamesAndTypes'
format_from_json_compact_each_row_with_names_and_types(body)
format_from_json_compact_each_row_with_names_and_types(@body)
else
body
@body
end
rescue JSON::ParserError
@raw_response.body
@body
end

def format_from_json_compact(body)
Expand Down Expand Up @@ -79,7 +81,7 @@ def parse_json_payload(payload)
end

def raise_database_error!
case @raw_response.body
case @body
when /DB::Exception:.*\(UNKNOWN_DATABASE\)/
raise ActiveRecord::NoDatabaseError
when /DB::Exception:.*\(DATABASE_ALREADY_EXISTS\)/
Expand Down
5 changes: 2 additions & 3 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
require 'active_record/connection_adapters/clickhouse/oid/map'
require 'active_record/connection_adapters/clickhouse/oid/uuid'
require 'active_record/connection_adapters/clickhouse/column'
require 'active_record/connection_adapters/clickhouse/format_manager'
require 'active_record/connection_adapters/clickhouse/quoting'
require 'active_record/connection_adapters/clickhouse/schema_creation'
require 'active_record/connection_adapters/clickhouse/schema_statements'
Expand Down Expand Up @@ -326,7 +325,7 @@ def create_view(table_name, request_settings: {}, **options)
drop_table(table_name, options.merge(if_exists: true))
end

execute(schema_creation.accept(td))
execute(schema_creation.accept(td), settings: request_settings)
end

def create_table(table_name, request_settings: {}, **options, &block)
Expand All @@ -343,7 +342,7 @@ def create_table(table_name, request_settings: {}, **options, &block)
drop_table(table_name, options.merge(if_exists: true))
end

execute(schema_creation.accept(td))
execute(schema_creation.accept(td), settings: request_settings)

if options[:with_distributed]
distributed_table_name = options.delete(:with_distributed)
Expand Down
2 changes: 1 addition & 1 deletion spec/cluster/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
let(:directory) { 'dsl_create_function' }

it 'creates a function' do
ActiveRecord::Base.connection.do_execute('CREATE FUNCTION forced_fun AS (x, k, b) -> k*x + b', format: nil)
ActiveRecord::Base.connection.execute('CREATE FUNCTION forced_fun AS (x, k, b) -> k*x + b')

subject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def up
sql = <<~SQL
CREATE FUNCTION multFun AS (x,y) -> x * y
SQL
do_execute(sql, format: nil)
execute(sql)

sql = <<~SQL
CREATE FUNCTION addFun AS (x,y) -> x + y
Expand Down
2 changes: 1 addition & 1 deletion spec/single/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@
context 'dsl' do
let(:directory) { 'dsl_create_function' }
it 'creates a function' do
ActiveRecord::Base.connection.do_execute('CREATE FUNCTION forced_fun AS (x, k, b) -> k*x + b', format: nil)
ActiveRecord::Base.connection.execute('CREATE FUNCTION forced_fun AS (x, k, b) -> k*x + b')

subject

Expand Down

0 comments on commit 7fb1a7f

Please sign in to comment.