changeset 252:bd009f0b1931

db version
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Dec 2012 12:50:25 +0100
parents 4dc9d00c359c
children 15fcd925b05b
files Core/Enumerations.h Core/OrthancException.cpp NEWS OrthancServer/DatabaseWrapper.cpp OrthancServer/PrepareDatabase.sql OrthancServer/ServerEnumerations.h UnitTests/ServerIndex.cpp
diffstat 7 files changed, 63 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.h	Wed Dec 05 12:32:43 2012 +0100
+++ b/Core/Enumerations.h	Wed Dec 05 12:50:25 2012 +0100
@@ -54,7 +54,8 @@
     ErrorCode_CannotWriteFile,
     ErrorCode_BadFileFormat,
     ErrorCode_Timeout,
-    ErrorCode_UnknownResource
+    ErrorCode_UnknownResource,
+    ErrorCode_IncompatibleDatabaseVersion
   };
 
   enum PixelFormat
--- a/Core/OrthancException.cpp	Wed Dec 05 12:32:43 2012 +0100
+++ b/Core/OrthancException.cpp	Wed Dec 05 12:50:25 2012 +0100
@@ -51,48 +51,51 @@
   {
     switch (error)
     {
-    case ErrorCode_Success:
-      return "Success";
+      case ErrorCode_Success:
+        return "Success";
 
-    case ErrorCode_ParameterOutOfRange:
-      return "Parameter out of range";
+      case ErrorCode_ParameterOutOfRange:
+        return "Parameter out of range";
 
-    case ErrorCode_NotImplemented:
-      return "Not implemented yet";
+      case ErrorCode_NotImplemented:
+        return "Not implemented yet";
 
-    case ErrorCode_InternalError:
-      return "Internal error";
+      case ErrorCode_InternalError:
+        return "Internal error";
 
-    case ErrorCode_NotEnoughMemory:
-      return "Not enough memory";
+      case ErrorCode_NotEnoughMemory:
+        return "Not enough memory";
 
-    case ErrorCode_UriSyntax:
-      return "Badly formatted URI";
+      case ErrorCode_UriSyntax:
+        return "Badly formatted URI";
 
-    case ErrorCode_BadParameterType:
-      return "Bad type for a parameter";
+      case ErrorCode_BadParameterType:
+        return "Bad type for a parameter";
 
-    case ErrorCode_InexistentFile:
-      return "Inexistent file";
+      case ErrorCode_InexistentFile:
+        return "Inexistent file";
+
+      case ErrorCode_BadFileFormat:
+        return "Bad file format";
 
-    case ErrorCode_BadFileFormat:
-      return "Bad file format";
+      case ErrorCode_CannotWriteFile:
+        return "Cannot write to file";
 
-    case ErrorCode_CannotWriteFile:
-      return "Cannot write to file";
+      case ErrorCode_Timeout:
+        return "Timeout";
 
-    case ErrorCode_Timeout:
-      return "Timeout";
+      case ErrorCode_UnknownResource:
+        return "Unknown resource";
 
-    case ErrorCode_UnknownResource:
-      return "Unknown resource";
+      case ErrorCode_BadSequenceOfCalls:
+        return "Bad sequence of calls";
 
-    case ErrorCode_BadSequenceOfCalls:
-      return "Bad sequence of calls";
+      case ErrorCode_IncompatibleDatabaseVersion:
+        return "Incompatible version of the database";
 
-    case ErrorCode_Custom:
-    default:
-      return "???";
+      case ErrorCode_Custom:
+      default:
+        return "???";
     }
   }
 }
--- a/NEWS	Wed Dec 05 12:32:43 2012 +0100
+++ b/NEWS	Wed Dec 05 12:50:25 2012 +0100
@@ -2,6 +2,7 @@
 ===============================
 
 * Download archives of patients, studies and serie as ZIP files
+* Orthanc now checks the version of its database schema before starting
 
 
 Version 0.3.0 (2012/11/30)
--- a/OrthancServer/DatabaseWrapper.cpp	Wed Dec 05 12:32:43 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Wed Dec 05 12:50:25 2012 +0100
@@ -735,6 +735,27 @@
       db_.Execute(query);
     }
 
+    // Sanity check of the version of the database
+    std::string version = GetGlobalProperty(GlobalProperty_DatabaseSchemaVersion, "Unknown");
+    bool ok = false;
+    try
+    {
+      LOG(INFO) << "Version of the Orthanc database: " << version;
+      unsigned int v = boost::lexical_cast<unsigned int>(version);
+
+      // This version of Orthanc is only compatible with version 2 of
+      // the DB schema (since Orthanc 0.3.1)
+      ok = (v == 2); 
+    }
+    catch (boost::bad_lexical_cast&)
+    {
+    }
+
+    if (!ok)
+    {
+      throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
+    }
+
     signalRemainingAncestor_ = new Internals::SignalRemainingAncestor;
     db_.Register(signalRemainingAncestor_);
     db_.Register(new Internals::SignalFileDeleted(listener_));
--- a/OrthancServer/PrepareDatabase.sql	Wed Dec 05 12:32:43 2012 +0100
+++ b/OrthancServer/PrepareDatabase.sql	Wed Dec 05 12:50:25 2012 +0100
@@ -85,3 +85,7 @@
 BEGIN
   DELETE FROM Resources WHERE internalId = old.parentId;
 END;
+
+-- Set the version of the database schema
+-- The "1" corresponds to the "GlobalProperty_DatabaseSchemaVersion" enumeration
+INSERT INTO GlobalProperties VALUES (1, "2");
--- a/OrthancServer/ServerEnumerations.h	Wed Dec 05 12:32:43 2012 +0100
+++ b/OrthancServer/ServerEnumerations.h	Wed Dec 05 12:50:25 2012 +0100
@@ -59,7 +59,8 @@
 
   enum GlobalProperty
   {
-    GlobalProperty_FlushSleep = 1
+    GlobalProperty_DatabaseSchemaVersion = 1,
+    GlobalProperty_FlushSleep = 2
   };
 
   enum ResourceType
--- a/UnitTests/ServerIndex.cpp	Wed Dec 05 12:32:43 2012 +0100
+++ b/UnitTests/ServerIndex.cpp	Wed Dec 05 12:50:25 2012 +0100
@@ -180,7 +180,7 @@
   index.DeleteResource(a[5]);
   ASSERT_EQ(0u, index.GetTableRecordCount("Resources"));
   ASSERT_EQ(0u, index.GetTableRecordCount("AttachedFiles"));
-  ASSERT_EQ(1u, index.GetTableRecordCount("GlobalProperties"));
+  ASSERT_EQ(2u, index.GetTableRecordCount("GlobalProperties"));
 
   ASSERT_EQ(3u, listener.deletedFiles_.size());
   ASSERT_FALSE(listener.deletedFiles_.find("world") == listener.deletedFiles_.end());