From d00081ee54b9edd09062e66b73a34a84015b4fe5 Mon Sep 17 00:00:00 2001 From: Tom Krouper Date: Wed, 11 Dec 2024 14:49:53 -0800 Subject: [PATCH] Change db_initialized function to check against database instead of files --- CHANGELOG.md | 2 ++ libraries/helpers.rb | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d621ceda..2bfdfd34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. ## Unreleased +Fix #715: Update `db_initialized?` to be based on database connection instead of file existence + ## 11.1.9 - *2024-12-09* ## 11.1.8 - *2024-11-18* diff --git a/libraries/helpers.rb b/libraries/helpers.rb index ad96fee6..d84084f5 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -257,11 +257,21 @@ def db_init end def db_initialized? - if v80plus - ::File.exist? "#{data_dir}/mysql.ibd" - else - ::File.exist? "#{data_dir}/mysql/user.frm" - end + cmd = shell_out("test \"#{db_initialized_check_cmd}\" -gt 0", user: 'root') + cmd.exitstatus == 0 + end + + def db_initialized_check_cmd + # If MySQL is running, mysqladmin ping will return 0 even with invalid credentials + # if MySQL is not running, mysqladmin will return 1 even with valid credentials + cmd = mysqladmin_bin + cmd << " --user=UNKNOWN_MYSQL_USER" + cmd << " ping > /dev/null 2>&1 &&" + cmd << " #{mysql_client_bin} #{defaults_file}" + cmd << " --skip-column-names --batch" + cmd << " --execute=\"SELECT count(*) FROM mysql.db WHERE db LIKE 'test%'\"" + return "scl enable #{scl_name} \"#{cmd}\"" if scl_package? + cmd end def mysql_install_db_bin @@ -279,6 +289,12 @@ def mysql_install_db_cmd cmd end + def mysql_client_bin + return "#{prefix_dir}/bin/mysql" if platform_family?('smartos') + return 'mysql' if scl_package? + "#{prefix_dir}/usr/bin/mysql" + end + def mysqladmin_bin return "#{prefix_dir}/bin/mysqladmin" if platform_family?('smartos') return 'mysqladmin' if scl_package?