comparison OrthancServer/main.cpp @ 2299:03982a0db696

Fix issue #29 (more consistent handling of the "--upgrade" argument)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Jul 2017 15:52:47 +0200
parents 5465cab476cf
children 3ab96768d144
comparison
equal deleted inserted replaced
2298:bf91a1c36802 2299:03982a0db696
879 879
880 return StartDicomServer(context, restApi, plugins); 880 return StartDicomServer(context, restApi, plugins);
881 } 881 }
882 882
883 883
884 static bool UpgradeDatabase(IDatabaseWrapper& database, 884 static void UpgradeDatabase(IDatabaseWrapper& database,
885 IStorageArea& storageArea, 885 IStorageArea& storageArea)
886 bool allowDatabaseUpgrade)
887 { 886 {
888 // Upgrade the schema of the database, if needed 887 // Upgrade the schema of the database, if needed
889 unsigned int currentVersion = database.GetDatabaseVersion(); 888 unsigned int currentVersion = database.GetDatabaseVersion();
889
890 LOG(WARNING) << "Starting the upgrade of the database schema";
891 LOG(WARNING) << "Current database version: " << currentVersion;
892 LOG(WARNING) << "Database version expected by Orthanc: " << ORTHANC_DATABASE_VERSION;
893
890 if (currentVersion == ORTHANC_DATABASE_VERSION) 894 if (currentVersion == ORTHANC_DATABASE_VERSION)
891 { 895 {
892 return true; 896 LOG(WARNING) << "No upgrade is needed, start Orthanc without the \"--upgrade\" argument";
897 return;
893 } 898 }
894 899
895 if (currentVersion > ORTHANC_DATABASE_VERSION) 900 if (currentVersion > ORTHANC_DATABASE_VERSION)
896 { 901 {
897 LOG(ERROR) << "The version of the database schema (" << currentVersion 902 LOG(ERROR) << "The version of the database schema (" << currentVersion
898 << ") is too recent for this version of Orthanc. Please upgrade Orthanc."; 903 << ") is too recent for this version of Orthanc. Please upgrade Orthanc.";
899 return false; 904 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
900 }
901
902 if (!allowDatabaseUpgrade)
903 {
904 LOG(ERROR) << "The database schema must be upgraded from version "
905 << currentVersion << " to " << ORTHANC_DATABASE_VERSION
906 << ": Please run Orthanc with the \"--upgrade\" command-line option";
907 return false;
908 } 905 }
909 906
910 LOG(WARNING) << "Upgrading the database from schema version " 907 LOG(WARNING) << "Upgrading the database from schema version "
911 << currentVersion << " to " << ORTHANC_DATABASE_VERSION; 908 << currentVersion << " to " << ORTHANC_DATABASE_VERSION;
912 909
924 // Sanity check 921 // Sanity check
925 currentVersion = database.GetDatabaseVersion(); 922 currentVersion = database.GetDatabaseVersion();
926 if (ORTHANC_DATABASE_VERSION != currentVersion) 923 if (ORTHANC_DATABASE_VERSION != currentVersion)
927 { 924 {
928 LOG(ERROR) << "The database schema was not properly upgraded, it is still at version " << currentVersion; 925 LOG(ERROR) << "The database schema was not properly upgraded, it is still at version " << currentVersion;
929 throw OrthancException(ErrorCode_InternalError); 926 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
930 } 927 }
931 928 else
932 return true; 929 {
930 LOG(WARNING) << "The database schema was successfully upgraded, "
931 << "you can now start Orthanc without the \"--upgrade\" argument";
932 }
933 } 933 }
934 934
935 935
936 static bool ConfigureServerContext(IDatabaseWrapper& database, 936 static bool ConfigureServerContext(IDatabaseWrapper& database,
937 IStorageArea& storageArea, 937 IStorageArea& storageArea,
1013 1013
1014 1014
1015 static bool ConfigureDatabase(IDatabaseWrapper& database, 1015 static bool ConfigureDatabase(IDatabaseWrapper& database,
1016 IStorageArea& storageArea, 1016 IStorageArea& storageArea,
1017 OrthancPlugins *plugins, 1017 OrthancPlugins *plugins,
1018 bool allowDatabaseUpgrade) 1018 bool upgradeDatabase)
1019 { 1019 {
1020 database.Open(); 1020 database.Open();
1021
1022 unsigned int currentVersion = database.GetDatabaseVersion();
1021 1023
1022 if (!UpgradeDatabase(database, storageArea, allowDatabaseUpgrade)) 1024 if (upgradeDatabase)
1023 { 1025 {
1024 return false; 1026 UpgradeDatabase(database, storageArea);
1027 return false; // Stop and don't restart Orthanc (cf. issue 29)
1028 }
1029 else if (currentVersion != ORTHANC_DATABASE_VERSION)
1030 {
1031 LOG(ERROR) << "The database schema must be changed from version "
1032 << currentVersion << " to " << ORTHANC_DATABASE_VERSION
1033 << ": Please run Orthanc with the \"--upgrade\" argument";
1034 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
1025 } 1035 }
1026 1036
1027 bool success = ConfigureServerContext(database, storageArea, plugins); 1037 bool success = ConfigureServerContext(database, storageArea, plugins);
1028 1038
1029 database.Close(); 1039 database.Close();
1032 } 1042 }
1033 1043
1034 1044
1035 static bool ConfigurePlugins(int argc, 1045 static bool ConfigurePlugins(int argc,
1036 char* argv[], 1046 char* argv[],
1037 bool allowDatabaseUpgrade) 1047 bool upgradeDatabase)
1038 { 1048 {
1039 std::auto_ptr<IDatabaseWrapper> databasePtr; 1049 std::auto_ptr<IDatabaseWrapper> databasePtr;
1040 std::auto_ptr<IStorageArea> storage; 1050 std::auto_ptr<IStorageArea> storage;
1041 1051
1042 #if ORTHANC_ENABLE_PLUGINS == 1 1052 #if ORTHANC_ENABLE_PLUGINS == 1
1067 } 1077 }
1068 1078
1069 assert(database != NULL); 1079 assert(database != NULL);
1070 assert(storage.get() != NULL); 1080 assert(storage.get() != NULL);
1071 1081
1072 return ConfigureDatabase(*database, *storage, &plugins, allowDatabaseUpgrade); 1082 return ConfigureDatabase(*database, *storage, &plugins, upgradeDatabase);
1073 1083
1074 #elif ORTHANC_ENABLE_PLUGINS == 0 1084 #elif ORTHANC_ENABLE_PLUGINS == 0
1075 // The plugins are disabled 1085 // The plugins are disabled
1076 databasePtr.reset(Configuration::CreateDatabaseWrapper()); 1086 databasePtr.reset(Configuration::CreateDatabaseWrapper());
1077 storage.reset(Configuration::CreateStorageArea()); 1087 storage.reset(Configuration::CreateStorageArea());
1078 1088
1079 return ConfigureDatabase(*databasePtr, *storage, NULL, allowDatabaseUpgrade); 1089 return ConfigureDatabase(*databasePtr, *storage, NULL, upgradeDatabase);
1080 1090
1081 #else 1091 #else
1082 # error The macro ORTHANC_ENABLE_PLUGINS must be set to 0 or 1 1092 # error The macro ORTHANC_ENABLE_PLUGINS must be set to 0 or 1
1083 #endif 1093 #endif
1084 } 1094 }
1085 1095
1086 1096
1087 static bool StartOrthanc(int argc, 1097 static bool StartOrthanc(int argc,
1088 char* argv[], 1098 char* argv[],
1089 bool allowDatabaseUpgrade) 1099 bool upgradeDatabase)
1090 { 1100 {
1091 return ConfigurePlugins(argc, argv, allowDatabaseUpgrade); 1101 return ConfigurePlugins(argc, argv, upgradeDatabase);
1092 } 1102 }
1093 1103
1094 1104
1095 static bool DisplayPerformanceWarning() 1105 static bool DisplayPerformanceWarning()
1096 { 1106 {
1102 1112
1103 int main(int argc, char* argv[]) 1113 int main(int argc, char* argv[])
1104 { 1114 {
1105 Logging::Initialize(); 1115 Logging::Initialize();
1106 1116
1107 bool allowDatabaseUpgrade = false; 1117 bool upgradeDatabase = false;
1108 const char* configurationFile = NULL; 1118 const char* configurationFile = NULL;
1109 1119
1110 1120
1111 /** 1121 /**
1112 * Parse the command-line options. 1122 * Parse the command-line options.
1191 return -1; 1201 return -1;
1192 } 1202 }
1193 } 1203 }
1194 else if (argument == "--upgrade") 1204 else if (argument == "--upgrade")
1195 { 1205 {
1196 allowDatabaseUpgrade = true; 1206 upgradeDatabase = true;
1197 } 1207 }
1198 else if (boost::starts_with(argument, "--config=")) 1208 else if (boost::starts_with(argument, "--config="))
1199 { 1209 {
1200 // TODO WHAT IS THE ENCODING? 1210 // TODO WHAT IS THE ENCODING?
1201 std::string configurationSample; 1211 std::string configurationSample;
1256 { 1266 {
1257 for (;;) 1267 for (;;)
1258 { 1268 {
1259 OrthancInitialize(configurationFile); 1269 OrthancInitialize(configurationFile);
1260 1270
1261 bool restart = StartOrthanc(argc, argv, allowDatabaseUpgrade); 1271 bool restart = StartOrthanc(argc, argv, upgradeDatabase);
1262 if (restart) 1272 if (restart)
1263 { 1273 {
1264 OrthancFinalize(); 1274 OrthancFinalize();
1265 LOG(WARNING) << "Logging system is resetting"; 1275 LOG(WARNING) << "Logging system is resetting";
1266 Logging::Reset(); 1276 Logging::Reset();