From 679aabd4c40713aaf9f83d3ea4b34cb15d54c143 Mon Sep 17 00:00:00 2001 From: Miquel Oliete Baliarda Date: Wed, 19 Jun 2024 11:24:36 +0200 Subject: [PATCH] user_exists? method added. --- lib/mysql_framework/scripts/base.rb | 8 ++++++++ lib/mysql_framework/version.rb | 2 +- spec/lib/mysql_framework/scripts/base_spec.rb | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/mysql_framework/scripts/base.rb b/lib/mysql_framework/scripts/base.rb index 84f5679..d8fbe03 100644 --- a/lib/mysql_framework/scripts/base.rb +++ b/lib/mysql_framework/scripts/base.rb @@ -50,6 +50,14 @@ def index_exists?(client, table_name, index_name) result.count >= 1 end + def user_exists?(client, user, host) + result = client.query(<<~SQL) + SELECT user FROM mysql.user WHERE user = '#{user}' AND host = '#{host}'; + SQL + + 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}"; diff --git a/lib/mysql_framework/version.rb b/lib/mysql_framework/version.rb index 6023ab3..f59e35e 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.9' + VERSION = '2.2.0' end diff --git a/spec/lib/mysql_framework/scripts/base_spec.rb b/spec/lib/mysql_framework/scripts/base_spec.rb index 126c739..35cf602 100644 --- a/spec/lib/mysql_framework/scripts/base_spec.rb +++ b/spec/lib/mysql_framework/scripts/base_spec.rb @@ -87,6 +87,18 @@ end end + describe '#user_exists?' do + it 'returns true when user exists' do + expect(client).to receive(:query).and_return(['result']) + expect(subject.user_exists?(client,'foo', 'localhost')).to eq(true) + end + + it 'returns false when user does not exist' do + expect(client).to receive(:query).and_return([]) + expect(subject.user_exists?(client,'foo', 'localhost')).to eq(false) + end + end + describe '#index_up_to_date?' do before do expect(client).to receive(:query).and_return(