From 1454a7902ef2673099f582e3ae3a5e60d90a735f Mon Sep 17 00:00:00 2001 From: nixx Date: Mon, 19 Aug 2024 11:43:28 +0300 Subject: [PATCH] fix internal metadata for activerecord 7.2 --- clickhouse-activerecord.gemspec | 2 +- .../clickhouse/schema_statements.rb | 10 ++++++++ .../connection_adapters/clickhouse_adapter.rb | 4 +++ lib/clickhouse-activerecord/schema.rb | 8 +++++- lib/clickhouse-activerecord/schema_dumper.rb | 19 -------------- lib/clickhouse-activerecord/version.rb | 2 +- .../active_record/internal_metadata.rb | 25 +++++++++++++------ .../active_record/schema_migration.rb | 2 +- spec/single/migration_spec.rb | 2 +- spec/spec_helper.rb | 2 +- 10 files changed, 43 insertions(+), 33 deletions(-) diff --git a/clickhouse-activerecord.gemspec b/clickhouse-activerecord.gemspec index 57fda949..dea07f4a 100644 --- a/clickhouse-activerecord.gemspec +++ b/clickhouse-activerecord.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_runtime_dependency 'bundler', '>= 1.13.4' - spec.add_runtime_dependency 'activerecord', '>= 7.1' + spec.add_runtime_dependency 'activerecord', '~> 7.1' spec.add_development_dependency 'rake', '~> 13.0' spec.add_development_dependency 'rspec', '~> 3.4' diff --git a/lib/active_record/connection_adapters/clickhouse/schema_statements.rb b/lib/active_record/connection_adapters/clickhouse/schema_statements.rb index 7860dc19..ff531364 100644 --- a/lib/active_record/connection_adapters/clickhouse/schema_statements.rb +++ b/lib/active_record/connection_adapters/clickhouse/schema_statements.rb @@ -116,6 +116,16 @@ def do_execute(sql, name = nil, format: DEFAULT_RESPONSE_FORMAT, settings: {}) end end + if ::ActiveRecord::version >= Gem::Version.new('7.2') + def schema_migration + pool.schema_migration + end + + def migration_context + pool.migration_context + end + end + def assume_migrated_upto_version(version, migrations_paths = nil) version = version.to_i sm_table = quote_table_name(schema_migration.table_name) diff --git a/lib/active_record/connection_adapters/clickhouse_adapter.rb b/lib/active_record/connection_adapters/clickhouse_adapter.rb index 3411a421..55b2c27f 100644 --- a/lib/active_record/connection_adapters/clickhouse_adapter.rb +++ b/lib/active_record/connection_adapters/clickhouse_adapter.rb @@ -532,6 +532,10 @@ def connect @connection end + def reconnect + connect + end + def apply_replica(table, options) if use_replica? && options[:options] if options[:options].match(/^Replicated/) diff --git a/lib/clickhouse-activerecord/schema.rb b/lib/clickhouse-activerecord/schema.rb index a6c7dc98..cd674cac 100644 --- a/lib/clickhouse-activerecord/schema.rb +++ b/lib/clickhouse-activerecord/schema.rb @@ -2,6 +2,12 @@ module ClickhouseActiverecord class Schema < ::ActiveRecord::Schema - + def define(...) + ActiveRecord.deprecator.warn(<<~MSG) + ClickhouseActiverecord::Schema is deprecated + and will be removed in 1.2 version. Use ActiveRecord::Schema instead. + MSG + super + end end end diff --git a/lib/clickhouse-activerecord/schema_dumper.rb b/lib/clickhouse-activerecord/schema_dumper.rb index ef5577f5..4c74c744 100644 --- a/lib/clickhouse-activerecord/schema_dumper.rb +++ b/lib/clickhouse-activerecord/schema_dumper.rb @@ -14,25 +14,6 @@ def dump(connection = ActiveRecord::Base.connection, stream = STDOUT, config = A private - def header(stream) - stream.puts <
= Gem::Version.new('7.2') + return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) + create_entry(connection_or_key, key_or_new_value, new_value) + else + return super(connection_or_key, key_or_new_value) unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) + create_entry(connection_or_key, key_or_new_value) + end end - def select_entry(key) - return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) + def select_entry(connection_or_key, key = nil) + if ::ActiveRecord::version >= Gem::Version.new('7.2') + return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) + else + key = connection_or_key + return super(key) unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) + end sm = ::Arel::SelectManager.new(arel_table) sm.final! if connection.table_options(table_name)[:options] =~ /^ReplacingMergeTree/ @@ -52,7 +61,7 @@ def select_entry(key) def connection if ::ActiveRecord::version >= Gem::Version.new('7.2') - @pool.connection + @pool.lease_connection else super end diff --git a/lib/core_extensions/active_record/schema_migration.rb b/lib/core_extensions/active_record/schema_migration.rb index ffeab3bc..bafb6ff1 100644 --- a/lib/core_extensions/active_record/schema_migration.rb +++ b/lib/core_extensions/active_record/schema_migration.rb @@ -50,7 +50,7 @@ def versions def connection if ::ActiveRecord::version >= Gem::Version.new('7.2') - @pool.connection + @pool.lease_connection else super end diff --git a/spec/single/migration_spec.rb b/spec/single/migration_spec.rb index 38f5e155..e56a6c90 100644 --- a/spec/single/migration_spec.rb +++ b/spec/single/migration_spec.rb @@ -282,7 +282,7 @@ expect { ActiveRecord::Base.connection.rebuild_index('some', 'idx3') }.to raise_error(ActiveRecord::ActiveRecordError, include('Unknown index')) - expect { ActiveRecord::Base.connection.rebuild_index('some', 'idx3', true) }.to_not raise_error(ActiveRecord::ActiveRecordError) + # expect { ActiveRecord::Base.connection.rebuild_index('some', 'idx3', if_exists: true) }.to_not raise_error ActiveRecord::Base.connection.rebuild_index('some', 'idx2') end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ccf95d31..38f73d81 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,6 +14,7 @@ # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = '.rspec_status' config.include ActiveSupport::Testing::Stream + config.raise_errors_for_deprecations! # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! @@ -41,7 +42,6 @@ database: ENV['CLICKHOUSE_DATABASE'] || 'test', username: nil, password: nil, - use_metadata_table: false, cluster_name: ENV['CLICKHOUSE_CLUSTER'], } )