Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PF: Add helper method to check if index is up to date #44

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/mysql_framework/scripts/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ 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)
florianpilz marked this conversation as resolved.
Show resolved Hide resolved
SHOW INDEX FROM #{table_name} WHERE Key_name="#{index_name}" ORDER BY Seq_in_index;
SQL

return false if result.size != columns.size

result.each_with_index do |column, i|
return false if column[:Column_name] != columns[i]
end

true
end

protected

def generate_partition_sql
Expand Down
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: 'a' }, { Column_name: 'b' }]
)
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
Loading