From 17f407b05a2ccee296afaad9199c62bbb7337d71 Mon Sep 17 00:00:00 2001
From: Cedric CAFFY <cedric.caffy@cern.ch>
Date: Wed, 22 Jan 2020 11:26:23 +0100
Subject: [PATCH] Added STATUS='COMPLETE' when inserting schema version in the
 CTA_CATALOGUE table Added CATALOGUE_STATUS_CONTENT_CK constraint checking in
 Postgres and MySQL schemas

---
 catalogue/1.1/mysql_catalogue_schema.sql      |  8 ++++--
 catalogue/1.1/oracle_catalogue_schema.sql     |  6 ++--
 catalogue/1.1/postgres_catalogue_schema.sql   |  8 ++++--
 catalogue/1.1/sqlite_catalogue_schema.sql     |  6 ++--
 catalogue/AllCatalogueSchema.hpp              | 28 +++++++++++++------
 catalogue/CMakeLists.txt                      |  1 +
 catalogue/insert_cta_catalogue_version.sql.in |  6 ++--
 catalogue/mysql_catalogue_schema_trailer.sql  |  2 ++
 .../postgres_catalogue_schema_trailer.sql     |  2 ++
 9 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/catalogue/1.1/mysql_catalogue_schema.sql b/catalogue/1.1/mysql_catalogue_schema.sql
index ba96bfbb6c..1b308e9b6c 100644
--- a/catalogue/1.1/mysql_catalogue_schema.sql
+++ b/catalogue/1.1/mysql_catalogue_schema.sql
@@ -288,7 +288,11 @@ CREATE TABLE EXPERIMENTS (
 );
 INSERT INTO CTA_CATALOGUE(
   SCHEMA_VERSION_MAJOR,
-  SCHEMA_VERSION_MINOR)
+  SCHEMA_VERSION_MINOR,
+  STATUS)
 VALUES(
   1,
-  1);
+  1,
+  'COMPLETE');
+ALTER TABLE CTA_CATALOGUE ADD CONSTRAINT 
+  CATALOGUE_STATUS_CONTENT_CK CHECK((NEXT_SCHEMA_VERSION_MAJOR IS NULL AND NEXT_SCHEMA_VERSION_MINOR IS NULL AND STATUS='COMPLETE') OR (STATUS='UPGRADING'));
\ No newline at end of file
diff --git a/catalogue/1.1/oracle_catalogue_schema.sql b/catalogue/1.1/oracle_catalogue_schema.sql
index 14e458c836..3cdc5b2823 100644
--- a/catalogue/1.1/oracle_catalogue_schema.sql
+++ b/catalogue/1.1/oracle_catalogue_schema.sql
@@ -310,10 +310,12 @@ CREATE TABLE EXPERIMENTS (
 );
 INSERT INTO CTA_CATALOGUE(
   SCHEMA_VERSION_MAJOR,
-  SCHEMA_VERSION_MINOR)
+  SCHEMA_VERSION_MINOR,
+  STATUS)
 VALUES(
   1,
-  1);
+  1,
+  'COMPLETE');
 ALTER TABLE CTA_CATALOGUE ADD CONSTRAINT CATALOGUE_STATUS_CONTENT_CK CHECK((NEXT_SCHEMA_VERSION_MAJOR IS NULL AND NEXT_SCHEMA_VERSION_MINOR IS NULL AND STATUS='COMPLETE') OR (NEXT_SCHEMA_VERSION_MAJOR IS NOT NULL AND NEXT_SCHEMA_VERSION_MINOR IS NOT NULL AND STATUS='UPGRADING')) INITIALLY DEFERRED;
 
 COMMIT;
diff --git a/catalogue/1.1/postgres_catalogue_schema.sql b/catalogue/1.1/postgres_catalogue_schema.sql
index 662305e2cd..b27719df47 100644
--- a/catalogue/1.1/postgres_catalogue_schema.sql
+++ b/catalogue/1.1/postgres_catalogue_schema.sql
@@ -295,11 +295,15 @@ CREATE TABLE EXPERIMENTS (
 );
 INSERT INTO CTA_CATALOGUE(
   SCHEMA_VERSION_MAJOR,
-  SCHEMA_VERSION_MINOR)
+  SCHEMA_VERSION_MINOR,
+  STATUS)
 VALUES(
   1,
-  1);
+  1,
+  'COMPLETE');
 ALTER TABLE ARCHIVE_FILE DROP CONSTRAINT
   ARCHIVE_FILE_DIN_DFI_UN;
 ALTER TABLE ARCHIVE_FILE ADD CONSTRAINT
   ARCHIVE_FILE_DIN_DFI_UN UNIQUE(DISK_INSTANCE_NAME, DISK_FILE_ID) DEFERRABLE INITIALLY IMMEDIATE;
+ALTER TABLE CTA_CATALOGUE ADD CONSTRAINT 
+  CATALOGUE_STATUS_CONTENT_CK CHECK((NEXT_SCHEMA_VERSION_MAJOR IS NULL AND NEXT_SCHEMA_VERSION_MINOR IS NULL AND STATUS='COMPLETE') OR (STATUS='UPGRADING'));
diff --git a/catalogue/1.1/sqlite_catalogue_schema.sql b/catalogue/1.1/sqlite_catalogue_schema.sql
index b730593c34..8205204162 100644
--- a/catalogue/1.1/sqlite_catalogue_schema.sql
+++ b/catalogue/1.1/sqlite_catalogue_schema.sql
@@ -279,7 +279,9 @@ CREATE TABLE EXPERIMENTS (
 );
 INSERT INTO CTA_CATALOGUE(
   SCHEMA_VERSION_MAJOR,
-  SCHEMA_VERSION_MINOR)
+  SCHEMA_VERSION_MINOR,
+  STATUS)
 VALUES(
   1,
-  1);
+  1,
+  'COMPLETE');
diff --git a/catalogue/AllCatalogueSchema.hpp b/catalogue/AllCatalogueSchema.hpp
index d3e567a99b..51f560dd34 100644
--- a/catalogue/AllCatalogueSchema.hpp
+++ b/catalogue/AllCatalogueSchema.hpp
@@ -1535,10 +1535,12 @@ namespace catalogue{
   ");"
   "INSERT INTO CTA_CATALOGUE("
   "  SCHEMA_VERSION_MAJOR,"
-  "  SCHEMA_VERSION_MINOR)"
+  "  SCHEMA_VERSION_MINOR,"
+  "  STATUS)"
   "VALUES("
   "  1,"
-  "  1);"
+  "  1,"
+  "  'COMPLETE');"
   "ALTER TABLE CTA_CATALOGUE ADD CONSTRAINT CATALOGUE_STATUS_CONTENT_CK CHECK((NEXT_SCHEMA_VERSION_MAJOR IS NULL AND NEXT_SCHEMA_VERSION_MINOR IS NULL AND STATUS='COMPLETE') OR (NEXT_SCHEMA_VERSION_MAJOR IS NOT NULL AND NEXT_SCHEMA_VERSION_MINOR IS NOT NULL AND STATUS='UPGRADING')) INITIALLY DEFERRED;"
   ""
   "COMMIT;"
@@ -1833,10 +1835,14 @@ namespace catalogue{
   ");"
   "INSERT INTO CTA_CATALOGUE("
   "  SCHEMA_VERSION_MAJOR,"
-  "  SCHEMA_VERSION_MINOR)"
+  "  SCHEMA_VERSION_MINOR,"
+  "  STATUS)"
   "VALUES("
   "  1,"
-  "  1);"
+  "  1,"
+  "  'COMPLETE');"
+  "ALTER TABLE CTA_CATALOGUE ADD CONSTRAINT "
+  "  CATALOGUE_STATUS_CONTENT_CK CHECK((NEXT_SCHEMA_VERSION_MAJOR IS NULL AND NEXT_SCHEMA_VERSION_MINOR IS NULL AND STATUS='COMPLETE') OR (STATUS='UPGRADING'));"
       },
   {"sqlite",  "CREATE TABLE ARCHIVE_FILE_ID("
   "  ID INTEGER PRIMARY KEY AUTOINCREMENT"
@@ -2119,10 +2125,12 @@ namespace catalogue{
   ");"
   "INSERT INTO CTA_CATALOGUE("
   "  SCHEMA_VERSION_MAJOR,"
-  "  SCHEMA_VERSION_MINOR)"
+  "  SCHEMA_VERSION_MINOR,"
+  "  STATUS)"
   "VALUES("
   "  1,"
-  "  1);"
+  "  1,"
+  "  'COMPLETE');"
       },
   {"postgres",  "CREATE SEQUENCE ARCHIVE_FILE_ID_SEQ"
   "  INCREMENT BY 1"
@@ -2421,14 +2429,18 @@ namespace catalogue{
   ");"
   "INSERT INTO CTA_CATALOGUE("
   "  SCHEMA_VERSION_MAJOR,"
-  "  SCHEMA_VERSION_MINOR)"
+  "  SCHEMA_VERSION_MINOR,"
+  "  STATUS)"
   "VALUES("
   "  1,"
-  "  1);"
+  "  1,"
+  "  'COMPLETE');"
   "ALTER TABLE ARCHIVE_FILE DROP CONSTRAINT"
   "  ARCHIVE_FILE_DIN_DFI_UN;"
   "ALTER TABLE ARCHIVE_FILE ADD CONSTRAINT"
   "  ARCHIVE_FILE_DIN_DFI_UN UNIQUE(DISK_INSTANCE_NAME, DISK_FILE_ID) DEFERRABLE INITIALLY IMMEDIATE;"
+  "ALTER TABLE CTA_CATALOGUE ADD CONSTRAINT "
+  "  CATALOGUE_STATUS_CONTENT_CK CHECK((NEXT_SCHEMA_VERSION_MAJOR IS NULL AND NEXT_SCHEMA_VERSION_MINOR IS NULL AND STATUS='COMPLETE') OR (STATUS='UPGRADING'));"
       },
     }  },};
 	  
diff --git a/catalogue/CMakeLists.txt b/catalogue/CMakeLists.txt
index dc310f54fe..4776686a9c 100644
--- a/catalogue/CMakeLists.txt
+++ b/catalogue/CMakeLists.txt
@@ -165,6 +165,7 @@ add_custom_command (OUTPUT sqlite_catalogue_schema.sql mysql_catalogue_schema.sq
     ${CMAKE_CURRENT_SOURCE_DIR}/oracle_catalogue_schema_trailer.sql
     ${CMAKE_CURRENT_SOURCE_DIR}/CreateAllSchemasCppFile.sh
     ${CMAKE_CURRENT_SOURCE_DIR}/AllCatalogueSchema.hpp.in
+    ${CMAKE_CURRENT_SOURCE_DIR}/insert_cta_catalogue_version.sql.in
 )
 
 add_custom_command(OUTPUT sqlite_catalogue_schema.cpp
diff --git a/catalogue/insert_cta_catalogue_version.sql.in b/catalogue/insert_cta_catalogue_version.sql.in
index 22d3d0b3a8..644a112b42 100644
--- a/catalogue/insert_cta_catalogue_version.sql.in
+++ b/catalogue/insert_cta_catalogue_version.sql.in
@@ -1,6 +1,8 @@
 INSERT INTO CTA_CATALOGUE(
   SCHEMA_VERSION_MAJOR,
-  SCHEMA_VERSION_MINOR)
+  SCHEMA_VERSION_MINOR,
+  STATUS)
 VALUES(
   @CTA_CATALOGUE_SCHEMA_VERSION_MAJOR@,
-  @CTA_CATALOGUE_SCHEMA_VERSION_MINOR@);
\ No newline at end of file
+  @CTA_CATALOGUE_SCHEMA_VERSION_MINOR@,
+  'COMPLETE');
\ No newline at end of file
diff --git a/catalogue/mysql_catalogue_schema_trailer.sql b/catalogue/mysql_catalogue_schema_trailer.sql
index e69de29bb2..297849c8f7 100644
--- a/catalogue/mysql_catalogue_schema_trailer.sql
+++ b/catalogue/mysql_catalogue_schema_trailer.sql
@@ -0,0 +1,2 @@
+ALTER TABLE CTA_CATALOGUE ADD CONSTRAINT 
+  CATALOGUE_STATUS_CONTENT_CK CHECK((NEXT_SCHEMA_VERSION_MAJOR IS NULL AND NEXT_SCHEMA_VERSION_MINOR IS NULL AND STATUS='COMPLETE') OR (STATUS='UPGRADING'));
\ No newline at end of file
diff --git a/catalogue/postgres_catalogue_schema_trailer.sql b/catalogue/postgres_catalogue_schema_trailer.sql
index 02931d9802..991527176c 100644
--- a/catalogue/postgres_catalogue_schema_trailer.sql
+++ b/catalogue/postgres_catalogue_schema_trailer.sql
@@ -2,3 +2,5 @@ ALTER TABLE ARCHIVE_FILE DROP CONSTRAINT
   ARCHIVE_FILE_DIN_DFI_UN;
 ALTER TABLE ARCHIVE_FILE ADD CONSTRAINT
   ARCHIVE_FILE_DIN_DFI_UN UNIQUE(DISK_INSTANCE_NAME, DISK_FILE_ID) DEFERRABLE INITIALLY IMMEDIATE;
+ALTER TABLE CTA_CATALOGUE ADD CONSTRAINT 
+  CATALOGUE_STATUS_CONTENT_CK CHECK((NEXT_SCHEMA_VERSION_MAJOR IS NULL AND NEXT_SCHEMA_VERSION_MINOR IS NULL AND STATUS='COMPLETE') OR (STATUS='UPGRADING'));
-- 
GitLab