comparison OrthancServer/DatabaseWrapper.cpp @ 1615:c40fe92a68e7

Primitives to upgrade the database version in plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 16 Sep 2015 15:18:59 +0200
parents 0586ed8897f1
children 0c58f189782d
comparison
equal deleted inserted replaced
1614:1c9e99d2bfd2 1615:c40fe92a68e7
763 { 763 {
764 target.push_back(s.ColumnString(0)); 764 target.push_back(s.ColumnString(0));
765 } 765 }
766 } 766 }
767 767
768 static void UpgradeDatabase(SQLite::Connection& db,
769 EmbeddedResources::FileResourceId script)
770 {
771 std::string upgrade;
772 EmbeddedResources::GetFileResource(upgrade, script);
773 db.BeginTransaction();
774 db.Execute(upgrade);
775 db.CommitTransaction();
776 }
777
778 768
779 DatabaseWrapper::DatabaseWrapper(const std::string& path) : listener_(NULL) 769 DatabaseWrapper::DatabaseWrapper(const std::string& path) : listener_(NULL)
780 { 770 {
781 db_.Open(path); 771 db_.Open(path);
782 Open(); 772 Open();
805 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); 795 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE);
806 db_.Execute(query); 796 db_.Execute(query);
807 } 797 }
808 798
809 // Check the version of the database 799 // Check the version of the database
810 std::string version; 800 std::string tmp;
811 if (!LookupGlobalProperty(version, GlobalProperty_DatabaseSchemaVersion)) 801 if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion))
812 { 802 {
813 version = "Unknown"; 803 tmp = "Unknown";
814 } 804 }
815 805
816 bool ok = false; 806 bool ok = false;
817 try 807 try
818 { 808 {
819 LOG(INFO) << "Version of the Orthanc database: " << version; 809 LOG(INFO) << "Version of the Orthanc database: " << tmp;
820 unsigned int v = boost::lexical_cast<unsigned int>(version); 810 version_ = boost::lexical_cast<unsigned int>(tmp);
821 811 ok = true;
822 // This version of Orthanc is only compatible with versions 3, 4 and 5 of the DB schema
823 ok = (v == 3 || v == 4 || v == 5);
824
825 if (v == 3)
826 {
827 LOG(WARNING) << "Upgrading database version from 3 to 4";
828 UpgradeDatabase(db_, EmbeddedResources::UPGRADE_DATABASE_3_TO_4);
829 v = 4;
830 }
831
832 if (v == 4)
833 {
834 LOG(WARNING) << "Upgrading database version from 4 to 5";
835 UpgradeDatabase(db_, EmbeddedResources::UPGRADE_DATABASE_4_TO_5);
836 v = 5;
837 }
838
839 // Sanity check
840 if (ORTHANC_DATABASE_VERSION != v)
841 {
842 throw OrthancException(ErrorCode_InternalError);
843 }
844 } 812 }
845 catch (boost::bad_lexical_cast&) 813 catch (boost::bad_lexical_cast&)
846 { 814 {
847 ok = false;
848 } 815 }
849 816
850 if (!ok) 817 if (!ok)
851 { 818 {
852 LOG(ERROR) << "Incompatible version of the Orthanc database: " << version; 819 LOG(ERROR) << "Incompatible version of the Orthanc database: " << tmp;
853 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); 820 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
854 } 821 }
855 822
856 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; 823 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor;
857 db_.Register(signalRemainingAncestor_); 824 db_.Register(signalRemainingAncestor_);
858 } 825 }
826
827
828 static void ExecuteUpgradeScript(SQLite::Connection& db,
829 EmbeddedResources::FileResourceId script)
830 {
831 std::string upgrade;
832 EmbeddedResources::GetFileResource(upgrade, script);
833 db.BeginTransaction();
834 db.Execute(upgrade);
835 db.CommitTransaction();
836 }
837
838
839 void DatabaseWrapper::Upgrade(unsigned int targetVersion,
840 IStorageArea& storageArea)
841 {
842 if (targetVersion != 5)
843 {
844 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
845 }
846
847 // This version of Orthanc is only compatible with versions 3, 4 and 5 of the DB schema
848 if (version_ != 3 &&
849 version_ != 4 &&
850 version_ != 5)
851 {
852 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
853 }
854
855 if (version_ == 3)
856 {
857 LOG(WARNING) << "Upgrading database version from 3 to 4";
858 ExecuteUpgradeScript(db_, EmbeddedResources::UPGRADE_DATABASE_3_TO_4);
859 version_ = 4;
860 }
861
862 if (version_ == 4)
863 {
864 LOG(WARNING) << "Upgrading database version from 4 to 5";
865 ExecuteUpgradeScript(db_, EmbeddedResources::UPGRADE_DATABASE_4_TO_5);
866 version_ = 5;
867 }
868 }
869
859 870
860 void DatabaseWrapper::SetListener(IDatabaseListener& listener) 871 void DatabaseWrapper::SetListener(IDatabaseListener& listener)
861 { 872 {
862 listener_ = &listener; 873 listener_ = &listener;
863 db_.Register(new Internals::SignalFileDeleted(listener)); 874 db_.Register(new Internals::SignalFileDeleted(listener));