SetProductionCmd.cpp 3.95 KB
Newer Older
1
/*
2
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2015-2021 CERN
4
5
6
7
 * @license        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.
8
 *
9
10
11
12
 *                 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.
13
 *
14
15
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 */

#include "SetProductionCmd.hpp"
#include "rdbms/Login.hpp"
#include "SchemaChecker.hpp"
#include "SetProductionCmdLineArgs.hpp"

namespace cta {
namespace catalogue {
  
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
SetProductionCmd::SetProductionCmd(
  std::istream &inStream,
  std::ostream &outStream,
  std::ostream &errStream):
  CmdLineTool(inStream, outStream, errStream) {
}

//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
SetProductionCmd::~SetProductionCmd() noexcept {
  
}

//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
int SetProductionCmd::exceptionThrowingMain(const int argc, char *const *const argv) {
  const SetProductionCmdLineArgs cmdLineArgs(argc, argv);

  if(cmdLineArgs.help) {
    printUsage(m_out);
    return 0;
  }
  
  const rdbms::Login dbLogin = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath);
  const uint64_t maxNbConns = 1;
  rdbms::ConnPool connPool(dbLogin, maxNbConns);
  auto conn = connPool.getConn();
  
  if(!isProductionSettable(dbLogin,conn)){
    throw cta::exception::Exception("Unable to set the catalogue as production because the column IS_PRODUCTION is missing");
  }
  
63
  m_out << "Setting the IS_PRODUCTION flag..." << std::endl;
64
  setProductionFlag(conn);
65
  m_out << "IS_PRODUCTION flag set." << std::endl;
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  
  return 0;
}

//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void SetProductionCmd::printUsage(std::ostream &os) {
  SetProductionCmdLineArgs::printUsage(os);
}

//------------------------------------------------------------------------------
// isProductionSettable
//------------------------------------------------------------------------------
bool SetProductionCmd::isProductionSettable(const cta::rdbms::Login & login, cta::rdbms::Conn & conn){
  //Check that the IS_PRODUCTION column is there
  cta::catalogue::SchemaChecker::Builder builder("catalogue",login.dbType,conn);
  auto schemaChecker = builder.build();
  cta::catalogue::SchemaCheckerResult res = schemaChecker->checkTableContainsColumns("CTA_CATALOGUE",{"IS_PRODUCTION"});
  return (res.getStatus() == cta::catalogue::SchemaCheckerResult::Status::SUCCESS);
}

//------------------------------------------------------------------------------
// setProductionFlag
//------------------------------------------------------------------------------
void SetProductionCmd::setProductionFlag(cta::rdbms::Conn& conn) {
  const char *  const sql = "UPDATE CTA_CATALOGUE SET IS_PRODUCTION='1'";
  try {
    conn.executeNonQuery(sql);
  } catch(const exception::Exception & ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}


}}