From 68ac0dba16bd17e6debc7d260533dee4981c0489 Mon Sep 17 00:00:00 2001 From: Florian Pilz Date: Fri, 25 Aug 2023 12:23:57 +0200 Subject: [PATCH 1/3] PF: Add helper method to check if index is up to date Working with `mysql_framework` in other projects needed to ship a custom implementation to check if an index is up to date, to decide if it needs to be re-created. This helper method should be provided by the library instead. --- lib/mysql_framework/scripts/base.rb | 14 +++++++++++ spec/lib/mysql_framework/scripts/base_spec.rb | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/mysql_framework/scripts/base.rb b/lib/mysql_framework/scripts/base.rb index eb9412b..fb4da23 100644 --- a/lib/mysql_framework/scripts/base.rb +++ b/lib/mysql_framework/scripts/base.rb @@ -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) + 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 diff --git a/spec/lib/mysql_framework/scripts/base_spec.rb b/spec/lib/mysql_framework/scripts/base_spec.rb index 945e4c3..81d7afc 100644 --- a/spec/lib/mysql_framework/scripts/base_spec.rb +++ b/spec/lib/mysql_framework/scripts/base_spec.rb @@ -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 From 1f30033d9e3afa1903148f0f2be9a393c75b07dc Mon Sep 17 00:00:00 2001 From: Florian Pilz Date: Fri, 25 Aug 2023 12:48:56 +0200 Subject: [PATCH 2/3] PF: Fix sorting as ORDER BY is not allowed in SHOW INDEX query --- lib/mysql_framework/scripts/base.rb | 5 +++-- spec/lib/mysql_framework/scripts/base_spec.rb | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/mysql_framework/scripts/base.rb b/lib/mysql_framework/scripts/base.rb index fb4da23..84f5679 100644 --- a/lib/mysql_framework/scripts/base.rb +++ b/lib/mysql_framework/scripts/base.rb @@ -52,12 +52,13 @@ def index_exists?(client, table_name, index_name) 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}" ORDER BY Seq_in_index; + SHOW INDEX FROM #{table_name} WHERE Key_name="#{index_name}"; SQL return false if result.size != columns.size - result.each_with_index do |column, i| + 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 diff --git a/spec/lib/mysql_framework/scripts/base_spec.rb b/spec/lib/mysql_framework/scripts/base_spec.rb index 81d7afc..126c739 100644 --- a/spec/lib/mysql_framework/scripts/base_spec.rb +++ b/spec/lib/mysql_framework/scripts/base_spec.rb @@ -90,7 +90,7 @@ describe '#index_up_to_date?' do before do expect(client).to receive(:query).and_return( - [{ Column_name: 'a' }, { Column_name: 'b' }] + [{ Column_name: 'b', Seq_in_index: 2 }, { Column_name: 'a', Seq_in_index: 1 }] ) end From cb241d7f443cbd5cf0d62c6c9e8b1febea209706 Mon Sep 17 00:00:00 2001 From: Florian Pilz Date: Fri, 25 Aug 2023 13:22:23 +0200 Subject: [PATCH 3/3] Bump version number --- lib/mysql_framework/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mysql_framework/version.rb b/lib/mysql_framework/version.rb index 347612d..1fb20cb 100644 --- a/lib/mysql_framework/version.rb +++ b/lib/mysql_framework/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MysqlFramework - VERSION = '2.1.5' + VERSION = '2.1.6' end