Skip to content
Snippets Groups Projects
Commit c25a468d authored by Jorge Camarero Vera's avatar Jorge Camarero Vera
Browse files

Log warn the comparation of indexes between catalogue and schema

parent daab0e0d
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@
### Features
- cta/CTA#1205 - Fail pipeline if cppcheck detects errors
- cta/CTA#1206 - Change NULL for nullptr
- cta/CTA#1217 - Schema verification should just issue an warning if there are extra indexes in the db that are not in the catalogue
### Bug fixes
### Building and Packaging
### Catalogue Schema
......
......@@ -16,6 +16,7 @@
*/
#include <algorithm>
#include <functional>
#include "SQLiteSchemaComparer.hpp"
#include "SQLiteSchemaInserter.hpp"
......@@ -83,25 +84,36 @@ void SQLiteSchemaComparer::insertSchemaInSQLite() {
m_isSchemaInserted = true;
}
SchemaCheckerResult SQLiteSchemaComparer::compareIndexes(){
SchemaCheckerResult SQLiteSchemaComparer::compareIndexes() {
insertSchemaInSQLite();
std::list<std::string> catalogueIndexes = m_databaseMetadataGetter.getIndexNames();
std::list<std::string> schemaIndexes = m_schemaMetadataGetter->getIndexNames();
return compareItems("INDEX", catalogueIndexes, schemaIndexes);
const Items catalogueIndexes = m_databaseMetadataGetter.getIndexNames();
const Items schemaIndexes = m_schemaMetadataGetter->getIndexNames();
return compareItems("INDEX", std::make_tuple(catalogueIndexes, Level::Warn), std::make_tuple(schemaIndexes, Level::Error));
}
SchemaCheckerResult SQLiteSchemaComparer::compareItems(const std::string &itemType, const std::list<std::string>& itemsFromDatabase, const std::list<std::string>& itemsFromSQLite){
SchemaCheckerResult SQLiteSchemaComparer::compareItems(const std::string &itemType, const LoggedItems& fromDatabase,
const LoggedItems& fromSQLite) {
SchemaCheckerResult result;
for(auto &databaseItem: itemsFromDatabase){
if(std::find(itemsFromSQLite.begin(),itemsFromSQLite.end(),databaseItem) == itemsFromSQLite.end()){
result.addError(itemType+" "+databaseItem+" is missing in the schema but defined in the "+m_databaseToCheckName+" database.");
}
}
for(auto &sqliteItem: itemsFromSQLite){
if(std::find(itemsFromDatabase.begin(),itemsFromDatabase.end(),sqliteItem) == itemsFromDatabase.end()){
result.addError(itemType+" "+sqliteItem+" is missing in the "+m_databaseToCheckName+" database but is defined in the schema.");
const auto [itemsFromDatabase, logFromDataBase] = fromDatabase;
const auto [itemsFromSQLite, logFromSQLite] = fromSQLite;
auto findMismatchs = [&result, &itemType](const Items& items, const Items& itemsToCompare, const std::string& message,
const Level& logLevel) -> void {
std::function<void(const std::string&)> addResult;
if (logLevel == Level::Error) addResult = [&result](const std::string& msg) {result.addError(msg);};
if (logLevel == Level::Warn) addResult = [&result](const std::string& msg) {result.addWarning(msg);};
for (auto &item : items) {
if (std::find(itemsToCompare.begin(), itemsToCompare.end(), item) == itemsToCompare.end()) {
addResult(itemType + " " + item + message);
}
}
}
};
std::string logMsg = " is missing in the schema but defined in the " + m_databaseToCheckName + " database.";
findMismatchs(itemsFromDatabase, itemsFromSQLite, logMsg, logFromDataBase);
logMsg = " is missing in the " + m_databaseToCheckName + " database but is defined in the schema.";
findMismatchs(itemsFromSQLite, itemsFromDatabase, logMsg, logFromSQLite);
return result;
}
......@@ -121,7 +133,9 @@ SchemaCheckerResult SQLiteSchemaComparer::compareTables(const std::list<std::str
schemaTableColumns[schemaTable] = m_schemaMetadataGetter->getColumns(schemaTable);
if(m_compareTableConstraints) {
schemaTableConstraints[schemaTable] = m_schemaMetadataGetter->getConstraintNames(schemaTable);
result += compareItems("IN TABLE "+schemaTable+", CONSTRAINT",databaseTableConstraints[schemaTable],schemaTableConstraints[schemaTable]);
const LoggedItems databaseConstrains = std::make_tuple(databaseTableConstraints[schemaTable], Level::Error);
const LoggedItems schemaConstrains = std::make_tuple(schemaTableConstraints[schemaTable], Level::Error);
result += compareItems("IN TABLE " + schemaTable + ", CONSTRAINT", databaseConstrains, schemaConstrains);
}
}
......
......@@ -16,6 +16,8 @@
*/
#pragma once
#include <tuple>
#include "SchemaComparer.hpp"
namespace cta {
......@@ -46,7 +48,10 @@ public:
private:
void insertSchemaInSQLite();
SchemaCheckerResult compareItems(const std::string &itemType, const std::list<std::string>& itemsFromDatabase, const std::list<std::string>& itemsFromSQLite);
enum Level {Warn, Error};
using Items = std::list<std::string>;
using LoggedItems = std::tuple<Items, Level>;
SchemaCheckerResult compareItems(const std::string &itemType, const LoggedItems& fromDatabase, const LoggedItems& fromSQLite);
SchemaCheckerResult compareTables(const std::list<std::string> &databaseTables, const std::list<std::string> &schemaTables);
typedef std::map<std::string, std::map<std::string, std::string>> TableColumns;
SchemaCheckerResult compareTableColumns(const TableColumns & schema1TableColumns, const std::string &schema1Type,const TableColumns & schema2TableColumns, const std::string &schema2Type);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment