Commit 1f4167bf authored by Steven Murray's avatar Steven Murray
Browse files

Added cta-catalogue-schema-drop command-line tool

parent 9989a609
......@@ -158,5 +158,34 @@ add_executable(cta-catalogue-schema-create
target_link_libraries (cta-catalogue-schema-create
ctacatalogue)
add_custom_command(OUTPUT drop_oracle_catalogue_schema.cpp
COMMAND sed 's/^/\ \ \"/' ${CMAKE_CURRENT_SOURCE_DIR}/drop_oracle_catalogue_schema.sql | sed 's/$$/\"/' > drop_oracle_catalogue_schema.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/drop_oracle_catalogue_schema.sql)
add_custom_command(OUTPUT DropOracleCatalogueSchema.cpp
COMMAND sed '/DROP_CTA_SQL_SCHEMA/r drop_oracle_catalogue_schema.cpp' ${CMAKE_CURRENT_SOURCE_DIR}/DropOracleCatalogueSchema.before_SQL.cpp > DropOracleCatalogueSchema.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/DropOracleCatalogueSchema.before_SQL.cpp drop_oracle_catalogue_schema.cpp)
add_custom_command(OUTPUT drop_sqlite_catalogue_schema.cpp
COMMAND sed 's/^/\ \ \"/' ${CMAKE_CURRENT_SOURCE_DIR}/drop_sqlite_catalogue_schema.sql | sed 's/$$/\"/' > drop_sqlite_catalogue_schema.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/drop_sqlite_catalogue_schema.sql)
add_custom_command(OUTPUT DropSqliteCatalogueSchema.cpp
COMMAND sed '/DROP_CTA_SQL_SCHEMA/r drop_sqlite_catalogue_schema.cpp' ${CMAKE_CURRENT_SOURCE_DIR}/DropSqliteCatalogueSchema.before_SQL.cpp > DropSqliteCatalogueSchema.cpp
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/DropSqliteCatalogueSchema.before_SQL.cpp drop_sqlite_catalogue_schema.cpp)
install (TARGETS cta-catalogue-schema-create DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-schema-create.1cta DESTINATION /usr/share/man/man1)
add_executable(cta-catalogue-schema-drop
DropOracleCatalogueSchema.cpp
DropSchemaCmd.cpp
DropSchemaCmdLineArgs.cpp
DropSchemaCmdMain.cpp
DropSqliteCatalogueSchema.cpp)
target_link_libraries (cta-catalogue-schema-drop
ctacatalogue)
install (TARGETS cta-catalogue-schema-drop DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-schema-drop.1cta DESTINATION /usr/share/man/man1)
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catalogue/DropOracleCatalogueSchema.hpp"
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
DropOracleCatalogueSchema::DropOracleCatalogueSchema(): sql(
// DROP_CTA_SQL_SCHEMA - The contents of drop_oracle_catalogue_schema.cpp go here
) {
}
} // namespace catalogue
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
namespace cta {
namespace catalogue {
/**
* Structure containing the SQL to drop the schema of the CTA catalogue database
* from an Oracle database.
*
* The CMakeLists.txt file of this directory instructs cmake to generate
* DropOracleCatalogueSchema.cpp from:
* - DropOracleCatalogueSchema.before_SQL.cpp
* - drop_oracle_catalogue_schema.sql
*
* The DropOracleCatalogueSchema.before_SQL.cpp file is not compilable and is
* therefore difficult for Integrated Developent Environments (IDEs) to handle.
*
* The purpose of this class is to help IDEs by isolating the "non-compilable"
* issues into a small cpp file.
*/
struct DropOracleCatalogueSchema {
/**
* Constructor.
*/
DropOracleCatalogueSchema();
/**
* The schema.
*/
const std::string sql;
};
} // namespace catalogue
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catalogue/CatalogueFactory.hpp"
#include "catalogue/DropOracleCatalogueSchema.hpp"
#include "catalogue/DropSchemaCmd.hpp"
#include "catalogue/DropSchemaCmdLineArgs.hpp"
#include "catalogue/DropSqliteCatalogueSchema.hpp"
#include "common/exception/Exception.hpp"
#include "rdbms/ConnFactoryFactory.hpp"
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
DropSchemaCmd::DropSchemaCmd(
std::istream &inStream,
std::ostream &outStream,
std::ostream &errStream):
CmdLineTool(inStream, outStream, errStream) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
DropSchemaCmd::~DropSchemaCmd() noexcept {
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
int DropSchemaCmd::exceptionThrowingMain(const int argc, char *const *const argv) {
const DropSchemaCmdLineArgs cmdLineArgs(argc, argv);
const rdbms::Login dbLogin = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath);
// Abort if the schema is already dropped
{
auto factory = rdbms::ConnFactoryFactory::create(dbLogin);
auto conn = factory->create();
if(conn->getTableNames().empty()) {
m_out << "Database contains no tables. Assuming the schema has already been dropped." << std::endl;
return 0;
}
}
// Abort if the schema is not locked
{
const uint64_t nbDbConns = 1;
auto catalogue = CatalogueFactory::create(dbLogin, nbDbConns);
if(catalogue->schemaIsLocked()) {
m_err <<
"Cannot drop the schema of the catalogue database because the schema is locked.\n"
"\n"
"Please see the following command-line tools:\n"
" cta-catalogue-schema-lock\n"
" cta-catalogue-schema-status\n"
" cta-catalogue-schema-unlock" << std::endl;
return 1;
}
}
dropCatalogueSchema(dbLogin);
return 0;
}
//------------------------------------------------------------------------------
// dropCatalogueSchema
//------------------------------------------------------------------------------
void DropSchemaCmd::dropCatalogueSchema(const rdbms::Login &dbLogin) {
auto factory = rdbms::ConnFactoryFactory::create(dbLogin);
auto conn = factory->create();
try {
switch(dbLogin.dbType) {
case rdbms::Login::DBTYPE_IN_MEMORY:
case rdbms::Login::DBTYPE_SQLITE:
{
DropSqliteCatalogueSchema dropSchema;
conn->executeNonQueries(dropSchema.sql);
}
break;
case rdbms::Login::DBTYPE_ORACLE:
{
DropOracleCatalogueSchema dropSchema;
conn->executeNonQueries(dropSchema.sql);
}
break;
case rdbms::Login::DBTYPE_NONE:
throw exception::Exception("Cannot delete the schema of catalogue database without a database type");
default:
{
exception::Exception ex;
ex.getMessage() << "Unknown database type: value=" << dbLogin.dbType;
throw ex;
}
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
} // namespace catalogue
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "catalogue/Catalogue.hpp"
#include "catalogue/CmdLineTool.hpp"
#include "rdbms/Conn.hpp"
#include "rdbms/Login.hpp"
namespace cta {
namespace catalogue {
/**
* Command-line tool that deletes all the rows of all the tables in the
* catalogue database except for the CTA_CATALOGUE table.
*/
class DropSchemaCmd: public CmdLineTool {
public:
/**
* Constructor.
*
* @param inStream Standard input stream.
* @param outStream Standard output stream.
* @param errStream Standard error stream.
*/
DropSchemaCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream);
/**
* Destructor.
*/
~DropSchemaCmd() noexcept;
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
int exceptionThrowingMain(const int argc, char *const *const argv);
private:
/**
* Unconditionally drops the schema of the catalogue database associated with
* the specified database login.
*
* @param dbLogin The database login.
*/
void dropCatalogueSchema(const rdbms::Login &dbLogin);
}; // class DropSchemaCmd
} // namespace catalogue
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catalogue/DropSchemaCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include <ostream>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
DropSchemaCmdLineArgs::DropSchemaCmdLineArgs(const int argc, const char *const *const argv) {
if(argc != 2) {
exception::Exception ex;
ex.getMessage() << "Wrong number of command-line arguments: excepted=1 actual=" << (argc - 1) << std::endl <<
std::endl;
printUsage(ex.getMessage());
throw ex;
}
dbConfigPath = argv[1];
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void DropSchemaCmdLineArgs::printUsage(std::ostream &os) {
os <<
"Usage:" << std::endl <<
" cta-catalogue-schema-drop databaseConnectionFile" << std::endl <<
"Where:" << std::endl <<
" databaseConnectionFile" << std::endl <<
" The path to the file containing the connection details of the CTA" << std::endl <<
" catalogue database" << std::endl;
}
} // namespace catalogue
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
namespace cta {
namespace catalogue {
/**
* Structure to store the command-line arguments of the command-line tool
* named cta-catalogue-schema-drop
*/
struct DropSchemaCmdLineArgs {
/**
* Path to the file containing the connection details of the catalogue
* database.
*/
std::string dbConfigPath;
/**
* Constructor that parses the specified command-line arguments.
*
* @param argc The number of command-line arguments including the name of the
* executable.
* @param argv The vector of command-line arguments.
*/
DropSchemaCmdLineArgs(const int argc, const char *const *const argv);
/**
* Prints the usage message of the command-line tool.
*
* @param os The output stream to which the usage message is to be printed.
*/
static void printUsage(std::ostream &os);
};
} // namespace catalogue
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catalogue/DropSchemaCmd.hpp"
#include "common/exception/Exception.hpp"
#include <iostream>
/**
* An exception throwing version of main().
*
* @param argc The number of command-line arguments including the program name.
* @param argv The command-line arguments.
* @return The exit value of the program.
*/
static int exceptionThrowingMain(const int argc, char *const *const argv);
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char *const *const argv) {
using namespace cta;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::Exception &ex) {
errorMessage = ex.getMessage().str();
} catch(std::exception &se) {
errorMessage = se.what();
} catch(...) {
errorMessage = "An unknown exception was thrown";
}
// Reaching this point means the command has failed, an exception was throw
// and errorMessage has been set accordingly
std::cerr << "Aborting: " << errorMessage << std::endl;
return 1;
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
static int exceptionThrowingMain(const int argc, char *const *const argv) {
using namespace cta;
catalogue::DropSchemaCmd cmd(std::cin, std::cout, std::cerr);
return cmd.exceptionThrowingMain(argc, argv);
}
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catalogue/DropSqliteCatalogueSchema.hpp"
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
DropSqliteCatalogueSchema::DropSqliteCatalogueSchema(): sql(
// DROP_CTA_SQL_SCHEMA - The contents of drop_sqlite_catalogue_schema.cpp go here
) {
}
} // namespace catalogue
} // namespace cta
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
namespace cta {
namespace catalogue {
/**
* Structure containing the SQL to drop the schema of the CTA catalogue database
* from an SQLite database.
*
* The CMakeLists.txt file of this directory instructs cmake to generate
* DropSqliteCatalogueSchema.cpp from:
* - DropSqliteCatalogueSchema.before_SQL.cpp
* - drop_sqlite_catalogue_schema.sql
*
* The DropSqliteCatalogueSchema.before_SQL.cpp file is not compilable and is
* therefore difficult for Integrated Developent Environments (IDEs) to handle.
*
* The purpose of this class is to help IDEs by isolating the "non-compilable"
* issues into a small cpp file.
*/
struct DropSqliteCatalogueSchema {
/**
* Constructor.
*/
DropSqliteCatalogueSchema();
/**
* The schema.
*/
const std::string sql;
};
} // namespace catalogue
} // namespace cta
.\" The CERN Tape Archive (CTA) project
.\" Copyright (C) 2015 CERN
.\"
.\" This program is free software: you can redistribute it and/or modify
.\" it under the terms of the GNU General Public License as published by
.\" the Free Software Foundation, either version 3 of the License, or
.\" (at your option) any later version.
.\"
.\" This program is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public License
.\" along with this program. If not, see <http://www.gnu.org/licenses/>.
.TH CTA-CATALOGUE-SCHEMA-DROP 1CTA "December 2016" CTA CTA
.SH NAME
cta-catalogue-schema-drop \- Drop the schema of the CTA catalogue database
.SH SYNOPSIS
.BI "cta-catalogue-schema-drop databaseConnectionFile"
.SH DESCRIPTION
\fBcta-catalogue-schema-drop\fP is a command-line tool that drops the schema of
the CTA catalogue database.
.P
This command-line tool will abort if it sees the value LOCKED in the
SCHEMA_STATUS column of the CTA_CATALOGUE database table.
.SH ARGUMENTS
.TP
\fBdatabaseConnectionFile
The path to the configuration file containing the connection details of the
CTA catalogue database.
.SH RETURN VALUE
Zero on success and non-zero on failure.
.SH EXAMPLES
cta-catalogue-schema-drop /etc/cta/cta_catalogue_db.conf
.SH AUTHOR
\fBCTA\fP Team