changeset 694:72dc919a028c

upgrade database from v3 to v4
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Feb 2014 13:26:19 +0100
parents 01d8611c4a60
children c59bc408fb10
files CMakeLists.txt Core/SQLite/FunctionContext.cpp Core/SQLite/FunctionContext.h NEWS OrthancServer/DatabaseWrapper.cpp OrthancServer/PrepareDatabase.sql OrthancServer/Upgrade3To4.sql
diffstat 7 files changed, 66 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Tue Feb 04 17:52:51 2014 +0100
+++ b/CMakeLists.txt	Wed Feb 05 13:26:19 2014 +0100
@@ -93,7 +93,7 @@
 # Prepare the embedded files
 set(EMBEDDED_FILES
   PREPARE_DATABASE ${CMAKE_CURRENT_SOURCE_DIR}/OrthancServer/PrepareDatabase.sql
-  UPGRADE_3_TO_4 ${CMAKE_CURRENT_SOURCE_DIR}/OrthancServer/Upgrade3To4.sql
+  UPGRADE_DATABASE_3_TO_4 ${CMAKE_CURRENT_SOURCE_DIR}/OrthancServer/Upgrade3To4.sql
   CONFIGURATION_SAMPLE ${CMAKE_CURRENT_SOURCE_DIR}/Resources/Configuration.json
   LUA_TOOLBOX ${CMAKE_CURRENT_SOURCE_DIR}/Resources/Toolbox.lua
   )
--- a/Core/SQLite/FunctionContext.cpp	Tue Feb 04 17:52:51 2014 +0100
+++ b/Core/SQLite/FunctionContext.cpp	Wed Feb 05 13:26:19 2014 +0100
@@ -89,6 +89,12 @@
       CheckIndex(index);
       return std::string(reinterpret_cast<const char*>(sqlite3_value_text(argv_[index])));
     }
+
+    bool FunctionContext::IsNullValue(unsigned int index) const
+    {
+      CheckIndex(index);
+      return sqlite3_value_type(argv_[index]) == SQLITE_NULL;
+    }
   
     void FunctionContext::SetNullResult()
     {
--- a/Core/SQLite/FunctionContext.h	Tue Feb 04 17:52:51 2014 +0100
+++ b/Core/SQLite/FunctionContext.h	Wed Feb 05 13:26:19 2014 +0100
@@ -74,6 +74,8 @@
       double GetDoubleValue(unsigned int index) const;
 
       std::string GetStringValue(unsigned int index) const;
+
+      bool IsNullValue(unsigned int index) const;
   
       void SetNullResult();
 
--- a/NEWS	Tue Feb 04 17:52:51 2014 +0100
+++ b/NEWS	Wed Feb 05 13:26:19 2014 +0100
@@ -4,9 +4,10 @@
 * AET comparison is now case-insensitive by default
 * Possibility to disable the HTTP server or the DICOM server
 * Recover pixel data for more transfer syntaxes (notably JPEG)
+* Automatic computation of MD5 hashes for the stored DICOM files
 * Maintenance tool to recover DICOM files compressed by Orthanc
 * The newline characters in the configuration file are fixed for Linux
-* Capture of the SIGTERM signal
+* Capture of the SIGTERM signal in Linux
 
 
 Version 0.7.2 (2013/11/08)
--- a/OrthancServer/DatabaseWrapper.cpp	Tue Feb 04 17:52:51 2014 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Wed Feb 05 13:26:19 2014 +0100
@@ -67,13 +67,25 @@
 
       virtual void Compute(SQLite::FunctionContext& context)
       {
+        std::string uncompressedMD5, compressedMD5;
+
+        if (!context.IsNullValue(5))
+        {
+          uncompressedMD5 = context.GetStringValue(5);
+        }
+
+        if (!context.IsNullValue(6))
+        {
+          compressedMD5 = context.GetStringValue(6);
+        }
+
         FileInfo info(context.GetStringValue(0),
                       static_cast<FileContentType>(context.GetIntValue(1)),
                       static_cast<uint64_t>(context.GetInt64Value(2)),
-                      context.GetStringValue(5),
+                      uncompressedMD5,
                       static_cast<CompressionType>(context.GetIntValue(3)),
                       static_cast<uint64_t>(context.GetInt64Value(4)),
-                      context.GetStringValue(6));
+                      compressedMD5);
         
         listener_.SignalFileDeleted(info);
       }
@@ -826,11 +838,24 @@
       LOG(INFO) << "Version of the Orthanc database: " << version;
       unsigned int v = boost::lexical_cast<unsigned int>(version);
 
-      // Version 3: from Orthanc 0.3.2 to Orthanc 0.7.2 (inclusive)
-      // Version 4: from Orthanc 0.7.3 (inclusive)
+      /**
+       * History of the database versions:
+       *  - Version 3: from Orthanc 0.3.2 to Orthanc 0.7.2 (inclusive)
+       *  - Version 4: from Orthanc 0.7.3 (inclusive)
+       **/
+
+      // This version of Orthanc is only compatible with versions 3 of 4 of the DB schema
+      ok = (v == 3 || v == 4);
 
-      // This version of Orthanc is only compatible with version 4 of the DB schema
-      ok = (v == 4);
+      if (v == 3)
+      {
+        LOG(WARNING) << "Upgrading database version from 3 to 4";
+        std::string upgrade;
+        EmbeddedResources::GetFileResource(upgrade, EmbeddedResources::UPGRADE_DATABASE_3_TO_4);
+        db_.BeginTransaction();
+        db_.Execute(upgrade);
+        db_.CommitTransaction();
+      }
     }
     catch (boost::bad_lexical_cast&)
     {
--- a/OrthancServer/PrepareDatabase.sql	Tue Feb 04 17:52:51 2014 +0100
+++ b/OrthancServer/PrepareDatabase.sql	Wed Feb 05 13:26:19 2014 +0100
@@ -78,6 +78,7 @@
 BEGIN
   SELECT SignalFileDeleted(old.uuid, old.fileType, old.uncompressedSize, 
                            old.compressionType, old.compressedSize,
+                           -- These 2 arguments are new in Orthanc 0.7.3 (database v4)
                            old.uncompressedMD5, old.compressedMD5);
 END;
 
--- a/OrthancServer/Upgrade3To4.sql	Tue Feb 04 17:52:51 2014 +0100
+++ b/OrthancServer/Upgrade3To4.sql	Wed Feb 05 13:26:19 2014 +0100
@@ -1,3 +1,24 @@
--- Add 2 columns at "AttachedFiles"
+-- This SQLite script updates the version of the Orthanc database from 3 to 4.
+
+-- Add 2 new columns at "AttachedFiles"
+
+ALTER TABLE AttachedFiles ADD COLUMN uncompressedMD5 TEXT;
+ALTER TABLE AttachedFiles ADD COLUMN compressedMD5 TEXT;
+
+-- Update the "AttachedFileDeleted" trigger
+
+DROP TRIGGER AttachedFileDeleted;
 
--- Delete & recreate trigger "AttachedFileDeleted"
+CREATE TRIGGER AttachedFileDeleted
+AFTER DELETE ON AttachedFiles
+BEGIN
+  SELECT SignalFileDeleted(old.uuid, old.fileType, old.uncompressedSize, 
+                           old.compressionType, old.compressedSize,
+                           -- These 2 arguments are new in Orthanc 0.7.3 (database v4)
+                           old.uncompressedMD5, old.compressedMD5);
+END;
+
+-- Change the database version
+-- The "1" corresponds to the "GlobalProperty_DatabaseSchemaVersion" enumeration
+
+UPDATE GlobalProperties SET value="4" WHERE property=1;