Skip to content

Commit 246775c

Browse files
committed
fix spec
1 parent def2886 commit 246775c

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

lib/active_record/connection_adapters/clickhouse_adapter.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ def initialize(config_or_deprecated_connection, deprecated_logger = nil, depreca
142142
connect
143143
end
144144

145+
# Return ClickHouse server version
146+
def server_version
147+
@server_version ||= do_system_execute('SELECT version()')['data'][0][0]
148+
end
149+
145150
# Savepoints are not supported, noop
146151
def create_savepoint(name)
147152
end
@@ -277,9 +282,11 @@ def column_name_for_operation(operation, node) # :nodoc:
277282
# SCHEMA STATEMENTS ========================================
278283

279284
def primary_keys(table_name)
280-
structure = do_system_execute("SHOW COLUMNS FROM `#{table_name}`")
281-
structure['data'].select {|m| m[3]&.include?('PRI') }.pluck(0)
282-
rescue ActiveRecord::ActiveRecordError => e
285+
if server_version.to_f >= 23.4
286+
structure = do_system_execute("SHOW COLUMNS FROM `#{table_name}`")
287+
return structure['data'].select {|m| m[3]&.include?('PRI') }.pluck(0)
288+
end
289+
283290
pk = table_structure(table_name).first
284291
return ['id'] if pk.present? && pk[0] == 'id'
285292
[]

spec/single/model_spec.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ class Model < ActiveRecord::Base
1010
self.table_name = 'sample'
1111
has_many :joins, class_name: 'ModelJoin', primary_key: 'event_name'
1212
end
13+
class ModelPk < ActiveRecord::Base
14+
self.table_name = 'sample'
15+
self.primary_key = 'event_name'
16+
end
17+
IS_NEW_CLICKHOUSE_SERVER = Model.connection.server_version.to_f >= 23.4
1318

1419
let(:date) { Date.today }
1520

@@ -20,8 +25,10 @@ class Model < ActiveRecord::Base
2025
quietly { ActiveRecord::MigrationContext.new(migrations_dir).up }
2126
end
2227

23-
it 'detect primary key' do
24-
expect(Model.primary_key).to eq('event_name')
28+
if IS_NEW_CLICKHOUSE_SERVER
29+
it "detect primary key" do
30+
expect(Model.primary_key).to eq('event_name')
31+
end
2532
end
2633

2734
describe '#do_execute' do
@@ -80,7 +87,11 @@ class Model < ActiveRecord::Base
8087

8188
it 'update model with primary key' do
8289
expect {
83-
Model.first.update!(event_value: 2)
90+
if IS_NEW_CLICKHOUSE_SERVER
91+
Model.first.update!(event_value: 2)
92+
else
93+
ModelPk.first.update!(event_value: 2)
94+
end
8495
}.to_not raise_error
8596
end
8697
end
@@ -96,7 +107,11 @@ class Model < ActiveRecord::Base
96107

97108
it 'destroy model with primary key' do
98109
expect {
99-
Model.first.destroy!
110+
if IS_NEW_CLICKHOUSE_SERVER
111+
Model.first.destroy!
112+
else
113+
ModelPk.first.destroy!
114+
end
100115
}.to_not raise_error
101116
end
102117
end
@@ -117,8 +132,13 @@ class Model < ActiveRecord::Base
117132
it 'select' do
118133
Model.create!(event_name: 'some event 1', date: 1.day.ago)
119134
Model.create!(event_name: 'some event 2', date: 2.day.ago)
120-
expect(Model.all.reverse_order!.to_sql).to eq('SELECT sample.* FROM sample ORDER BY sample.event_name DESC')
121-
expect(Model.all.reverse_order!.map(&:event_name)).to eq(['some event 2', 'some event 1'])
135+
if IS_NEW_CLICKHOUSE_SERVER
136+
expect(Model.all.reverse_order!.to_sql).to eq('SELECT sample.* FROM sample ORDER BY sample.event_name DESC')
137+
expect(Model.all.reverse_order!.map(&:event_name)).to eq(['some event 2', 'some event 1'])
138+
else
139+
expect(Model.all.reverse_order!.to_sql).to eq('SELECT sample.* FROM sample ORDER BY sample.date DESC')
140+
expect(Model.all.reverse_order!.map(&:event_name)).to eq(['some event 1', 'some event 2'])
141+
end
122142
end
123143
end
124144

0 commit comments

Comments
 (0)