From 412302a09307cebd3b02f23fd2bb64e36564341d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 19 Apr 2021 08:53:57 +0200 Subject: [PATCH] Better database connection check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes the MySQL output and adds a rake task to properly check the connection This will e.g., output something like this ``` Database connection failed with error: FATAL: Datenbank »bla« existiert nicht ``` --- lib/tasks/database.rake | 17 ++++++++++++++++- packaging/scripts/check | 6 +++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/tasks/database.rake b/lib/tasks/database.rake index d5477e8bfa..5e243a828e 100644 --- a/lib/tasks/database.rake +++ b/lib/tasks/database.rake @@ -41,7 +41,22 @@ end namespace 'openproject' do namespace 'db' do desc 'Ensure database version compatibility' - task ensure_database_compatibility: ['environment', 'db:load_config'] do + task check_connection: %w[environment db:load_config] do + begin + ActiveRecord::Base.establish_connection + ActiveRecord::Base.connection.execute "SELECT 1;" + unless ActiveRecord::Base.connected? + puts "Database connection failed" + Kernel.exit 1 + end + rescue StandardError => e + puts "Database connection failed with error: #{e}" + Kernel.exit 1 + end + end + + desc 'Ensure database version compatibility' + task ensure_database_compatibility: %w[openproject:db:check_connection] do ## # Ensure database server version is compatible OpenProject::Database::check! diff --git a/packaging/scripts/check b/packaging/scripts/check index bf01971415..25addd348c 100755 --- a/packaging/scripts/check +++ b/packaging/scripts/check @@ -11,10 +11,10 @@ log_ko() { } # try to get a setting to make sure database connection works -if rake setting:get[protocol] &>/dev/null; then - log_ok "MySQL configuration is working" +if rake openproject:db:check_connection 2>/dev/null; then + log_ok "PostgreSQL configuration is working" else - log_ko "MySQL connection is NOT working" + log_ko "PostgreSQL connection is NOT working" fi if ps -u "$SERVER_USER" &>/dev/null ; then