Skip to content

Commit

Permalink
Merge pull request #44 from Sage/add-index-up-to-date-helper
Browse files Browse the repository at this point in the history
PF: Add helper method to check if index is up to date
  • Loading branch information
florianpilz authored Aug 28, 2023
2 parents 07f3157 + cb241d7 commit c69045f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/mysql_framework/scripts/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ def index_exists?(client, table_name, index_name)
result.count >= 1
end

def index_up_to_date?(client, table_name, index_name, columns)
result = client.query(<<~SQL)
SHOW INDEX FROM #{table_name} WHERE Key_name="#{index_name}";
SQL

return false if result.size != columns.size

index_columns = result.sort_by { |column| column[:Seq_in_index] }
index_columns.each_with_index do |column, i|
return false if column[:Column_name] != columns[i]
end

true
end

protected

def generate_partition_sql
Expand Down
2 changes: 1 addition & 1 deletion lib/mysql_framework/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MysqlFramework
VERSION = '2.1.5'
VERSION = '2.1.6'
end
24 changes: 24 additions & 0 deletions spec/lib/mysql_framework/scripts/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,28 @@
expect(subject.index_exists?(client,'foo','bar')).to eq(false)
end
end

describe '#index_up_to_date?' do
before do
expect(client).to receive(:query).and_return(
[{ Column_name: 'b', Seq_in_index: 2 }, { Column_name: 'a', Seq_in_index: 1 }]
)
end

it 'returns true when index is up to date' do
expect(subject.index_up_to_date?(client, 'foo', 'bar', ['a', 'b'])).to eq(true)
end

it 'returns false when index is missing a column' do
expect(subject.index_up_to_date?(client, 'foo', 'bar', ['a', 'b', 'c'])).to eq(false)
end

it 'returns false when index is having too many columns' do
expect(subject.index_up_to_date?(client, 'foo', 'bar', ['a'])).to eq(false)
end

it 'returns false when index has columns in wrong order' do
expect(subject.index_up_to_date?(client, 'foo', 'bar', ['b', 'a'])).to eq(false)
end
end
end

0 comments on commit c69045f

Please sign in to comment.