Skip to content

Commit

Permalink
fix internal metadata for activerecord 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
PNixx committed Aug 19, 2024
1 parent c16a9c4 commit 1454a79
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion clickhouse-activerecord.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
8 changes: 7 additions & 1 deletion lib/clickhouse-activerecord/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 0 additions & 19 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@ def dump(connection = ActiveRecord::Base.connection, stream = STDOUT, config = A

private

def header(stream)
stream.puts <<HEADER
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `rails
# #{simple ? 'db' : 'clickhouse'}:schema:load`. When creating a new database, `rails #{simple ? 'db' : 'clickhouse'}:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
#{simple ? 'ActiveRecord' : 'ClickhouseActiverecord'}::Schema.define(#{define_params}) do
HEADER
end

def tables(stream)
functions = @connection.functions
functions.each do |function|
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.1.0'
VERSION = '1.1.1'
end
25 changes: 17 additions & 8 deletions lib/core_extensions/active_record/internal_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module InternalMetadata

def create_table
return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter)
return if table_exists? || !enabled?
return if !enabled? || table_exists?

key_options = connection.internal_string_options_for_primary_key
table_options = {
Expand All @@ -31,14 +31,23 @@ def create_table

private

def update_entry(key, new_value)
return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter)

create_entry(key, new_value)
def update_entry(connection_or_key, key_or_new_value, new_value = nil)
if ::ActiveRecord::version >= 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/
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/core_extensions/active_record/schema_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def versions

def connection
if ::ActiveRecord::version >= Gem::Version.new('7.2')
@pool.connection
@pool.lease_connection
else
super
end
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 @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -41,7 +42,6 @@
database: ENV['CLICKHOUSE_DATABASE'] || 'test',
username: nil,
password: nil,
use_metadata_table: false,
cluster_name: ENV['CLICKHOUSE_CLUSTER'],
}
)
Expand Down

0 comments on commit 1454a79

Please sign in to comment.