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 &currentOpt, 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, bool isWithChecked);
38     bool IsValueNeedUpgrade() const override;
39 
40 protected:
41     int BeginUpgrade() override;
42     int EndUpgrade(bool isSuccess) override;
43     int GetDatabaseVersion(int &version) const override;
44     int SetDatabaseVersion(int version) override;
45     int UpgradeFromDatabaseVersion(int version) override;
46     void SetUpgradeSqls(int version, std::vector<std::string> &sqls, bool &isCreateUpgradeFile) const;
47     void InitTimeForUpgrade(int version);
48     std::pair<int, TimeOffset> GetLocalTimeOffset();
49     void UpgradeTime(TimeOffset offset);
50     void MigrateMetaFromMetaDB();
51     void SetCreateSql(std::vector<std::string> &sql) const;
52     static int MoveDatabaseToNewDir(const std::string &parentDir, const std::string &upgradeLockFile);
53     static int GetDbVersion(const std::string &dbPath, const OpenDbProperties &option, int &version);
54 
55     sqlite3 *db_ = nullptr;
56     SecurityOption secOpt_;
57     bool isMemDB_;
58     bool isMetaUpgrade_;
59     std::string subDir_;
60     bool isMigrateMetaDb_; // need recreate meta table and migrate data from meta.meta
61 };
62 } // namespace DistributedDB
63 #endif // SQLITE_SINGLE_VER_DATABASE_UPGRADER_H
64