Skip to content
Snippets Groups Projects
Commit a667875d authored by Cedric CAFFY's avatar Cedric CAFFY Committed by Cedric Caffy
Browse files

cta-statistics-save --drop will delete all the tables that are located in its creation schema

parent 0f51c041
No related branches found
No related tags found
No related merge requests found
......@@ -62,17 +62,22 @@ int StatisticsSaveCmd::exceptionThrowingMain(const int argc, char *const *const
auto statisticsSchema = StatisticsSchemaFactory::create(loginStatistics.dbType);
if(cmdLineArgs.buildDatabase){
//Build the database
buildStatisticsDatabase(statisticsConn,*statisticsSchema);
return EXIT_SUCCESS;
}
if(cmdLineArgs.dropDatabase){
//drop the database
if(userConfirmDropStatisticsSchemaFromDb(loginStatistics)){
dropStatisticsDatabase(statisticsConn);
dropStatisticsDatabase(statisticsConn,*statisticsSchema);
}
return EXIT_SUCCESS;
}
//Save the CTA statistics
//Check the content of the CTA catalogue
auto loginCatalogue = rdbms::Login::parseFile(cmdLineArgs.catalogueDbConfigPath);
rdbms::ConnPool catalogueConnPool(loginCatalogue, maxNbConns);
auto catalogueConn = catalogueConnPool.getConn();
......@@ -87,6 +92,7 @@ int StatisticsSaveCmd::exceptionThrowingMain(const int argc, char *const *const
return EXIT_FAILURE;
}
//Check that the schema tables are in the statistics database
SchemaChecker::Builder statisticsCheckerBuilder("statistics",loginStatistics.dbType,statisticsConn);
std::unique_ptr<SchemaChecker> statisticsChecker =
statisticsCheckerBuilder.useCppSchemaStatementsReader(*statisticsSchema)
......@@ -94,20 +100,10 @@ int StatisticsSaveCmd::exceptionThrowingMain(const int argc, char *const *const
.build();
statisticsChecker->compareTablesLocatedInSchema();
//Compute the statistics
return EXIT_SUCCESS;
}
//------------------------------------------------------------------------------
// tableExists
//------------------------------------------------------------------------------
bool StatisticsSaveCmd::tableExists(const std::string tableName, rdbms::Conn &conn) const {
const auto names = conn.getTableNames();
for(const auto &name : names) {
if(tableName == name) {
return true;
}
}
return false;
}
//------------------------------------------------------------------------------
// printUsage
......@@ -117,21 +113,24 @@ void StatisticsSaveCmd::printUsage(std::ostream &os) {
}
void StatisticsSaveCmd::verifyCmdLineArgs(const StatisticsSaveCmdLineArgs& cmdLineArgs) const {
bool catalogueDbConfigPathEmpty = cmdLineArgs.catalogueDbConfigPath.empty();
bool statisticsDbConfigPathEmpty = cmdLineArgs.statisticsDbConfigPath.empty();
if(cmdLineArgs.buildDatabase && cmdLineArgs.dropDatabase){
throw cta::exception::Exception("--build and --drop are mutually exclusive.");
}
if(cmdLineArgs.buildDatabase && !cmdLineArgs.catalogueDbConfigPath.empty()){
if(cmdLineArgs.buildDatabase && !catalogueDbConfigPathEmpty){
throw cta::exception::Exception("The catalogue database configuration file should not be provided when --build flag is set.");
}
if(cmdLineArgs.dropDatabase && !cmdLineArgs.catalogueDbConfigPath.empty()){
if(cmdLineArgs.dropDatabase && !catalogueDbConfigPathEmpty){
throw cta::exception::Exception("The catalogue database configuration file should not be provided when --drop flag is set.");
}
if(cmdLineArgs.buildDatabase && cmdLineArgs.statisticsDbConfigPath.empty()){
throw cta::exception::Exception("The statistics database configuration file should be provided.");
}
if(cmdLineArgs.dropDatabase && cmdLineArgs.statisticsDbConfigPath.empty()){
if(statisticsDbConfigPathEmpty && (cmdLineArgs.buildDatabase || cmdLineArgs.dropDatabase)){
throw cta::exception::Exception("The statistics database configuration file should be provided.");
}
if((!cmdLineArgs.buildDatabase && !cmdLineArgs.dropDatabase) && (catalogueDbConfigPathEmpty || statisticsDbConfigPathEmpty)){
throw cta::exception::Exception("You should provide the catalogue database and the statistics database connection files.");
}
}
void StatisticsSaveCmd::buildStatisticsDatabase(cta::rdbms::Conn& statisticsDatabaseConn, const StatisticsSchema& statisticsSchema) {
......@@ -152,10 +151,10 @@ bool StatisticsSaveCmd::userConfirmDropStatisticsSchemaFromDb(const rdbms::Login
return userResponse == "yes";
}
void StatisticsSaveCmd::dropStatisticsDatabase(cta::rdbms::Conn& statisticsDatabaseConn) {
void StatisticsSaveCmd::dropStatisticsDatabase(cta::rdbms::Conn& statisticsDatabaseConn,const StatisticsSchema& statisticsSchema) {
try {
std::list<std::string> tablesInDb = statisticsDatabaseConn.getTableNames();
std::list<std::string> statisticsTables = {"CTA_STATISTICS"};
std::list<std::string> statisticsTables = statisticsSchema.getSchemaTableNames();
for(auto & tableToDrop: statisticsTables){
const bool tableToDropIsInDb = (tablesInDb.end() != std::find(tablesInDb.begin(), tablesInDb.end(), tableToDrop));
if(tableToDropIsInDb) {
......
......@@ -65,24 +65,34 @@ private:
* @param os The output stream to which the usage message is to be printed.
*/
void printUsage(std::ostream &os) override;
/**
* Returns true if the table with the specified name exists in the database
* schema of the specified database connection.
*
* @param tableName The name of the database table.
* @param conn The database connection.
* @return True if the table exists.
* Verifies the user input
* throws an exception if the command line args
* are not correct
* @param cmdLineArgs the command line arguments provided by the user
*/
bool tableExists(const std::string tableName, rdbms::Conn &conn) const;
void verifyCmdLineArgs(const StatisticsSaveCmdLineArgs& cmdLineArgs) const;
/**
* Create the tables needed to fill the CTA statistics database
* @param statisticsDatabaseConn the database where the CTA statistics will be stored
* @param statisticsSchema the schema that will be used to insert the CTA statistics tables
*/
void buildStatisticsDatabase(cta::rdbms::Conn &statisticsDatabaseConn,const StatisticsSchema & statisticsSchema);
/**
* Asks the user if he really wants to drop the CTA statistics tables from the database
* @param dbLogin the CTA statistics database
* @return true if the user confirmed, false otherwise
*/
bool userConfirmDropStatisticsSchemaFromDb(const rdbms::Login &dbLogin);
void dropStatisticsDatabase(cta::rdbms::Conn &statisticsDatabaseConn);
/**
* Drop the tables associated to the CTA statistics
* @param statisticsDatabaseConn the database where the CTA statistics tables will be deleted
*/
void dropStatisticsDatabase(cta::rdbms::Conn &statisticsDatabaseConn, const StatisticsSchema & statisticsSchema);
}; // class VerifySchemaCmd
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment