1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef SQLITE_SINGLE_VER_DATABASE_UPGRADER_H 17 #define SQLITE_SINGLE_VER_DATABASE_UPGRADER_H 18 19 #include "macro_utils.h" 20 #include "sqlite_utils.h" 21 #include "single_ver_database_upgrader.h" 22 23 namespace DistributedDB { 24 class SQLiteSingleVerDatabaseUpgrader : virtual public SingleVerDatabaseUpgrader { 25 public: 26 explicit SQLiteSingleVerDatabaseUpgrader(sqlite3 *db, const SecurityOption &secopt, bool isMemDb); 27 ~SQLiteSingleVerDatabaseUpgrader() override; 28 DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerDatabaseUpgrader); 29 30 // used for transferring db file to new dir while classifycation feature in SOFTWARE_VERSION_RELEASE_3_0 31 static int TransferDatabasePath(const std::string &parentDir, const OpenDbProperties &option); 32 33 void SetMetaUpgrade(const SecurityOption ¤tOpt, const SecurityOption &expectOpt, const std::string &subDir); 34 void SetSubdir(const std::string &subDir); 35 static int SetPathSecOptWithCheck(const std::string &path, const SecurityOption &secOption, 36 const std::string &dbStore, bool isWithChecked = false); 37 static int SetSecOption(const std::string &path, const SecurityOption &secOption, SecurityOption existedSecOpt, 38 bool isWithChecked); 39 bool IsValueNeedUpgrade() const override; 40 41 protected: 42 int BeginUpgrade() override; 43 int EndUpgrade(bool isSuccess) override; 44 int GetDatabaseVersion(int &version) const override; 45 int SetDatabaseVersion(int version) override; 46 int UpgradeFromDatabaseVersion(int version) override; 47 void SetUpgradeSqls(int version, std::vector<std::string> &sqls, bool &isCreateUpgradeFile) const; 48 void InitTimeForUpgrade(int version); 49 std::pair<int, TimeOffset> GetLocalTimeOffset(); 50 void UpgradeTime(TimeOffset offset); 51 void MigrateMetaFromMetaDB(); 52 void SetCreateSql(std::vector<std::string> &sql) const; 53 static int MoveDatabaseToNewDir(const std::string &parentDir, const std::string &upgradeLockFile); 54 static int GetDbVersion(const std::string &dbPath, const OpenDbProperties &option, int &version); 55 56 sqlite3 *db_ = nullptr; 57 SecurityOption secOpt_; 58 bool isMemDB_; 59 bool isMetaUpgrade_; 60 std::string subDir_; 61 bool isMigrateMetaDb_; // need recreate meta table and migrate data from meta.meta 62 }; 63 } // namespace DistributedDB 64 #endif // SQLITE_SINGLE_VER_DATABASE_UPGRADER_H 65