Skip to content

Commit

Permalink
feature: ability to rename cte
Browse files Browse the repository at this point in the history
  • Loading branch information
TamarinEA committed Jan 26, 2021
1 parent 333d471 commit 6cd4bec
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/sphinx/integration/extensions/thinking_sphinx/index/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ def delete_withs(*names)
end
end

# Переименовывает CTE
#
# old_name - String, название сохранённого CTE
# new_name - String, новое название CTE
#
# При перемещении новое CTE (если оно не было сохранено заранее) попадёт в конец списка CTE.
# Можно указать то же название CTE, что переместит блок в конец списка CTE.
#
# Returns nothing
def rename_with(old_name, new_name)
with_block = @index.local_options[:source_cte].delete(old_name)
@index.local_options[:source_cte][new_name] = with_block
end

# Формирует LEFT JOIN
#
# name - Symbol or Hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,42 @@
it { expect(index.local_options[:source_cte][:_rubrics2]).to eq "select id from rubrics2" }
end

describe 'rename with' do
context 'when different new name' do
let(:index) do
ThinkingSphinx::Index::Builder.generate(ModelWithDisk) do
with(:_rubrics1) { "select id from rubrics1" }
with(:_rubrics2) { "select id from rubrics2" }

rename_with(:_rubrics1, :_rubrics3)
end
end

it do
expect(index.local_options[:source_cte].size).to eq 2
expect(index.local_options[:source_cte][:_rubrics3]).to eq "select id from rubrics1"
expect(index.local_options[:source_cte].keys).to eq [:_rubrics2, :_rubrics3]
end
end

context 'when same new name' do
let(:index) do
ThinkingSphinx::Index::Builder.generate(ModelWithDisk) do
with(:_rubrics1) { "select id from rubrics1" }
with(:_rubrics2) { "select id from rubrics2" }

rename_with(:_rubrics1, :_rubrics1)
end
end

it do
expect(index.local_options[:source_cte].size).to eq 2
expect(index.local_options[:source_cte][:_rubrics1]).to eq "select id from rubrics1"
expect(index.local_options[:source_cte].keys).to eq [:_rubrics2, :_rubrics1]
end
end
end

describe 'composite_index' do
let(:index) do
ThinkingSphinx::Index::Builder.generate(ModelWithDisk, nil) do
Expand Down

0 comments on commit 6cd4bec

Please sign in to comment.