From 60c40c06cf4d9e5522c6aade3b74aab725f99df8 Mon Sep 17 00:00:00 2001
From: Jorge Camarero Vera <jorge.camarero@cern.ch>
Date: Thu, 12 May 2022 15:46:15 +0200
Subject: [PATCH] Schema verify return 2 if it fails and the schema is
 upgrading

---
 ReleaseNotes.md               |  1 +
 catalogue/VerifySchemaCmd.cpp | 20 ++++++++++++++++++++
 catalogue/VerifySchemaCmd.hpp |  8 ++++++++
 3 files changed, 29 insertions(+)

diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index 3bfe1cbb56..43d7a4c4dd 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -8,6 +8,7 @@
 - cta/CTA#950 - Eliminate race condition preventing the drive to go down on failure
 - cta/CTA#1160 - Fix DrainingToDisk stale status in case if there is DiskWriteThreadPool thread left
 - cta/operations#708 - Fix "Should run cleaner but VID is missing. Putting the drive down"
+- cta/CTA#1197 - Return code from `cta-catalogue-schema-verify` should indicate if the schema is in UPGRADING state
 
 ### Building and Packaging
 ### Catalogue Schema
diff --git a/catalogue/VerifySchemaCmd.cpp b/catalogue/VerifySchemaCmd.cpp
index aa0a782147..f43104279a 100644
--- a/catalogue/VerifySchemaCmd.cpp
+++ b/catalogue/VerifySchemaCmd.cpp
@@ -87,6 +87,10 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
   result += schemaChecker->warnErrorLoggingTables();
   result += schemaChecker->warnMissingIndexes();
   result.displayWarnings(std::cout);
+  if(result.getStatus() == SchemaCheckerResult::Status::FAILED
+    && isUpgrading(&conn)) {
+    return 2;
+  }
   if(result.getStatus() == SchemaCheckerResult::Status::FAILED){
     return 1;
   }
@@ -113,5 +117,21 @@ void VerifySchemaCmd::printUsage(std::ostream &os) {
   VerifySchemaCmdLineArgs::printUsage(os);
 }
 
+bool VerifySchemaCmd::isUpgrading(rdbms::Conn *conn) {
+  const char *const sql =
+    "SELECT "
+      "CTA_CATALOGUE.STATUS AS STATUS "
+    "FROM "
+      "CTA_CATALOGUE";
+
+  auto stmt = conn->createStmt(sql);
+  auto rset = stmt.executeQuery();
+
+  if(!rset.next()) {
+    throw exception::Exception("CTA_CATALOGUE does not contain any STATUS");
+  }
+  return rset.columnString("STATUS") == "UPGRADING";
+}
+
 } // namespace catalogue
 } // namespace cta
diff --git a/catalogue/VerifySchemaCmd.hpp b/catalogue/VerifySchemaCmd.hpp
index b5abe39e0c..5226d22ecd 100644
--- a/catalogue/VerifySchemaCmd.hpp
+++ b/catalogue/VerifySchemaCmd.hpp
@@ -74,6 +74,14 @@ private:
    * @return True if the table exists.
    */
   bool tableExists(const std::string tableName, rdbms::Conn &conn) const;
+
+  /*
+   * Returns true if the catalogue is upgrading
+   *
+   * @param conn The database connection
+   * @return True if the catalogue is upgrading
+   */
+  bool isUpgrading(rdbms::Conn *conn);
   
 #if 0
   /**
-- 
GitLab