changeset 743:78fdfbe7b1fd

Now verifying the DatabasePatchLevel (revision) in another transaction than the one that upgrades the schema
author Alain Mazy <am@orthanc.team>
date Fri, 03 Oct 2025 18:39:46 +0200
parents 696f2dd60f4d
children 55a827c8913a
files PostgreSQL/NEWS PostgreSQL/Plugins/PostgreSQLIndex.cpp PostgreSQL/Plugins/SQL/PrepareIndex.sql
diffstat 3 files changed, 26 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/PostgreSQL/NEWS	Wed Sep 17 14:41:20 2025 +0200
+++ b/PostgreSQL/NEWS	Fri Oct 03 18:39:46 2025 +0200
@@ -1,3 +1,21 @@
+Pending changes in the mainline
+===============================
+
+DB schema revision: 6
+Minimum plugin SDK (for build): 1.12.5
+Optimal plugin SDK (for build): 1.12.9
+Minimum Orthanc runtime: 1.12.5
+Optimal Orthanc runtime: 1.12.9
+
+Minimal Postgresql Server version: 9
+Optimal Postgresql Server version: 11+
+
+
+Maintenance:
+* Now verifying the DatabasePatchLevel (revision) in another transaction than
+  the one that upgrades the schema.
+
+
 Release 9.0 (2025-08-13)
 ========================
 
--- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Wed Sep 17 14:41:20 2025 +0200
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Fri Oct 03 18:39:46 2025 +0200
@@ -130,6 +130,7 @@
           LOG(WARNING) << "PostgreSQL is creating the database schema";
 
           ApplyPrepareIndex(t, manager);
+          hasAppliedAnUpgrade = true;
 
           if (!t.GetDatabaseTransaction().DoesTableExist("Resources"))
           {
@@ -266,20 +267,6 @@
             LOG(WARNING) << "Upgrading DB schema by applying PrepareIndex.sql";
             // apply all idempotent changes that are in the PrepareIndex.sql
             ApplyPrepareIndex(t, manager);
-
-            if (!LookupGlobalIntegerProperty(currentRevision, manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel))
-            {
-              LOG(ERROR) << "No Database revision found after the upgrade !";
-              throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
-            }
-
-            LOG(WARNING) << "Database revision after the upgrade is " << currentRevision;
-
-            if (currentRevision != CURRENT_DB_REVISION)
-            {
-              LOG(ERROR) << "Invalid database revision after the upgrade !";
-              throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
-            }
           }
 
         }
@@ -289,6 +276,12 @@
         if (hasAppliedAnUpgrade)
         {
           int currentRevision = 0;
+
+          if (!LookupGlobalIntegerProperty(currentRevision, manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel))
+          {
+            LOG(ERROR) << "No Database revision found after the upgrade !";
+            throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
+          }
           
           LookupGlobalIntegerProperty(currentRevision, manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel);
           LOG(WARNING) << "Database revision after the upgrade is " << currentRevision;
--- a/PostgreSQL/Plugins/SQL/PrepareIndex.sql	Wed Sep 17 14:41:20 2025 +0200
+++ b/PostgreSQL/Plugins/SQL/PrepareIndex.sql	Fri Oct 03 18:39:46 2025 +0200
@@ -154,7 +154,7 @@
 
 
 --------------------- PatientRecyclingOrder -------------------
--- from rev 99, we always maintain a PatientRecyclingOrder metadata, no matter if the patient is protected or not
+-- from rev 6, we always maintain a PatientRecyclingOrder metadata, no matter if the patient is protected or not
 CREATE OR REPLACE FUNCTION PatientAddedOrUpdated(
     IN patient_id BIGINT
     )