Commit dfd6d4ef authored by Steven Murray's avatar Steven Murray
Browse files

cta/CTA#86 Make error message more explanatory

When dropping the database schema, tables and sequences are only
dropped if they are present.

This commit avoids the message "table or view does not exist" by
modifying cta-catalogue-schema-drop to simply not try to delete
tables that do not exist.
parent 43bd4702
......@@ -131,31 +131,13 @@ 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)
DropSchemaCmdMain.cpp)
target_link_libraries (cta-catalogue-schema-drop
ctacatalogue)
......
......@@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <rdbms/OcciConn.hpp>
#include "catalogue/CatalogueFactory.hpp"
#include "catalogue/DropOracleCatalogueSchema.hpp"
#include "catalogue/DropSchemaCmd.hpp"
......@@ -26,6 +25,9 @@
#include "rdbms/ConnFactoryFactory.hpp"
#include "rdbms/OcciConn.hpp"
#include <algorithm>
#include <rdbms/OcciConn.hpp>
namespace cta {
namespace catalogue {
......@@ -153,8 +155,41 @@ void DropSchemaCmd::dropCatalogueSchema(const rdbms::Login::DbType &dbType, rdbm
//------------------------------------------------------------------------------
void DropSchemaCmd::dropSqliteCatalogueSchema(rdbms::Conn &conn) {
try {
DropSqliteCatalogueSchema dropSchema;
conn.executeNonQueries(dropSchema.sql);
std::list<std::string> tablesInDb = conn.getTableNames();
std::list<std::string> tablesToDrop = {
"CTA_CATALOGUE",
"ARCHIVE_ROUTE",
"TAPE_FILE",
"ARCHIVE_FILE",
"ARCHIVE_FILE_ID",
"TAPE",
"REQUESTER_MOUNT_RULE",
"REQUESTER_GROUP_MOUNT_RULE",
"ADMIN_USER",
"ADMIN_HOST",
"STORAGE_CLASS",
"TAPE_POOL",
"LOGICAL_LIBRARY",
"MOUNT_POLICY"};
dropDatabaseTables(conn, tablesToDrop);
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
//------------------------------------------------------------------------------
// dropDatabaseTables
//------------------------------------------------------------------------------
void DropSchemaCmd::dropDatabaseTables(rdbms::Conn &conn, const std::list<std::string> &tablesToDrop) {
try {
std::list<std::string> tablesInDb = conn.getTableNames();
for(auto tableToDrop : tablesToDrop) {
const bool tableToDropIsInDb = tablesInDb.end() != std::find(tablesInDb.begin(), tablesInDb.end(), tableToDrop);
if(tableToDropIsInDb) {
conn.executeNonQuery(std::string("DROP TABLE ") + tableToDrop, rdbms::Stmt::AutocommitMode::ON);
m_out << "Dropped table " << tableToDrop << std::endl;
}
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
......@@ -165,8 +200,47 @@ void DropSchemaCmd::dropSqliteCatalogueSchema(rdbms::Conn &conn) {
//------------------------------------------------------------------------------
void DropSchemaCmd::dropOracleCatalogueSchema(rdbms::Conn &conn) {
try {
DropOracleCatalogueSchema dropSchema;
conn.executeNonQueries(dropSchema.sql);
std::list<std::string> tablesInDb = conn.getTableNames();
std::list<std::string> tablesToDrop = {
"CTA_CATALOGUE",
"ARCHIVE_ROUTE",
"TAPE_FILE",
"ARCHIVE_FILE",
"TAPE",
"REQUESTER_MOUNT_RULE",
"REQUESTER_GROUP_MOUNT_RULE",
"ADMIN_USER",
"ADMIN_HOST",
"STORAGE_CLASS",
"TAPE_POOL",
"LOGICAL_LIBRARY",
"MOUNT_POLICY"
};
dropDatabaseTables(conn, tablesToDrop);
std::list<std::string> sequencesToDrop = {"ARCHIVE_FILE_ID_SEQ"};
rdbms::OcciConn &occiConn = dynamic_cast<rdbms::OcciConn &>(conn);
dropDatabaseSequences(occiConn, sequencesToDrop);
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
}
//------------------------------------------------------------------------------
// dropDatabaseSequences
//------------------------------------------------------------------------------
void DropSchemaCmd::dropDatabaseSequences(rdbms::OcciConn &conn, const std::list<std::string> &sequencesToDrop) {
try {
std::list<std::string> sequencesInDb = conn.getSequenceNames();
for(auto sequenceToDrop : sequencesToDrop) {
const bool sequenceToDropIsInDb = sequencesInDb.end() != std::find(sequencesInDb.begin(), sequencesInDb.end(),
sequenceToDrop);
if(sequenceToDropIsInDb) {
conn.executeNonQuery(std::string("DROP SEQUENCE ") + sequenceToDrop, rdbms::Stmt::AutocommitMode::ON);
m_out << "Dropped sequence " << sequenceToDrop << std::endl;
}
}
} catch(exception::Exception &ex) {
throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
}
......
......@@ -22,6 +22,7 @@
#include "catalogue/CmdLineTool.hpp"
#include "rdbms/Conn.hpp"
#include "rdbms/Login.hpp"
#include "rdbms/OcciConn.hpp"
namespace cta {
namespace catalogue {
......@@ -90,6 +91,13 @@ private:
*/
void dropSqliteCatalogueSchema(rdbms::Conn &conn);
/**
* Drops the database tables with the specified names.
*
* @param tablesToDrop The names of the database tables to be dropped.
*/
void dropDatabaseTables(rdbms::Conn &conn, const std::list<std::string> &tablesToDrop);
/**
* Unconditionally drops the schema of the catalogue database associated with
* the specified database connection.
......@@ -98,6 +106,13 @@ private:
*/
void dropOracleCatalogueSchema(rdbms::Conn &conn);
/**
* Drops the database sequences with the specified names.
*
* @param seqeuncesToDrop The names of the database sequences to be dropped.
*/
void dropDatabaseSequences(rdbms::OcciConn &conn, const std::list<std::string> &sequencesToDrop);
}; // class DropSchemaCmd
} // namespace catalogue
......
ALTER SESSION SET recyclebin = OFF;
DROP TABLE CTA_CATALOGUE;
DROP TABLE ARCHIVE_ROUTE;
DROP TABLE TAPE_FILE;
DROP TABLE ARCHIVE_FILE;
DROP TABLE TAPE;
DROP TABLE REQUESTER_MOUNT_RULE;
DROP TABLE REQUESTER_GROUP_MOUNT_RULE;
DROP TABLE ADMIN_USER;
DROP TABLE ADMIN_HOST;
DROP TABLE STORAGE_CLASS;
DROP TABLE TAPE_POOL;
DROP TABLE LOGICAL_LIBRARY;
DROP TABLE MOUNT_POLICY;
DROP SEQUENCE ARCHIVE_FILE_ID_SEQ;
DROP TABLE CTA_CATALOGUE;
DROP TABLE ARCHIVE_ROUTE;
DROP TABLE TAPE_FILE;
DROP TABLE ARCHIVE_FILE;
DROP TABLE ARCHIVE_FILE_ID;
DROP TABLE TAPE;
DROP TABLE REQUESTER_MOUNT_RULE;
DROP TABLE REQUESTER_GROUP_MOUNT_RULE;
DROP TABLE ADMIN_USER;
DROP TABLE ADMIN_HOST;
DROP TABLE STORAGE_CLASS;
DROP TABLE TAPE_POOL;
DROP TABLE LOGICAL_LIBRARY;
DROP TABLE MOUNT_POLICY;
Markdown is supported
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