Skip to content

Commit

Permalink
published 0.5.14 - settings for long request in the migration
Browse files Browse the repository at this point in the history
  • Loading branch information
PNixx committed Feb 21, 2023
1 parent 7c23d5c commit 2d2e8a3
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 17 deletions.
31 changes: 16 additions & 15 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@ def clickhouse_connection(config)
end
end

module ClickhouseRelationReverseOrder
def reverse_order!
return super unless connection.is_a?(ConnectionAdapters::ClickhouseAdapter)

orders = order_values.uniq.compact_blank
return super unless orders.empty? && !primary_key

self.order_values = %w(date created_at).select {|c| column_names.include?(c) }.map{|c| arel_attribute(c).desc }
self
end
end
Relation.prepend(ClickhouseRelationReverseOrder)

module TypeCaster
class Map
def is_view
Expand All @@ -74,7 +61,7 @@ def is_view
end

module ModelSchema
module ClassMethods
module ClassMethods
def is_view
@is_view || false
end
Expand Down Expand Up @@ -352,8 +339,22 @@ def drop_table(table_name, options = {}) # :nodoc:
end
end

def add_column(table_name, column_name, type, **options)
return if options[:if_not_exists] == true && column_exists?(table_name, column_name, type)

at = create_alter_table table_name
at.add_column(column_name, type, **options)
execute(schema_creation.accept(at), nil, settings: {wait_end_of_query: 1, send_progress_in_http_headers: 1})
end

def remove_column(table_name, column_name, type = nil, **options)
return if options[:if_exists] == true && !column_exists?(table_name, column_name)

execute("ALTER TABLE #{quote_table_name(table_name)} #{remove_column_for_alter(table_name, column_name, type, **options)}", nil, settings: {wait_end_of_query: 1, send_progress_in_http_headers: 1})
end

def change_column(table_name, column_name, type, options = {})
result = do_execute "ALTER TABLE #{quote_table_name(table_name)} #{change_column_for_alter(table_name, column_name, type, options)}"
result = do_execute("ALTER TABLE #{quote_table_name(table_name)} #{change_column_for_alter(table_name, column_name, type, options)}", nil, settings: {wait_end_of_query: 1, send_progress_in_http_headers: 1})
raise "Error parse json response: #{result}" if result.presence && !result.is_a?(Hash)
end

Expand Down
6 changes: 5 additions & 1 deletion lib/clickhouse-activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'active_record/connection_adapters/clickhouse_adapter'

require 'core_extensions/active_record/relation'

require_relative '../core_extensions/active_record/migration/command_recorder'
ActiveRecord::Migration::CommandRecorder.include CoreExtensions::ActiveRecord::Migration::CommandRecorder

Expand All @@ -14,5 +16,7 @@
end

module ClickhouseActiverecord

def self.load
ActiveRecord::Relation.prepend(CoreExtensions::ActiveRecord::Relation)
end
end
6 changes: 6 additions & 0 deletions lib/clickhouse-activerecord/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module ClickhouseActiverecord
require 'rails'

class Railtie < Rails::Railtie
initializer "clickhouse.load" do
ActiveSupport.on_load :active_record do
ClickhouseActiverecord.load
end
end

rake_tasks { load 'tasks/clickhouse.rake' }
end
end
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 = '0.5.13'
VERSION = '0.5.14'
end
15 changes: 15 additions & 0 deletions lib/core_extensions/active_record/relation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module CoreExtensions
module ActiveRecord
module Relation
def reverse_order!
return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter)

orders = order_values.uniq.reject(&:blank?)
return super unless orders.empty? && !primary_key

self.order_values = (column_names & %w[date created_at]).map { |c| arel_table[c].desc }
self
end
end
end
end
12 changes: 12 additions & 0 deletions spec/cases/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
}.to raise_error(ActiveRecord::ActiveRecordError, 'Clickhouse delete is not supported')
end
end

describe '#reverse_order!' do
it 'blank' do
expect(model.all.reverse_order!.map(&:event_name)).to eq([])
end

it 'select' do
model.create!(event_name: 'some event 1', date: 1.day.ago)
model.create!(event_name: 'some event 2', date: 2.day.ago)
expect(model.all.reverse_order!.map(&:event_name)).to eq(['some event 1', 'some event 2'])
end
end
end

context 'array' do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require 'clickhouse-activerecord'
require 'active_support/testing/stream'

ClickhouseActiverecord.load

FIXTURES_PATH = File.join(File.dirname(__FILE__), 'fixtures')
CLUSTER_NAME = 'test'

Expand Down

0 comments on commit 2d2e8a3

Please sign in to comment.