LCOV - code coverage report
Current view: top level - otsdaq/tools - otsdaq_fix_new_table_fields_tool.cc (source / functions) Coverage Total Hit
Test: mu2edaq.info.cleaned Lines: 0.0 % 380 0
Test Date: 2026-07-30 01:56:44 Functions: 0.0 % 73 0

            Line data    Source code
       1              : #include <dirent.h>
       2              : #include <cassert>
       3              : #include <iostream>
       4              : #include <memory>
       5              : #include <string>
       6              : 
       7              : #include "otsdaq/ConfigurationInterface/ConfigurationInterface.h"
       8              : #include "otsdaq/ConfigurationInterface/ConfigurationManagerRW.h"
       9              : // #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h"
      10              : // #include "artdaq-database/JsonDocument/JSONDocument.h"
      11              : 
      12              : // Shared test utilities
      13              : #include "otsdaq/Macros/TestUtilities.h"
      14              : 
      15              : /// usage:
      16              : /// otsdaq_flatten_system_aliases <baseFlatVersion> <pathToSwapIn (optional)>
      17              : ///
      18              : /// if baseFlatVersion is invalid or temporary nothing is saved in the new db
      19              : ///     (Note: this can be used to swap dbs using pathToSwapIn)
      20              : ///
      21              : using namespace ots;
      22              : 
      23            0 : void FixNewTableFields(int argc, char* argv[])
      24              : {
      25            0 :         std::cout << "=================================================\n";
      26            0 :         std::cout << "=================================================\n";
      27            0 :         std::cout << "=================================================\n";
      28            0 :         __COUT__ << "\nFixing new table fields!" << __E__;
      29              : 
      30              :         std::cout << "\n\nusage: Two arguments:\n\t <pathToSwapIn (optional)> \n\n"
      31            0 :                   << "\t Default values: pathToSwapIn = \"\" \n\n"
      32            0 :                   << __E__;
      33              : 
      34              :         std::cout << "\n\nNote: This assumes artdaq db file type interface. "
      35              :                   << "The current database/ will be moved to database_<linuxtime>/ "
      36              :                   << "unless a pathToSwapIn is specified, in which case the path will "
      37            0 :                   << "be copied overwriting database/ \n\n"
      38            0 :                   << __E__;
      39              : 
      40            0 :         std::cout << "argc = " << argc << __E__;
      41            0 :         for(int i = 0; i < argc; i++)
      42            0 :                 std::cout << "argv[" << i << "] = " << argv[i] << __E__;
      43              : 
      44            0 :         if(argc > 2)
      45              :         {
      46            0 :                 std::cout << "Error! Must provide at most one parameter.\n\n" << __E__;
      47            0 :                 return;
      48              :         }
      49              : 
      50              :         // determine if "h"elp was first parameter
      51            0 :         std::string pathToSwapIn = "";
      52            0 :         if(argc >= 2)
      53            0 :                 pathToSwapIn = argv[1];
      54              : 
      55            0 :         if(pathToSwapIn == "-h" || pathToSwapIn == "--help")
      56              :         {
      57              :                 std::cout
      58            0 :                     << "Recognized parameter 1 as a 'help' option. Usage was printed. Exiting."
      59            0 :                     << __E__;
      60            0 :                 return;
      61              :         }
      62              : 
      63            0 :         __COUTV__(pathToSwapIn);
      64              : 
      65              :         // return;
      66              : 
      67              :         //==============================================================================
      68              :         // Define environment variables
      69              :         //      Note: normally these environment variables are set by ots script
      70              : 
      71            0 :         test::util::check_and_make_envs();
      72              :         ////////////////////////////////////////////////////
      73              : 
      74              :         //==============================================================================
      75              :         // get prepared with initial source db
      76              : 
      77              :         // ConfigurationManager instance immediately loads active groups
      78            0 :         __COUT__ << "Loading active Aliases..." << __E__;
      79            0 :         ConfigurationManagerRW  cfgMgrInst("flatten_admin");
      80            0 :         ConfigurationManagerRW* cfgMgr = &cfgMgrInst;
      81              : 
      82              :         {
      83            0 :                 std::string accumulatedWarnings;
      84            0 :                 cfgMgr->restoreActiveTableGroups(false /*throwErrors*/,
      85              :                                                  "" /*pathToActiveGroupsFile*/,
      86              :                                                  ConfigurationManager::LoadGroupType::ALL_TYPES,
      87              :                                                  &accumulatedWarnings);
      88              : 
      89            0 :                 __COUT__ << "Done Loading active groups: \n" << accumulatedWarnings << std::endl;
      90            0 :         }
      91              : 
      92              :         // create set of groups to persist
      93              :         //      include active context
      94              :         //      include active backbone
      95              :         //      include active iterate group
      96              :         //      include active config group
      97              :         //              (keep key translation separate activeGroupKeys)
      98              :         //      include all groups with system aliases
      99              : 
     100              :         // for group in set
     101              :         //      load/activate group and flatten tables to flatVersion to new DB
     102              :         //              save new version to modifiedTables
     103              :         //      save group with flatVersion key to new DB
     104              :         //              save new key to groupSet
     105              :         //      ++flatVersion
     106              : 
     107              :         // reload the active backbone (using activeGroupKeys)
     108              :         //      modify group aliases and table aliases properly based on groupSet and
     109              :         // modifiedTables       save new backbone with flatVersion to new DB
     110              : 
     111              :         // backup the file ConfigurationManager::ACTIVE_GROUPS_FILENAME with time
     112              :         //      and change the ConfigurationManager::ACTIVE_GROUPS_FILENAME
     113              :         //      to reflect new group names/keys
     114              : 
     115              :         /* map<<groupName, origKey>, newKey> */
     116            0 :         std::map<std::pair<std::string, TableGroupKey>, TableGroupKey> groupSet;
     117              :         /* <tableName, <origVersion, newVersion> >*/
     118            0 :         std::map<std::pair<std::string, TableVersion>, TableVersion>   modifiedTables;
     119            0 :         std::map<std::string, std::pair<TableGroupKey, TableGroupKey>> activeGroupKeys;
     120            0 :         std::map<std::pair<std::string, TableGroupKey>, std::string>   groupErrors;
     121              : 
     122            0 :         std::string activeBackboneGroupName = "";
     123            0 :         std::string activeContextGroupName  = "";
     124            0 :         std::string activeIterateGroupName  = "";
     125            0 :         std::string activeConfigGroupName   = "";
     126              : 
     127            0 :         std::string nowTime = std::to_string(time(0));
     128              : 
     129            0 :         std::string thenTime = "";
     130            0 :         if(pathToSwapIn != "")  // get target then time
     131              :         {
     132            0 :                 thenTime = pathToSwapIn.substr(pathToSwapIn.rfind('_') + 1);
     133            0 :                 __COUT__ << "thenTime = " << thenTime << __E__;
     134              :                 // return;
     135              :         }
     136              : 
     137              :         // add active groups to set
     138              :         std::map<std::string, std::pair<std::string, TableGroupKey>> activeGroupsMap =
     139            0 :             cfgMgr->getActiveTableGroups();
     140              : 
     141            0 :         for(const auto& activeGroup : activeGroupsMap)
     142              :         {
     143            0 :                 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>, TableGroupKey>(
     144            0 :                     std::pair<std::string, TableGroupKey>(activeGroup.second.first,
     145            0 :                                                           activeGroup.second.second),
     146            0 :                     TableGroupKey()));
     147            0 :                 activeGroupKeys.insert(
     148            0 :                     std::pair<std::string, std::pair<TableGroupKey, TableGroupKey>>(
     149            0 :                         activeGroup.second.first,
     150            0 :                         std::pair<TableGroupKey, TableGroupKey>(activeGroup.second.second,
     151            0 :                                                                 TableGroupKey())));
     152              : 
     153            0 :                 if(activeGroup.first == ConfigurationManager::GROUP_TYPE_NAME_BACKBONE)
     154              :                 {
     155            0 :                         activeBackboneGroupName = activeGroup.second.first;
     156            0 :                         __COUT__ << "found activeBackboneGroupName = " << activeBackboneGroupName
     157            0 :                                  << __E__;
     158              :                 }
     159            0 :                 else if(activeGroup.first == ConfigurationManager::GROUP_TYPE_NAME_CONTEXT)
     160              :                 {
     161            0 :                         activeContextGroupName = activeGroup.second.first;
     162            0 :                         __COUT__ << "found activeContextGroupName = " << activeContextGroupName
     163            0 :                                  << __E__;
     164              :                 }
     165            0 :                 else if(activeGroup.first == ConfigurationManager::GROUP_TYPE_NAME_ITERATE)
     166              :                 {
     167            0 :                         activeIterateGroupName = activeGroup.second.first;
     168            0 :                         __COUT__ << "found activeIterateGroupName = " << activeIterateGroupName
     169            0 :                                  << __E__;
     170              :                 }
     171            0 :                 else if(activeGroup.first == ConfigurationManager::GROUP_TYPE_NAME_CONFIGURATION)
     172              :                 {
     173            0 :                         activeConfigGroupName = activeGroup.second.first;
     174            0 :                         __COUT__ << "found activeConfigGroupName = " << activeConfigGroupName
     175            0 :                                  << __E__;
     176              :                 }
     177              :         }
     178              : 
     179              :         // add system alias groups to set
     180              :         const std::string groupAliasesTableName =
     181            0 :             ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
     182            0 :         std::map<std::string, TableVersion> activeVersions = cfgMgr->getActiveVersions();
     183            0 :         if(activeVersions.find(groupAliasesTableName) == activeVersions.end())
     184              :         {
     185            0 :                 __SS__ << "\nActive version of " << groupAliasesTableName << " missing! "
     186              :                        << groupAliasesTableName
     187              :                        << " is a required member of the Backbone configuration group."
     188            0 :                        << "\n\nLikely you need to activate a valid Backbone group." << __E__;
     189            0 :                 __SS_THROW__;
     190            0 :         }
     191              : 
     192              :         std::vector<std::pair<std::string, ConfigurationTree>> aliasNodePairs =
     193            0 :             cfgMgr->getNode(groupAliasesTableName).getChildren();
     194            0 :         for(auto& groupPair : aliasNodePairs)
     195            0 :                 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>, TableGroupKey>(
     196            0 :                     std::pair<std::string, TableGroupKey>(
     197            0 :                         groupPair.second.getNode("GroupName").getValueAsString(),
     198            0 :                         TableGroupKey(groupPair.second.getNode("GroupKey").getValueAsString())),
     199            0 :                     TableGroupKey()));
     200              : 
     201            0 :         __COUT__ << "Identified groups:" << __E__;
     202            0 :         for(auto& group : groupSet)
     203            0 :                 __COUT__ << group.first.first << " " << group.first.second << __E__;
     204            0 :         __COUT__ << __E__;
     205            0 :         __COUT__ << __E__;
     206              : 
     207              :         // return;
     208              :         //==============================================================================
     209              :         // prepare to manipulate directories
     210            0 :         std::string currentDir = __ENV__("ARTDAQ_DATABASE_URI");
     211              : 
     212            0 :         if(currentDir.find("filesystemdb://") != 0)
     213              :         {
     214            0 :                 __SS__ << "filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << __E__;
     215            0 :                 __SS_THROW__;
     216            0 :         }
     217              : 
     218            0 :         currentDir = currentDir.substr(std::string("filesystemdb://").length());
     219            0 :         while(currentDir.length() &&
     220            0 :               currentDir[currentDir.length() - 1] == '/')  // remove trailing '/'s
     221            0 :                 currentDir = currentDir.substr(0, currentDir.length() - 1);
     222            0 :         std::string moveToDir = currentDir + "_" + nowTime;
     223              : 
     224            0 :         if(pathToSwapIn != "")
     225              :         {
     226              :                 DIR* dp;
     227            0 :                 if((dp = opendir(pathToSwapIn.c_str())) == 0)
     228              :                 {
     229            0 :                         __COUT__ << "ERROR:(" << errno << ").  Can't open directory: " << pathToSwapIn
     230            0 :                                  << __E__;
     231            0 :                         exit(0);
     232              :                 }
     233            0 :                 closedir(dp);
     234              :         }
     235              : 
     236              :         // handle directory swap
     237            0 :         __COUT__ << "Copying current directory: \t" << currentDir << __E__;
     238            0 :         __COUT__ << "\t... to: \t\t" << moveToDir << __E__;
     239              :         // return;
     240            0 :         rename(currentDir.c_str(), moveToDir.c_str());
     241              : 
     242            0 :         if(pathToSwapIn != "")  // move the swap in directory in
     243              :         {
     244            0 :                 __COUT__ << "Swapping in directory: \t" << pathToSwapIn << __E__;
     245            0 :                 __COUT__ << "\t.. to: \t\t" << currentDir << __E__;
     246            0 :                 rename(pathToSwapIn.c_str(), currentDir.c_str());
     247              : 
     248              :                 // also swap in active groups file
     249              :                 // check if original active file exists
     250              :                 std::string activeGroupsFile =
     251            0 :                     ConfigurationManager::ACTIVE_GROUPS_FILENAME + "." + thenTime;
     252            0 :                 FILE* fp = fopen(activeGroupsFile.c_str(), "r");
     253            0 :                 if(fp)
     254              :                 {
     255            0 :                         __COUT__ << "Swapping active groups file: \t" << activeGroupsFile << __E__;
     256            0 :                         __COUT__ << "\t.. to: \t\t" << ConfigurationManager::ACTIVE_GROUPS_FILENAME
     257            0 :                                  << __E__;
     258            0 :                         rename(activeGroupsFile.c_str(),
     259              :                                ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str());
     260              :                 }
     261              : 
     262            0 :                 __COUT__ << "Path swapped in. Done." << __E__;
     263            0 :                 return;
     264            0 :         }
     265              :         else  // copy the bkup back
     266            0 :                 std::system(("cp -r " + moveToDir + " " + currentDir).c_str());
     267              : 
     268              :         // return;
     269              : 
     270              :         //=============
     271              :         // now ready to save each member table of active groups
     272              :         //      to new version fixing column names.
     273              : 
     274              :         // int flatVersion = 0;
     275              : 
     276              :         // ConfigurationInterface* theInterface_ = ConfigurationInterface::getInstance(false);
     277              :         // //true for File interface, false for artdaq database;
     278              :         TableView* cfgView;
     279              :         TableBase* config;
     280              : 
     281            0 :         bool        errDetected      = false;
     282            0 :         std::string accumulateErrors = "";
     283              : 
     284            0 :         std::map<std::string /*name*/, TableVersion>          memberMap;
     285            0 :         std::map<std::string /*name*/, std::string /*alias*/> groupAliases;
     286            0 :         std::string                                           groupComment;
     287              :         //      std::string                     groupAuthor;
     288              :         //      std::string                             groupCreateTime;
     289              :         //      time_t                                  groupCreateTime_t;
     290              :         //      TableBase*              groupMetadataTable = cfgMgr->getMetadataTable();
     291              : 
     292              :         //      //don't do anything more if flatVersion is not persistent
     293              :         //      if(TableVersion(flatVersion).isInvalid() ||
     294              :         //                      TableVersion(flatVersion).isTemporaryVersion())
     295              :         //      {
     296              :         //              __COUT__<< "\n\nflatVersion " << TableVersion(flatVersion) <<
     297              :         //                              " is an invalid or temporary version. Skipping to end!" << __E__;
     298              :         //              goto CLEAN_UP;
     299              :         //      }
     300              : 
     301            0 :         for(auto& groupPair : groupSet)
     302              :         {
     303            0 :                 errDetected = false;
     304              : 
     305            0 :                 __COUT__ << "****************************" << __E__;
     306            0 :                 __COUT__ << "Loading members for " << groupPair.first.first << "("
     307            0 :                          << groupPair.first.second << ")" << __E__;
     308              : 
     309              :                 //=========================
     310              :                 // load group, group metadata, and tables from original DB
     311              :                 try
     312              :                 {
     313            0 :                         cfgMgr->loadTableGroup(groupPair.first.first,
     314            0 :                                                groupPair.first.second,
     315              :                                                false /*doActivate*/,
     316              :                                                &memberMap /*memberMap*/,
     317              :                                                0 /*progressBar*/,
     318              :                                                &accumulateErrors,
     319              :                                                &groupComment,
     320              :                                                0,  //&groupAuthor,
     321              :                                                0,  //&groupCreateTime,
     322              :                                                false /*doNotLoadMember*/,
     323              :                                                0 /*groupTypeString*/,
     324              :                                                &groupAliases);
     325              :                 }
     326            0 :                 catch(std::runtime_error& e)
     327              :                 {
     328            0 :                         __COUT__ << "Error was caught loading members for " << groupPair.first.first
     329            0 :                                  << "(" << groupPair.first.second << ")" << __E__;
     330            0 :                         __COUT__ << e.what() << __E__;
     331            0 :                         errDetected = true;
     332            0 :                 }
     333            0 :                 catch(...)
     334              :                 {
     335            0 :                         __COUT__ << "Error was caught loading members for " << groupPair.first.first
     336            0 :                                  << "(" << groupPair.first.second << ")" << __E__;
     337            0 :                         errDetected = true;
     338            0 :                 }
     339              : 
     340              :                 //=========================
     341              : 
     342              :                 // note error if any (loading) failure
     343            0 :                 if(errDetected)
     344              :                 {
     345              :                         // power on if group failed
     346              :                         //      and record error
     347              : 
     348            0 :                         groupErrors.insert(
     349            0 :                             std::pair<std::pair<std::string, TableGroupKey>, std::string>(
     350            0 :                                 std::pair<std::string, TableGroupKey>(groupPair.first.first,
     351            0 :                                                                       groupPair.first.second),
     352              :                                 "Error caught loading the group."));
     353            0 :                         continue;
     354              :                 }
     355              : 
     356              :                 //=========================
     357              :                 // save group and its tables with new key and versions!
     358              :                 try
     359              :                 {
     360            0 :                         __COUT__ << "Before member map: " << StringMacros::mapToString(memberMap)
     361            0 :                                  << __E__;
     362              : 
     363              :                         // saving tables
     364            0 :                         for(auto& memberPair : memberMap)
     365              :                         {
     366            0 :                                 __COUT__ << memberPair.first << ":v" << memberPair.second << __E__;
     367              : 
     368              :                                 // check if table has already been modified by a previous group
     369              :                                 //      (i.e. two groups using the same version of a table)
     370            0 :                                 if(modifiedTables.find(std::pair<std::string, TableVersion>(
     371            0 :                                        memberPair.first, memberPair.second)) != modifiedTables.end())
     372              :                                 {
     373            0 :                                         __COUT__ << "Table was already modified!" << __E__;
     374              :                                         memberPair.second =
     375            0 :                                             modifiedTables[std::pair<std::string, TableVersion>(
     376            0 :                                                 memberPair.first, memberPair.second)];
     377            0 :                                         __COUT__ << "\t to...\t" << memberPair.first << ":v"
     378            0 :                                                  << memberPair.second << __E__;
     379            0 :                                         continue;
     380            0 :                                 }
     381              : 
     382              :                                 // save new version, and then record new version in map
     383              : 
     384              :                                 // first copy to new column names
     385              :                                 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(
     386            0 :                                     memberPair.first /*table name*/, memberPair.second /*source version*/
     387            0 :                                 );
     388              : 
     389              :                                 // then save temporary to persistent version
     390              :                                 TableVersion persistentVersion = cfgMgr->saveNewTable(
     391            0 :                                     memberPair.first /*table name*/, temporaryVersion);
     392              : 
     393              :                                 // save new version to modifiedTables
     394            0 :                                 modifiedTables.insert(
     395            0 :                                     std::pair<std::pair<std::string, TableVersion>, TableVersion>(
     396            0 :                                         std::pair<std::string, TableVersion>(memberPair.first,
     397            0 :                                                                              memberPair.second),
     398              :                                         persistentVersion));
     399              : 
     400            0 :                                 memberPair.second = persistentVersion;  // change version in the member
     401              :                                                                         // map
     402              : 
     403            0 :                                 __COUT__ << "\t to...\t" << memberPair.first << ":v" << memberPair.second
     404            0 :                                          << __E__;
     405            0 :                         }  // end table member loop
     406              : 
     407              :                         // now save new group
     408            0 :                         __COUT__ << "After member map: " << StringMacros::mapToString(memberMap)
     409            0 :                                  << __E__;
     410              : 
     411              :                         // return;
     412              : 
     413              :                         TableGroupKey newGroupKey =
     414            0 :                             cfgMgr->saveNewTableGroup(groupPair.first.first /*groupName*/,
     415              :                                                       memberMap,
     416              :                                                       groupComment,
     417            0 :                                                       &groupAliases);
     418              : 
     419              :                         // and modify groupSet and activeGroupKeys keys
     420            0 :                         groupPair.second = TableGroupKey(newGroupKey);
     421              : 
     422              :                         // if this is an active group, save key change
     423            0 :                         if(activeGroupKeys.find(groupPair.first.first) != activeGroupKeys.end() &&
     424            0 :                            activeGroupKeys[groupPair.first.first].first == groupPair.first.second)
     425            0 :                                 activeGroupKeys[groupPair.first.first].second =
     426            0 :                                     TableGroupKey(newGroupKey);
     427            0 :                 }
     428            0 :                 catch(std::runtime_error& e)
     429              :                 {
     430            0 :                         __COUT__ << "Error was caught saving group " << groupPair.first.first << " ("
     431            0 :                                  << groupPair.first.second << ") " << __E__;
     432            0 :                         __COUT__ << e.what() << __E__;
     433              : 
     434            0 :                         groupErrors.insert(
     435            0 :                             std::pair<std::pair<std::string, TableGroupKey>, std::string>(
     436            0 :                                 std::pair<std::string, TableGroupKey>(groupPair.first.first,
     437            0 :                                                                       groupPair.first.second),
     438              :                                 "Error caught saving the group."));
     439            0 :                 }
     440            0 :                 catch(...)
     441              :                 {
     442            0 :                         __COUT__ << "Error was caught saving group " << groupPair.first.first << " ("
     443            0 :                                  << groupPair.first.second << ") " << __E__;
     444              : 
     445            0 :                         groupErrors.insert(
     446            0 :                             std::pair<std::pair<std::string, TableGroupKey>, std::string>(
     447            0 :                                 std::pair<std::string, TableGroupKey>(groupPair.first.first,
     448            0 :                                                                       groupPair.first.second),
     449              :                                 "Error caught saving the group."));
     450            0 :                 }
     451              :                 //=========================
     452              : 
     453              :         }  // end group loop
     454              : 
     455              :         // record in readme of moveToDir
     456              :         {
     457            0 :                 FILE* fp = fopen((moveToDir + "/README_fix_new_table_fields.txt").c_str(), "a");
     458            0 :                 if(!fp)
     459            0 :                         __COUT__ << "\tError opening README file!" << __E__;
     460              :                 else
     461              :                 {
     462              :                         time_t     rawtime;
     463              :                         struct tm* timeinfo;
     464              :                         char       buffer[200];
     465              : 
     466            0 :                         time(&rawtime);
     467            0 :                         timeinfo = localtime(&rawtime);
     468            0 :                         strftime(buffer, 200, "%b %d, %Y %I:%M%p %Z", timeinfo);
     469              : 
     470            0 :                         fprintf(fp,
     471              :                                 "This database %s \n\t is a backup of %s \n\t BEFORE forcing to new "
     472              :                                 "table fields \n\t and was created at this time \n\t %lu \t %s\n\n\n",
     473              :                                 currentDir.c_str(),
     474              :                                 moveToDir.c_str(),
     475              :                                 time(0),
     476              :                                 buffer);
     477              : 
     478            0 :                         fclose(fp);
     479              :                 }
     480              :         }
     481              : 
     482              :         // record in readme for currentDir
     483              :         {
     484            0 :                 FILE* fp = fopen((currentDir + "/README_otsdaq_flatten.txt").c_str(), "a");
     485              : 
     486            0 :                 if(!fp)
     487            0 :                         __COUT__ << "\tError opening README file!" << __E__;
     488              :                 else
     489              :                 {
     490              :                         time_t     rawtime;
     491              :                         struct tm* timeinfo;
     492              :                         char       buffer[200];
     493              : 
     494            0 :                         time(&rawtime);
     495            0 :                         timeinfo = localtime(&rawtime);
     496            0 :                         strftime(buffer, 200, "%b %d, %Y %I:%M:%S%p %Z", timeinfo);
     497              : 
     498            0 :                         fprintf(fp,
     499              :                                 "This database %s \n\t was forced to new table fields \n\t at this "
     500              :                                 "time \n\t %lu \t %s\n\n\n",
     501              :                                 currentDir.c_str(),
     502              :                                 time(0),
     503              :                                 buffer);
     504              : 
     505            0 :                         fclose(fp);
     506              :                 }
     507              :         }
     508              : 
     509              :         //      //print resulting all groups
     510              :         //
     511              :         //      __COUT__  << "Resulting Groups:" << __E__;
     512              :         //      for(const auto &group: groupSet)
     513              :         //              __COUT__<< group.first.first << ": " <<
     514              :         //                      group.first.second << " => " << group.second << __E__;
     515              :         //      __COUT__  << "Resulting Groups end." << __E__;
     516              :         //
     517              :         //
     518              :         //      //print resulting active groups
     519              :         //
     520              :         //      __COUT__  << "Resulting Active Groups:" << __E__;
     521              :         //      for(const auto &activeGroup: activeGroupKeys)
     522              :         //              __COUT__<< activeGroup.first << ": " <<
     523              :         //                      activeGroup.second.first << " => " << activeGroup.second.second << __E__;
     524              :         //
     525              :         //      __COUT__<< activeBackboneGroupName << " is the " <<
     526              :         //                      ConfigurationManager::GROUP_TYPE_NAME_BACKBONE << "." << __E__;
     527              :         //      __COUT__  << "Resulting Active Groups end." << __E__;
     528              : 
     529              :         // reload the active backbone (using activeGroupKeys)
     530              :         //      modify group aliases and table aliases properly based on groupSet and
     531              :         // modifiedTables       save new backbone with flatVersion to new DB
     532              : 
     533            0 :         if(activeBackboneGroupName == "")
     534              :         {
     535            0 :                 __COUT__ << "No active Backbone table identified." << __E__;
     536            0 :                 goto CLEAN_UP;
     537              :         }
     538              : 
     539            0 :         __COUT__ << "Modifying the active Backbone table to reflect new table versions and "
     540            0 :                     "group keys."
     541            0 :                  << __E__;
     542              : 
     543              :         {  // start active backbone group handling
     544              : 
     545            0 :                 cfgMgr->loadTableGroup(activeBackboneGroupName,
     546            0 :                                        activeGroupKeys[activeBackboneGroupName].second,
     547              :                                        true /*doActivate*/,
     548              :                                        &memberMap,
     549              :                                        0 /*progressBar*/,
     550              :                                        &accumulateErrors,
     551              :                                        &groupComment);
     552              : 
     553              :                 // modify Group Aliases Table and Version Aliases Table to point
     554              :                 //      at DEFAULT and flatVersion respectively
     555              : 
     556              :                 const std::string groupAliasesName =
     557            0 :                     ConfigurationManager::GROUP_ALIASES_TABLE_NAME;
     558              :                 const std::string versionAliasesName =
     559            0 :                     ConfigurationManager::VERSION_ALIASES_TABLE_NAME;
     560              : 
     561            0 :                 std::map<std::string, TableVersion> activeMap = cfgMgr->getActiveVersions();
     562              : 
     563              :                 // modify Group Aliases Table
     564            0 :                 if(activeMap.find(groupAliasesName) != activeMap.end())
     565              :                 {
     566            0 :                         __COUT__ << "\n\nModifying " << groupAliasesName << __E__;
     567              : 
     568              :                         // now save new group
     569            0 :                         __COUT__ << "Before member map: " << StringMacros::mapToString(memberMap)
     570            0 :                                  << __E__;
     571              : 
     572              :                         // save new Group Aliases table and Version Aliases table
     573              :                         // first save new group aliases table
     574            0 :                         __COUT__ << groupAliasesName << ":v" << memberMap[groupAliasesName] << __E__;
     575              : 
     576              :                         // first copy to new column names
     577              :                         TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(
     578              :                             groupAliasesName /*table name*/,
     579            0 :                             memberMap[groupAliasesName] /*source version*/
     580            0 :                         );
     581              : 
     582            0 :                         config = cfgMgr->getTableByName(groupAliasesName);
     583            0 :                         config->setActiveView(temporaryVersion);
     584            0 :                         cfgView = config->getViewP();
     585              : 
     586            0 :                         unsigned int col1 = cfgView->findCol("GroupName");
     587            0 :                         unsigned int col2 = cfgView->findCol("GroupKey");
     588              : 
     589            0 :                         cfgView->print();
     590              : 
     591              :                         // change all key entries found to the new key and delete rows for groups not
     592              :                         // found
     593              :                         bool found;
     594            0 :                         for(unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
     595              :                         {
     596            0 :                                 found = false;
     597            0 :                                 for(const auto& group : groupSet)
     598            0 :                                         if(group.second.isInvalid())
     599            0 :                                                 continue;
     600            0 :                                         else if(cfgView->getDataView()[row][col1] == group.first.first &&
     601            0 :                                                 cfgView->getDataView()[row][col2] ==
     602            0 :                                                     group.first.second.toString())
     603              :                                         {
     604              :                                                 // found a matching group/key pair
     605            0 :                                                 __COUT__ << "Changing row " << row << " for "
     606            0 :                                                          << cfgView->getDataView()[row][col1]
     607            0 :                                                          << " key=" << cfgView->getDataView()[row][col2]
     608            0 :                                                          << " to NEW key=" << group.second << __E__;
     609            0 :                                                 cfgView->setValue(group.second.toString(), row, col2);
     610            0 :                                                 found = true;
     611            0 :                                                 break;
     612              :                                         }
     613              : 
     614            0 :                                 if(!found)  // delete row
     615            0 :                                         cfgView->deleteRow(row--);
     616              :                         }
     617              : 
     618            0 :                         cfgView->print();
     619              : 
     620              :                         // then save temporary to persistent version
     621              :                         TableVersion persistentVersion =
     622            0 :                             cfgMgr->saveNewTable(groupAliasesName /*table name*/, temporaryVersion);
     623              : 
     624              :                         //              //change the version of the active view to flatVersion and save it
     625              :                         //              config = cfgMgr->getTableByName(groupAliasesName);
     626              :                         //              cfgView = config->getViewP();
     627              :                         //              cfgView->setVersion(TableVersion(flatVersion));
     628              :                         //              theInterface_->saveActiveVersion(config);
     629              : 
     630            0 :                         memberMap[groupAliasesName] =
     631            0 :                             persistentVersion;  // change version in the member map
     632              : 
     633            0 :                         __COUT__ << "\t to...\t" << groupAliasesName << ":v"
     634            0 :                                  << memberMap[groupAliasesName] << __E__;
     635              : 
     636            0 :                 }  // done modifying group aliases
     637              : 
     638              :                 // modify Version Aliases Table
     639            0 :                 if(activeMap.find(versionAliasesName) != activeMap.end())
     640              :                 {
     641            0 :                         __COUT__ << "\n\nModifying " << versionAliasesName << __E__;
     642              : 
     643              :                         // first save new version aliases table
     644            0 :                         __COUT__ << versionAliasesName << ":v" << memberMap[versionAliasesName]
     645            0 :                                  << __E__;
     646              : 
     647              :                         // first copy to new column names
     648              :                         TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns(
     649              :                             versionAliasesName /*table name*/,
     650            0 :                             memberMap[versionAliasesName] /*source version*/
     651            0 :                         );
     652              : 
     653            0 :                         config = cfgMgr->getTableByName(versionAliasesName);
     654            0 :                         config->setActiveView(temporaryVersion);
     655            0 :                         cfgView           = config->getViewP();
     656            0 :                         unsigned int col1 = cfgView->findCol("TableName");
     657            0 :                         unsigned int col2 = cfgView->findCol("Version");
     658              : 
     659              :                         // change all version entries to the new version and delete rows with no match
     660              :                         bool found;
     661            0 :                         for(unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row)
     662              :                         {
     663            0 :                                 found = false;
     664            0 :                                 for(const auto& table : modifiedTables)
     665            0 :                                         if(cfgView->getDataView()[row][col1] == table.first.first &&
     666            0 :                                            cfgView->getDataView()[row][col2] == table.first.second.toString())
     667              :                                         {
     668              :                                                 // found a matching group/key pair
     669            0 :                                                 __COUT__ << "Changing row " << row << " for "
     670            0 :                                                          << cfgView->getDataView()[row][col1]
     671            0 :                                                          << " version=" << cfgView->getDataView()[row][col2]
     672            0 :                                                          << " to NEW version=" << table.second << __E__;
     673            0 :                                                 cfgView->setValue(table.second.toString(), row, col2);
     674            0 :                                                 found = true;
     675            0 :                                                 break;
     676              :                                         }
     677              : 
     678            0 :                                 if(!found)  // delete row
     679            0 :                                         cfgView->deleteRow(row--);
     680              :                         }
     681              : 
     682              :                         // then save temporary to persistent version
     683              :                         TableVersion persistentVersion =
     684            0 :                             cfgMgr->saveNewTable(versionAliasesName /*table name*/, temporaryVersion);
     685              : 
     686            0 :                         memberMap[versionAliasesName] =
     687            0 :                             persistentVersion;  // change version in the member map
     688              : 
     689            0 :                         __COUT__ << "\t to...\t" << versionAliasesName << ":v"
     690            0 :                                  << memberMap[versionAliasesName] << __E__;
     691              : 
     692            0 :                 }  // done modifying version aliases
     693              : 
     694              :                 // now save new group
     695            0 :                 __COUT__ << "After member map: " << StringMacros::mapToString(memberMap) << __E__;
     696              : 
     697              :                 TableGroupKey newGroupKey = cfgMgr->saveNewTableGroup(
     698              :                     activeBackboneGroupName /*groupName*/,
     699              :                     memberMap,
     700              :                     groupComment,
     701            0 :                     0 /*groupAliases*/);  // Do we need groupAliases for backbone here?
     702              : 
     703            0 :                 activeGroupKeys[activeBackboneGroupName].second = TableGroupKey(newGroupKey);
     704              : 
     705            0 :                 __COUT__ << "New to-be-active backbone group " << activeBackboneGroupName << ":v"
     706            0 :                          << activeGroupKeys[activeBackboneGroupName].second << __E__;
     707            0 :         }  // end active backbone group handling
     708              : 
     709              :         // backup the file ConfigurationManager::ACTIVE_GROUPS_FILENAME with time
     710              :         //      and change the ConfigurationManager::ACTIVE_GROUPS_FILENAME
     711              :         //      to reflect new group names/keys
     712              : 
     713              :         {
     714            0 :                 __COUT__ << "Manipulating the Active Groups file..." << __E__;
     715              : 
     716              :                 // check if original active file exists
     717            0 :                 FILE* fp = fopen(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(), "r");
     718            0 :                 if(!fp)
     719              :                 {
     720            0 :                         __SS__ << "Original active groups file '"
     721            0 :                                << ConfigurationManager::ACTIVE_GROUPS_FILENAME << "' not found."
     722            0 :                                << __E__;
     723            0 :                         goto CLEAN_UP;
     724            0 :                 }
     725              : 
     726            0 :                 __COUT__ << "Backing up file: " << ConfigurationManager::ACTIVE_GROUPS_FILENAME
     727            0 :                          << __E__;
     728              : 
     729            0 :                 fclose(fp);
     730              : 
     731              :                 std::string renameFile =
     732            0 :                     ConfigurationManager::ACTIVE_GROUPS_FILENAME + "." + nowTime;
     733            0 :                 rename(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(), renameFile.c_str());
     734              : 
     735            0 :                 __COUT__ << "Backup file name: " << renameFile << __E__;
     736              : 
     737              :                 TableGroupKey *theConfigurationTableGroupKey_, *theContextTableGroupKey_,
     738              :                     *theBackboneTableGroupKey_, *theIterateTableGroupKey_;
     739            0 :                 std::string theConfigurationTableGroup_, theContextTableGroup_,
     740            0 :                     theBackboneTableGroup_, theIterateTableGroup_;
     741              : 
     742            0 :                 theConfigurationTableGroup_    = activeConfigGroupName;
     743            0 :                 theConfigurationTableGroupKey_ = &(activeGroupKeys[activeConfigGroupName].second);
     744              : 
     745            0 :                 theContextTableGroup_    = activeContextGroupName;
     746            0 :                 theContextTableGroupKey_ = &(activeGroupKeys[activeContextGroupName].second);
     747              : 
     748            0 :                 theBackboneTableGroup_    = activeBackboneGroupName;
     749            0 :                 theBackboneTableGroupKey_ = &(activeGroupKeys[activeBackboneGroupName].second);
     750              : 
     751            0 :                 theIterateTableGroup_    = activeIterateGroupName;
     752            0 :                 theIterateTableGroupKey_ = &(activeGroupKeys[activeIterateGroupName].second);
     753              : 
     754              :                 // the following is copied from ConfigurationManagerRW::activateTableGroup
     755              :                 {
     756            0 :                         __COUT__ << "Updating persistent active groups to "
     757            0 :                                  << ConfigurationManager::ACTIVE_GROUPS_FILENAME << " ..." << __E__;
     758              : 
     759            0 :                         std::string fn = ConfigurationManager::ACTIVE_GROUPS_FILENAME;
     760            0 :                         FILE*       fp = fopen(fn.c_str(), "w");
     761            0 :                         if(!fp)
     762            0 :                                 return;
     763              : 
     764            0 :                         fprintf(fp, "%s\n", theContextTableGroup_.c_str());
     765            0 :                         fprintf(fp,
     766              :                                 "%s\n",
     767              :                                 theContextTableGroupKey_
     768            0 :                                     ? theContextTableGroupKey_->toString().c_str()
     769              :                                     : "-1");
     770            0 :                         fprintf(fp, "%s\n", theBackboneTableGroup_.c_str());
     771            0 :                         fprintf(fp,
     772              :                                 "%s\n",
     773              :                                 theBackboneTableGroupKey_
     774            0 :                                     ? theBackboneTableGroupKey_->toString().c_str()
     775              :                                     : "-1");
     776            0 :                         fprintf(fp, "%s\n", theConfigurationTableGroup_.c_str());
     777            0 :                         fprintf(fp,
     778              :                                 "%s\n",
     779              :                                 theConfigurationTableGroupKey_
     780            0 :                                     ? theConfigurationTableGroupKey_->toString().c_str()
     781              :                                     : "-1");
     782            0 :                         fprintf(fp, "%s\n", theIterateTableGroup_.c_str());
     783            0 :                         fprintf(fp,
     784              :                                 "%s\n",
     785              :                                 theIterateTableGroupKey_
     786            0 :                                     ? theIterateTableGroupKey_->toString().c_str()
     787              :                                     : "-1");
     788            0 :                         fclose(fp);
     789            0 :                 }
     790            0 :         }
     791              : 
     792              :         // print resulting all groups
     793              : 
     794            0 :         __COUT__ << "Resulting Groups:" << __E__;
     795            0 :         for(const auto& group : groupSet)
     796            0 :                 __COUT__ << "\t" << group.first.first << ": " << group.first.second << " => "
     797            0 :                          << group.second << __E__;
     798            0 :         __COUT__ << "Resulting Groups end." << __E__;
     799              : 
     800              :         // print resulting active groups
     801              : 
     802            0 :         __COUT__ << "Resulting Active Groups:" << __E__;
     803            0 :         for(const auto& activeGroup : activeGroupKeys)
     804            0 :                 __COUT__ << "\t" << activeGroup.first << ": " << activeGroup.second.first
     805            0 :                          << " => " << activeGroup.second.second << __E__;
     806              : 
     807            0 :         __COUT__ << activeBackboneGroupName << " is the "
     808            0 :                  << ConfigurationManager::GROUP_TYPE_NAME_BACKBONE << "." << __E__;
     809            0 :         __COUT__ << "Resulting Active Groups end." << __E__;
     810              : 
     811            0 : CLEAN_UP:
     812              :         //==============================================================================
     813            0 :         __COUT__ << "End of Flattening Active Table Groups!\n\n\n" << __E__;
     814              : 
     815            0 :         __COUT__ << "****************************" << __E__;
     816            0 :         __COUT__ << "There were " << groupSet.size() << " groups considered, and there were "
     817            0 :                  << groupErrors.size() << " errors found handling those groups." << __E__;
     818            0 :         __COUT__ << "The following errors were found handling the groups:" << __E__;
     819            0 :         for(auto& groupErr : groupErrors)
     820            0 :                 __COUT__ << "\t" << groupErr.first.first << " " << groupErr.first.second << ": \t"
     821            0 :                          << groupErr.second << __E__;
     822            0 :         __COUT__ << "End of errors.\n\n" << __E__;
     823              : 
     824            0 :         __COUT__ << "Run the following to return to your previous database structure:"
     825            0 :                  << __E__;
     826            0 :         __COUT__ << "\t otsdaq_fix_new_table_fiels " << moveToDir << "\n\n" << __E__;
     827              : 
     828            0 :         return;
     829            0 : }  // end FixNewTableFields()
     830              : 
     831            0 : int main(int argc, char* argv[])
     832              : {
     833            0 :         FixNewTableFields(argc, argv);
     834            0 :         return 0;
     835              : }
     836              : // BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1