From 2d2e8a39e9dbc2c88b084aa7d5e7606d17dd8690 Mon Sep 17 00:00:00 2001 From: nixx Date: Tue, 21 Feb 2023 10:40:41 +0300 Subject: [PATCH] published 0.5.14 - settings for long request in the migration --- .../connection_adapters/clickhouse_adapter.rb | 31 ++++++++++--------- lib/clickhouse-activerecord.rb | 6 +++- lib/clickhouse-activerecord/railtie.rb | 6 ++++ lib/clickhouse-activerecord/version.rb | 2 +- lib/core_extensions/active_record/relation.rb | 15 +++++++++ spec/cases/model_spec.rb | 12 +++++++ spec/spec_helper.rb | 2 ++ 7 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 lib/core_extensions/active_record/relation.rb diff --git a/lib/active_record/connection_adapters/clickhouse_adapter.rb b/lib/active_record/connection_adapters/clickhouse_adapter.rb index f61273ab..52811247 100644 --- a/lib/active_record/connection_adapters/clickhouse_adapter.rb +++ b/lib/active_record/connection_adapters/clickhouse_adapter.rb @@ -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 @@ -74,7 +61,7 @@ def is_view end module ModelSchema - module ClassMethods + module ClassMethods def is_view @is_view || false end @@ -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 diff --git a/lib/clickhouse-activerecord.rb b/lib/clickhouse-activerecord.rb index dd5a927b..e7a64800 100644 --- a/lib/clickhouse-activerecord.rb +++ b/lib/clickhouse-activerecord.rb @@ -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 @@ -14,5 +16,7 @@ end module ClickhouseActiverecord - + def self.load + ActiveRecord::Relation.prepend(CoreExtensions::ActiveRecord::Relation) + end end diff --git a/lib/clickhouse-activerecord/railtie.rb b/lib/clickhouse-activerecord/railtie.rb index 34fc8db8..c562182a 100644 --- a/lib/clickhouse-activerecord/railtie.rb +++ b/lib/clickhouse-activerecord/railtie.rb @@ -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 diff --git a/lib/clickhouse-activerecord/version.rb b/lib/clickhouse-activerecord/version.rb index 42136356..5b33f3e4 100644 --- a/lib/clickhouse-activerecord/version.rb +++ b/lib/clickhouse-activerecord/version.rb @@ -1,3 +1,3 @@ module ClickhouseActiverecord - VERSION = '0.5.13' + VERSION = '0.5.14' end diff --git a/lib/core_extensions/active_record/relation.rb b/lib/core_extensions/active_record/relation.rb new file mode 100644 index 00000000..f936005e --- /dev/null +++ b/lib/core_extensions/active_record/relation.rb @@ -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 diff --git a/spec/cases/model_spec.rb b/spec/cases/model_spec.rb index f70ce01d..79e8dc81 100644 --- a/spec/cases/model_spec.rb +++ b/spec/cases/model_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 34fb9b83..0e6170b0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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'