Remove old way to init SqliteDb.

This commit is contained in:
levlam 2021-09-22 17:05:52 +03:00
parent f8402d5d9d
commit 2008290425
5 changed files with 11 additions and 34 deletions

View File

@ -8,6 +8,7 @@
#include "td/utils/common.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/port/path.h"
#include "td/utils/port/Stat.h"
#include "td/utils/SliceBuilder.h"
@ -70,17 +71,13 @@ string db_key_to_sqlcipher_key(const DbKey &db_key) {
SqliteDb::~SqliteDb() = default;
Status SqliteDb::init(CSlice path, bool *was_created) {
// If database does not exist, delete all other files which may left
// from older database
Status SqliteDb::init(CSlice path) {
// if database does not exist, delete all other files which could have been left from the old database
bool is_db_exists = stat(path).is_ok();
if (!is_db_exists) {
TRY_STATUS(destroy(path));
}
if (was_created != nullptr) {
*was_created = !is_db_exists;
}
sqlite3 *db;
CHECK(sqlite3_threadsafe() != 0);
int rc = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE /*| SQLITE_OPEN_SHAREDCACHE*/,
@ -196,7 +193,7 @@ Status SqliteDb::check_encryption() {
Result<SqliteDb> SqliteDb::open_with_key(CSlice path, const DbKey &db_key, optional<int32> cipher_version) {
auto res = do_open_with_key(path, db_key, cipher_version ? cipher_version.value() : 0);
if (res.is_error() && !cipher_version) {
if (res.is_error() && !cipher_version && !db_key.is_empty()) {
return do_open_with_key(path, db_key, 3);
}
return res;
@ -217,7 +214,7 @@ Result<SqliteDb> SqliteDb::do_open_with_key(CSlice path, const DbKey &db_key, in
}
db.set_cipher_version(cipher_version);
}
TRY_STATUS_PREFIX(db.check_encryption(), "Can't open database: ");
TRY_STATUS_PREFIX(db.check_encryption(), "Can't check database: ");
return std::move(db);
}

View File

@ -11,7 +11,6 @@
#include "td/db/detail/RawSqliteDb.h"
#include "td/utils/logging.h"
#include "td/utils/optional.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
@ -25,10 +24,6 @@ namespace td {
class SqliteDb {
public:
SqliteDb() = default;
explicit SqliteDb(CSlice path) {
auto status = init(path);
LOG_IF(FATAL, status.is_error()) << status;
}
SqliteDb(SqliteDb &&) = default;
SqliteDb &operator=(SqliteDb &&) = default;
SqliteDb(const SqliteDb &) = delete;
@ -47,7 +42,6 @@ class SqliteDb {
*this = SqliteDb();
}
Status init(CSlice path, bool *was_created = nullptr) TD_WARN_UNUSED_RESULT;
Status exec(CSlice cmd) TD_WARN_UNUSED_RESULT;
Result<bool> has_table(Slice table);
Result<string> get_pragma(Slice name);
@ -61,7 +55,7 @@ class SqliteDb {
static Status destroy(Slice path) TD_WARN_UNUSED_RESULT;
// Anyway we can't change the key on the fly, so having static functions is more than enough
// we can't change the key on the fly, so static functions are more than enough
static Result<SqliteDb> open_with_key(CSlice path, const DbKey &db_key, optional<int32> cipher_version = {});
static Result<SqliteDb> change_key(CSlice path, const DbKey &new_db_key, const DbKey &old_db_key);
@ -86,6 +80,8 @@ class SqliteDb {
std::shared_ptr<detail::RawSqliteDb> raw_;
bool enable_logging_ = false;
Status init(CSlice path) TD_WARN_UNUSED_RESULT;
Status check_encryption();
static Result<SqliteDb> do_open_with_key(CSlice path, const DbKey &db_key, int32 cipher_version);
void set_cipher_version(int32 cipher_version);

View File

@ -11,19 +11,6 @@
namespace td {
Result<bool> SqliteKeyValue::init(string path) {
path_ = std::move(path);
bool is_created = false;
SqliteDb db;
TRY_STATUS(db.init(path, &is_created));
TRY_STATUS(db.exec("PRAGMA encoding=\"UTF-8\""));
TRY_STATUS(db.exec("PRAGMA synchronous=NORMAL"));
TRY_STATUS(db.exec("PRAGMA journal_mode=WAL"));
TRY_STATUS(db.exec("PRAGMA temp_store=MEMORY"));
TRY_STATUS(init_with_connection(std::move(db), "KV"));
return is_created;
}
Status SqliteKeyValue::init_with_connection(SqliteDb connection, string table_name) {
auto init_guard = ScopeExit() + [&] {
close();

View File

@ -33,8 +33,6 @@ class SqliteKeyValue {
return db_.empty();
}
Result<bool> init(string path) TD_WARN_UNUSED_RESULT;
Status init_with_connection(SqliteDb connection, string table_name) TD_WARN_UNUSED_RESULT;
Result<bool> try_regenerate_index() TD_WARN_UNUSED_RESULT {
@ -106,7 +104,6 @@ class SqliteKeyValue {
}
private:
string path_;
string table_name_;
SqliteDb db_;
SqliteStatement get_stmt_;

View File

@ -137,8 +137,7 @@ TEST(DB, binlog_encryption) {
TEST(DB, sqlite_lfs) {
string path = "test_sqlite_db";
SqliteDb::destroy(path).ignore();
SqliteDb db;
db.init(path).ensure();
auto db = SqliteDb::open_with_key(path, DbKey::empty()).move_as_ok();
db.exec("PRAGMA journal_mode=WAL").ensure();
db.exec("PRAGMA user_version").ensure();
}
@ -380,7 +379,8 @@ TEST(DB, key_value) {
QueryHandler<SqliteKeyValue> sqlite_kv;
CSlice name = "test_sqlite_kv";
SqliteDb::destroy(name).ignore();
sqlite_kv.impl().init(name.str()).ensure();
auto db = SqliteDb::open_with_key(name, DbKey::empty()).move_as_ok();
sqlite_kv.impl().init_with_connection(std::move(db), "KV").ensure();
int cnt = 0;
for (auto &q : queries) {