Commit 564ccec7 authored by Cedric CAFFY's avatar Cedric CAFFY
Browse files

cta-catalogue-schema-verify now take the folder path where the schemas are located as an argument

parent 65ff1c82
......@@ -25,17 +25,18 @@
namespace cta {
namespace catalogue {
SQLiteSchemaComparer::SQLiteSchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType, rdbms::Conn &catalogueConn): SchemaComparer(catalogueDbType,catalogueConn) {
SQLiteSchemaComparer::SQLiteSchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType, rdbms::Conn &catalogueConn, const std::string & allSchemasVersionPath): SchemaComparer(catalogueDbType,catalogueConn),m_allSchemasVersionPath(allSchemasVersionPath) {
log::DummyLogger dl("dummy","dummy");
auto login = rdbms::Login::parseString("in_memory");
rdbms::ConnPool connPool(login,1);
rdbms::Conn conn = connPool.getConn();
m_sqliteConn = std::move(conn);
m_sqliteConnPool.reset(new rdbms::ConnPool(login,1));
m_sqliteConn = std::move(m_sqliteConnPool->getConn());
m_sqliteSchemaMetadataGetter.reset(new SQLiteCatalogueMetadataGetter(m_sqliteConn));
}
SQLiteSchemaComparer::~SQLiteSchemaComparer() {
m_sqliteSchemaMetadataGetter.release();
//Release the connection before the connPool is deleted
m_sqliteConn.~Conn();
m_sqliteConnPool.reset();
}
SchemaComparerResult SQLiteSchemaComparer::compare(){
......@@ -54,7 +55,7 @@ SchemaComparerResult SQLiteSchemaComparer::compareTables(){
}
void SQLiteSchemaComparer::insertSchemaInSQLite() {
cta::catalogue::SQLiteSchemaInserter schemaInserter(m_catalogueSchemaVersion,m_dbType,"/home/cedric/CTA/catalogue/",m_sqliteConn);
cta::catalogue::SQLiteSchemaInserter schemaInserter(m_catalogueSchemaVersion,m_dbType,m_allSchemasVersionPath,m_sqliteConn);
schemaInserter.insert();
}
......
......@@ -24,7 +24,7 @@ namespace catalogue {
class SQLiteSchemaComparer: public SchemaComparer {
public:
SQLiteSchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType, rdbms::Conn &catalogueConn);
SQLiteSchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType, rdbms::Conn &catalogueConn, const std::string & allSchemasVersionPath);
SchemaComparerResult compare() override;
virtual ~SQLiteSchemaComparer();
......@@ -37,7 +37,9 @@ private:
typedef std::map<std::string, std::map<std::string, std::string>> TableColumns;
SchemaComparerResult compareTableColumns(const TableColumns & tableColumns1, const std::string &originTableColumns1,const TableColumns & tableColumns2, const std::string &originTableColumns2);
rdbms::Conn m_sqliteConn;
std::unique_ptr<SQLiteCatalogueMetadataGetter> m_sqliteSchemaMetadataGetter;
std::unique_ptr<rdbms::ConnPool> m_sqliteConnPool;
std::unique_ptr<SQLiteCatalogueMetadataGetter> m_sqliteSchemaMetadataGetter;
const std::string m_allSchemasVersionPath;
};
}}
\ No newline at end of file
......@@ -27,7 +27,7 @@
namespace cta {
namespace catalogue {
SQLiteSchemaInserter::SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allVersionsSchemaDirectory,rdbms::Conn &sqliteConn):m_schemaVersion(schemaVersion),m_dbType(catalogueDbType),m_allVersionSchemaDirectory(allVersionsSchemaDirectory), m_sqliteCatalogueConn(sqliteConn){}
SQLiteSchemaInserter::SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allSchemasVersionPath,rdbms::Conn &sqliteConn):m_schemaVersion(schemaVersion),m_dbType(catalogueDbType),m_allSchemasVersionPath(allSchemasVersionPath), m_sqliteCatalogueConn(sqliteConn){}
SQLiteSchemaInserter::~SQLiteSchemaInserter() {}
......@@ -81,7 +81,7 @@ std::string SQLiteSchemaInserter::readSchemaFromFile() {
}
std::string SQLiteSchemaInserter::getSchemaFilePath() {
return m_allVersionSchemaDirectory+m_schemaVersion+"/"+getDatabaseType()+c_catalogueFileNameTrailer;
return m_allSchemasVersionPath+m_schemaVersion+"/"+getDatabaseType()+c_catalogueFileNameTrailer;
}
std::string SQLiteSchemaInserter::getDatabaseType() {
......
......@@ -25,7 +25,7 @@ namespace catalogue {
class SQLiteSchemaInserter {
public:
SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allVersionsSchemaDirectory,rdbms::Conn &sqliteConn);
SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allSchemasVersionPath,rdbms::Conn &sqliteConn);
void insert();
virtual ~SQLiteSchemaInserter();
private:
......@@ -33,7 +33,7 @@ namespace catalogue {
std::string m_schemaVersion;
cta::rdbms::Login::DbType m_dbType;
std::string m_allVersionSchemaDirectory;
std::string m_allSchemasVersionPath;
cta::rdbms::Conn & m_sqliteCatalogueConn;
std::string readSchemaFromFile();
std::list<std::string> getAllStatementsFromSchema(const std::string &schema);
......
......@@ -22,25 +22,28 @@
namespace cta{
namespace catalogue{
SchemaChecker::SchemaChecker(const cta::rdbms::Login::DbType &catalogueDbType,cta::rdbms::Conn &conn):m_dbType(catalogueDbType),m_conn(conn) {
m_schemaComparer.reset(new SQLiteSchemaComparer(m_dbType,m_conn));
SchemaChecker::SchemaChecker(rdbms::Login::DbType dbType,cta::rdbms::Conn &conn):m_dbType(dbType),m_catalogueConn(conn) {
}
SchemaChecker::~SchemaChecker() {
}
void SchemaChecker::setSchemaComparer(std::unique_ptr<SchemaComparer> schemaComparer){
if(m_schemaComparer != nullptr){
m_schemaComparer.release();
}
m_schemaComparer = std::move(schemaComparer);
void SchemaChecker::useSQLiteSchemaComparer(const std::string &allSchemasVersionPath){
m_schemaComparer.reset(new SQLiteSchemaComparer(m_dbType,m_catalogueConn,allSchemasVersionPath));
}
void SchemaChecker::compareSchema(){
SchemaChecker::Status SchemaChecker::compareSchema(){
if(m_schemaComparer == nullptr){
throw cta::exception::Exception("No schema comparer used. Please specify the schema comparer by using the methods useXXXXSchemaComparer(schemaComparer)");
}
cta::catalogue::SchemaComparerResult res = m_schemaComparer->compare();
std::cout << "Schema version : " << m_schemaComparer->getCatalogueVersion() << std::endl;
std::cout << "Status of the checking : " << cta::catalogue::SchemaComparerResult::StatusToString(res.getStatus()) << std::endl;
res.printDiffs();
std::cout << "Status of the checking : " << cta::catalogue::SchemaComparerResult::StatusToString(res.getStatus()) << std::endl;
if(res.getStatus() == SchemaComparerResult::Status::FAILED){
return SchemaChecker::FAILURE;
}
return SchemaChecker::OK;
}
void SchemaChecker::checkNoParallelTables(){
......
......@@ -26,14 +26,18 @@ namespace catalogue{
class SchemaChecker {
public:
SchemaChecker(const cta::rdbms::Login::DbType &catalogueDbType,cta::rdbms::Conn &conn);
enum Status {
OK,
FAILURE
};
SchemaChecker(rdbms::Login::DbType dbType,cta::rdbms::Conn &conn);
virtual ~SchemaChecker();
void setSchemaComparer(std::unique_ptr<SchemaComparer> schemaComparer);
void compareSchema();
void useSQLiteSchemaComparer(const std::string &allSchemasDirectoryPath);
Status compareSchema();
void checkNoParallelTables();
private:
const cta::rdbms::Login::DbType &m_dbType;
cta::rdbms::Conn &m_conn;
cta::rdbms::Login::DbType m_dbType;
cta::rdbms::Conn &m_catalogueConn;
std::unique_ptr<SchemaComparer> m_schemaComparer;
};
......
......@@ -30,6 +30,7 @@
#include "common/log/DummyLogger.hpp"
#include "Catalogue.hpp"
#include "SchemaChecker.hpp"
#include "SQLiteSchemaComparer.hpp"
#include <algorithm>
#include <map>
......@@ -60,6 +61,8 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
printUsage(m_out);
return 0;
}
std::string allSchemasDirectoryPath = cmdLineArgs.allSchemaDirectoryPath;
auto login = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath);
const uint64_t maxNbConns = 1;
......@@ -73,75 +76,83 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
return 1;
}
cta::catalogue::SchemaChecker schemaChecker(login.dbType,conn);
schemaChecker.compareSchema();
schemaChecker.checkNoParallelTables();
if(!cmdLineArgs.allSchemaDirectoryPath.empty()){
cta::catalogue::SchemaChecker schemaChecker(login.dbType,conn);
schemaChecker.useSQLiteSchemaComparer(allSchemasDirectoryPath);
SchemaChecker::Status comparisonStatus = schemaChecker.compareSchema();
schemaChecker.checkNoParallelTables();
if(comparisonStatus == SchemaChecker::Status::FAILURE){
return 1;
}
return 0;
} else {
std::unique_ptr<CatalogueSchema> schema;
switch(login.dbType) {
case rdbms::Login::DBTYPE_IN_MEMORY:
case rdbms::Login::DBTYPE_SQLITE:
schema.reset(new SqliteCatalogueSchema);
break;
case rdbms::Login::DBTYPE_POSTGRESQL:
schema.reset(new PostgresCatalogueSchema);
break;
case rdbms::Login::DBTYPE_MYSQL:
schema.reset(new MysqlCatalogueSchema);
break;
case rdbms::Login::DBTYPE_ORACLE:
schema.reset(new OracleCatalogueSchema);
break;
case rdbms::Login::DBTYPE_NONE:
throw exception::Exception("Cannot verify a catalogue without a database type");
default:
{
exception::Exception ex;
ex.getMessage() << "Unknown database type: value=" << login.dbType;
throw ex;
}
}
/*std::unique_ptr<CatalogueSchema> schema;
switch(login.dbType) {
case rdbms::Login::DBTYPE_IN_MEMORY:
case rdbms::Login::DBTYPE_SQLITE:
schema.reset(new SqliteCatalogueSchema);
break;
case rdbms::Login::DBTYPE_POSTGRESQL:
schema.reset(new PostgresCatalogueSchema);
break;
case rdbms::Login::DBTYPE_MYSQL:
schema.reset(new MysqlCatalogueSchema);
break;
case rdbms::Login::DBTYPE_ORACLE:
schema.reset(new OracleCatalogueSchema);
break;
case rdbms::Login::DBTYPE_NONE:
throw exception::Exception("Cannot verify a catalogue without a database type");
default:
{
if (nullptr == schema) {
exception::Exception ex;
ex.getMessage() << "Unknown database type: value=" << login.dbType;
throw ex;
ex.getMessage() << "The catalogue schema uninitialized";
throw ex;
}
}
if (nullptr == schema) {
exception::Exception ex;
ex.getMessage() << "The catalogue schema uninitialized";
throw ex;
}
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();
const VerifyStatus verifyTablesStatus = verifyTableNames(schemaTableNames, dbTableNames);
std::cerr << "Checking columns in tables..." << std::endl;
const VerifyStatus verifyColumnsStatus = verifyColumns(schemaTableNames, dbTableNames, *schema, conn);
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 index names..." << std::endl;
const auto schemaIndexNames = schema->getSchemaIndexNames();
const auto dbIndexNames = conn.getIndexNames();
const VerifyStatus verifyIndexesStatus = verifyIndexNames(schemaIndexNames, dbIndexNames);
std::cerr << "Checking table names..." << std::endl;
const auto schemaTableNames = schema->getSchemaTableNames();
const auto dbTableNames = conn.getTableNames();
const VerifyStatus verifyTablesStatus = verifyTableNames(schemaTableNames, dbTableNames);
std::cerr << "Checking sequence names..." << std::endl;
const auto schemaSequenceNames = schema->getSchemaSequenceNames();
const auto dbSequenceNames = conn.getSequenceNames();
const VerifyStatus verifySequencesStatus = verifySequenceNames(schemaSequenceNames, dbSequenceNames);
std::cerr << "Checking columns in tables..." << std::endl;
const VerifyStatus verifyColumnsStatus = verifyColumns(schemaTableNames, dbTableNames, *schema, conn);
if (verifySchemaStatus == VerifyStatus::ERROR ||
verifyTablesStatus == VerifyStatus::ERROR ||
verifyIndexesStatus == VerifyStatus::ERROR ||
verifySequencesStatus == VerifyStatus::ERROR ||
verifyColumnsStatus == VerifyStatus::ERROR ) {
return 1;
}*/
return 0;
std::cerr << "Checking index names..." << std::endl;
const auto schemaIndexNames = schema->getSchemaIndexNames();
const auto dbIndexNames = conn.getIndexNames();
const VerifyStatus verifyIndexesStatus = verifyIndexNames(schemaIndexNames, dbIndexNames);
std::cerr << "Checking sequence names..." << std::endl;
const auto schemaSequenceNames = schema->getSchemaSequenceNames();
const auto dbSequenceNames = conn.getSequenceNames();
const VerifyStatus verifySequencesStatus = verifySequenceNames(schemaSequenceNames, dbSequenceNames);
if (verifySchemaStatus == VerifyStatus::ERROR ||
verifyTablesStatus == VerifyStatus::ERROR ||
verifyIndexesStatus == VerifyStatus::ERROR ||
verifySequencesStatus == VerifyStatus::ERROR ||
verifyColumnsStatus == VerifyStatus::ERROR ) {
return 1;
}
return 0;
}
}
//------------------------------------------------------------------------------
......@@ -149,7 +160,7 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
//------------------------------------------------------------------------------
bool VerifySchemaCmd::tableExists(const std::string tableName, rdbms::Conn &conn) const {
const auto names = conn.getTableNames();
for(auto &name : names) {
for(const auto &name : names) {
if(tableName == name) {
return true;
}
......
......@@ -32,6 +32,7 @@ VerifySchemaCmdLineArgs::VerifySchemaCmdLineArgs(const int argc, char *const *co
help(false) {
static struct option longopts[] = {
{"schemaversionsdirectory",required_argument,NULL,'s'},
{"help", no_argument, NULL, 'h'},
{NULL , 0, NULL, 0}
};
......@@ -41,15 +42,18 @@ VerifySchemaCmdLineArgs::VerifySchemaCmdLineArgs(const int argc, char *const *co
opterr = 0;
int opt = 0;
while((opt = getopt_long(argc, argv, ":h", longopts, NULL)) != -1) {
while((opt = getopt_long(argc, argv, ":hs:", longopts, NULL)) != -1) {
switch(opt) {
case 'h':
help = true;
break;
case 's':
allSchemaDirectoryPath = optarg;
break;
case ':': // Missing parameter
{
exception::CommandLineNotParsed ex;
ex.getMessage() << "The -" << (char)opt << " option requires a parameter";
ex.getMessage() << "The -" << (char)optopt << " option requires a parameter";
throw ex;
}
case '?': // Unknown option
......
......@@ -38,6 +38,8 @@ struct VerifySchemaCmdLineArgs {
* database.
*/
std::string dbConfigPath;
std::string allSchemaDirectoryPath;
/**
* Constructor that parses the specified command-line arguments.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment