From 3420052321184938af784a7baff60c20f9cd8dcc Mon Sep 17 00:00:00 2001 From: Victor Kotlyar <Victor.Kotlyar@cern.ch> Date: Mon, 17 Jun 2019 16:55:24 +0200 Subject: [PATCH] Add verification of the schema values in CTA_CATALOGUE. --- catalogue/RdbmsCatalogue.cpp | 2 +- catalogue/VerifySchemaCmd.cpp | 49 ++++++++++++++++++++++++++++++----- catalogue/VerifySchemaCmd.hpp | 12 +++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/catalogue/RdbmsCatalogue.cpp b/catalogue/RdbmsCatalogue.cpp index 9f1bb020a9..64bbdfba00 100644 --- a/catalogue/RdbmsCatalogue.cpp +++ b/catalogue/RdbmsCatalogue.cpp @@ -5768,7 +5768,7 @@ std::map<std::string, uint64_t> RdbmsCatalogue::getSchemaVersion() const { const char *const sql = "SELECT " "CTA_CATALOGUE.SCHEMA_VERSION_MAJOR AS SCHEMA_VERSION_MAJOR," - "CTA_CATALOGUE.SCHEMA_VERSION_MINOR AS SCHEMA_VERSION_MINOR," + "CTA_CATALOGUE.SCHEMA_VERSION_MINOR AS SCHEMA_VERSION_MINOR " "FROM " "CTA_CATALOGUE"; diff --git a/catalogue/VerifySchemaCmd.cpp b/catalogue/VerifySchemaCmd.cpp index d8b2372613..e99d65331f 100644 --- a/catalogue/VerifySchemaCmd.cpp +++ b/catalogue/VerifySchemaCmd.cpp @@ -22,10 +22,13 @@ #include "catalogue/OracleCatalogueSchema.hpp" #include "catalogue/PostgresCatalogueSchema.hpp" #include "catalogue/SqliteCatalogueSchema.hpp" +#include "catalogue/CatalogueFactoryFactory.hpp" #include "common/exception/Exception.hpp" #include "rdbms/ConnPool.hpp" #include "rdbms/Login.hpp" #include "rdbms/AutocommitMode.hpp" +#include "common/log/DummyLogger.hpp" +#include "Catalogue.hpp" #include <algorithm> #include <map> @@ -68,7 +71,7 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar std::cerr << "Cannot verify the database schema because the CTA_CATALOGUE table does not exists" << std::endl; return 1; } - + std::unique_ptr<CatalogueSchema> schema; switch(login.dbType) { case rdbms::Login::DBTYPE_IN_MEMORY: @@ -100,11 +103,14 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar throw ex; } - const auto schemaVersion = schema->getSchemaVersion(); - for (const auto &schemaInfo : schemaVersion) { - std::cerr << schemaInfo.first << ':' << schemaInfo.second << std::endl; - } - + std::cerr << "Checking schema version..." << std::endl; + log::DummyLogger dummyLog("dummy", "dummy"); + const auto catalogueFactory = CatalogueFactoryFactory::create(dummyLog, login, maxNbConns, maxNbConns); + const auto catalogue = catalogueFactory->create(); + const auto schemaDbVersion = catalogue->getSchemaVersion(); + const auto schemaVersion = schema->getSchemaVersion(); + const VerifyStatus verifySchemaStatus = verifySchemaVersion(schemaVersion, schemaDbVersion); + std::cerr << "Checking table names..." << std::endl; const auto schemaTableNames = schema->getSchemaTableNames(); const auto dbTableNames = conn.getTableNames(); @@ -125,7 +131,8 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar const auto dbTriggerNames = conn.getTriggerNames(); const VerifyStatus verifyTriggersStatus = verifyTriggerNames(schemaTriggerNames, dbTriggerNames); - if (verifyTablesStatus == VerifyStatus::ERROR || + if (verifySchemaStatus == VerifyStatus::ERROR || + verifyTablesStatus == VerifyStatus::ERROR || verifyIndexesStatus == VerifyStatus::ERROR || verifySequencesStatus == VerifyStatus::ERROR || verifyTriggersStatus == VerifyStatus::ERROR ) { @@ -154,6 +161,34 @@ void VerifySchemaCmd::printUsage(std::ostream &os) { VerifySchemaCmdLineArgs::printUsage(os); } +//------------------------------------------------------------------------------ +// verifySchemaVersion +//------------------------------------------------------------------------------ +VerifySchemaCmd::VerifyStatus VerifySchemaCmd::verifySchemaVersion(const std::map<std::string, uint64_t> &schemaVersion, + const std::map<std::string, uint64_t> &schemaDbVersion) const { + VerifyStatus status = VerifyStatus::UNKNOWN; + for (const auto &schemaInfo : schemaVersion) { + if (schemaDbVersion.count(schemaInfo.first)) { + if (schemaInfo.second != schemaDbVersion.at(schemaInfo.first)) { + std::cerr << " ERROR: the schema version mismatch: SCHEMA[" + << schemaInfo.first << "] = " << schemaInfo.second + << ", DB[" << schemaInfo.first << "] = " + << schemaDbVersion.at(schemaInfo.first) << std::endl; + status = VerifyStatus::ERROR; + } + } else { + std::cerr << " ERROR: the schema version value for " << schemaInfo.first + <<" not found in the Catalogue DB" << std::endl; + status = VerifyStatus::ERROR; + } + } + if (status != VerifyStatus::INFO && status != VerifyStatus::ERROR) { + std::cerr << " OK" << std::endl; + status = VerifyStatus::OK; + } + return status; +} + //------------------------------------------------------------------------------ // verifyTableNames //------------------------------------------------------------------------------ diff --git a/catalogue/VerifySchemaCmd.hpp b/catalogue/VerifySchemaCmd.hpp index 0c176195bc..e65fb49f3d 100644 --- a/catalogue/VerifySchemaCmd.hpp +++ b/catalogue/VerifySchemaCmd.hpp @@ -74,6 +74,18 @@ private: */ bool tableExists(const std::string tableName, rdbms::Conn &conn) const; + /** + * Verifies schema version values in the database against the catalogue schema + * version values. + * Returns verification status as result. + * + * @param schemaVersion The map of the catalogue schema version values. + * @param schemaDbVersion The map of the database schema version values. + * @return The verification status code. + */ + VerifyStatus verifySchemaVersion(const std::map<std::string, uint64_t> &schemaVersion, + const std::map<std::string, uint64_t> &schemaDbVersion) const; + /** * Verifies table names in the database against the catalogue schema table names. * Returns verification status as result. -- GitLab