Skip to content

Commit 8e66991

Browse files
committed
fix schema dump ignoring indexes with multi-column expressions
1 parent 48c74a6 commit 8e66991

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

lib/clickhouse-activerecord/schema_dumper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def table(table, stream)
9999
end
100100
end
101101

102-
indexes = sql.scan(/INDEX \S+ \S+ TYPE .*? GRANULARITY \d+/)
102+
indexes = sql.scan(/INDEX \S+ .+? TYPE .*? GRANULARITY \d+/)
103103
if indexes.any?
104104
tbl.puts ''
105105
indexes.flatten.map!(&:strip).each do |index|
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class CreateSimpleIndex < ActiveRecord::Migration[7.1]
4+
def up
5+
add_index :some, 'date', name: 'idx4', type: 'minmax', granularity: 1
6+
end
7+
end

spec/single/migration_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,48 @@
311311

312312
ActiveRecord::Base.connection.clear_index('some', 'idx2')
313313
end
314+
315+
it 'schema dumps and restores column indexes' do
316+
require 'clickhouse-activerecord/schema_dumper'
317+
318+
quietly { migration_context.up(4) }
319+
320+
current_schema = StringIO.new
321+
ClickhouseActiverecord::SchemaDumper.dump(ActiveRecord::Base.connection, current_schema)
322+
323+
ActiveRecord::Base.connection.drop_table('some', sync: true)
324+
quietly { eval(current_schema.string) }
325+
326+
expect(ActiveRecord::Base.connection.show_create_table('some')).to include('INDEX idx4 date TYPE minmax GRANULARITY 1')
327+
end
328+
329+
it 'schema dumps and restores multi-column indexes' do
330+
require 'clickhouse-activerecord/schema_dumper'
331+
332+
quietly { migration_context.up(1) }
333+
334+
current_schema = StringIO.new
335+
ClickhouseActiverecord::SchemaDumper.dump(ActiveRecord::Base.connection, current_schema)
336+
337+
ActiveRecord::Base.connection.drop_table('some', sync: true)
338+
quietly { eval(current_schema.string) }
339+
340+
expect(ActiveRecord::Base.connection.show_create_table('some')).to include('INDEX idx (int1 * int2, date) TYPE minmax GRANULARITY 3')
341+
end
342+
343+
it 'schema dumps and restores expression indexes' do
344+
require 'clickhouse-activerecord/schema_dumper'
345+
346+
quietly { migration_context.up(3) }
347+
348+
current_schema = StringIO.new
349+
ClickhouseActiverecord::SchemaDumper.dump(ActiveRecord::Base.connection, current_schema)
350+
351+
ActiveRecord::Base.connection.drop_table('some', sync: true)
352+
quietly { eval(current_schema.string) }
353+
354+
expect(ActiveRecord::Base.connection.show_create_table('some')).to include('INDEX idx2 int1 * int2 TYPE set(10) GRANULARITY 4')
355+
end
314356
end
315357
end
316358
end

0 commit comments

Comments
 (0)