Skip to content

Commit

Permalink
Merge pull request #1626 from brave/min-visit-upgrade
Browse files Browse the repository at this point in the history
Adds migration for min visits
  • Loading branch information
NejcZdovc committed Feb 18, 2019
1 parent cc54ee1 commit 16cfc3a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
36 changes: 35 additions & 1 deletion components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace brave_rewards {

namespace {

const int kCurrentVersionNumber = 4;
const int kCurrentVersionNumber = 5;
const int kCompatibleVersionNumber = 1;

} // namespace
Expand Down Expand Up @@ -1079,6 +1079,33 @@ bool PublisherInfoDatabase::MigrateV3toV4() {
return false;
}

bool PublisherInfoDatabase::MigrateV4toV5() {
sql::Transaction transaction(&GetDB());
if (!transaction.Begin()) {
return false;
}

sql::Statement info_sql(db_.GetUniqueStatement(
"SELECT publisher_id, month, year, reconcile_stamp "
"FROM activity_info "
"WHERE visits = 0"));

while (info_sql.Step()) {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"UPDATE activity_info SET visits = 1 "
"WHERE publisher_id = ? AND month = ? AND "
"year = ? AND reconcile_stamp = ?"));

statement.BindString(0, info_sql.ColumnString(0));
statement.BindInt(1, info_sql.ColumnInt(1));
statement.BindInt(2, info_sql.ColumnInt(2));
statement.BindInt64(3, info_sql.ColumnInt64(3));
statement.Run();
}

return transaction.Commit();
}

sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

Expand Down Expand Up @@ -1112,6 +1139,13 @@ sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() {
}
}

// to version 5
if (old_version < 5 && cur_version < 6) {
if (!MigrateV4toV5()) {
LOG(ERROR) << "DB: Error with MigrateV4toV5";
}
}

meta_table_.SetVersionNumber(cur_version);
return sql::INIT_OK;
}
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/publisher_info_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class PublisherInfoDatabase {

bool MigrateV3toV4();

bool MigrateV4toV5();

sql::InitStatus EnsureCurrentVersion();

sql::Database db_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/common/brave_paths.h"
#include "sql/database.h"
#include "sql/statement.h"
#include "third_party/sqlite/sqlite3.h"
Expand Down Expand Up @@ -45,6 +47,33 @@ class PublisherInfoDatabaseTest : public ::testing::Test {
ASSERT_NE(publisher_info_database_, nullptr);
}

void CreateMigrationDatabase(base::ScopedTempDir* temp_dir,
base::FilePath* db_file,
const std::string& version) {
const std::string file_name = "publisher_info_db_v" + version;
ASSERT_TRUE(temp_dir->CreateUniqueTempDir());
*db_file = temp_dir->GetPath().AppendASCII(file_name);

// Get test data migration file
base::FilePath path;
ASSERT_TRUE(base::PathService::Get(brave::DIR_TEST_DATA, &path));
path = path.AppendASCII("rewards-data");
ASSERT_TRUE(base::PathExists(path));
path = path.AppendASCII("migration");
ASSERT_TRUE(base::PathExists(path));
path = path.AppendASCII(file_name);
ASSERT_TRUE(base::PathExists(path));

// Move it to temp dir
bool result = base::CopyFile(path, *db_file);
ASSERT_TRUE(result);
ASSERT_TRUE(base::PathExists(*db_file));

publisher_info_database_ =
std::make_unique<PublisherInfoDatabase>(*db_file);
ASSERT_NE(publisher_info_database_, nullptr);
}

int CountTableRows(const std::string& table) {
std::string sql = "SELECT COUNT(*) FROM " + table;
sql::Statement s(GetDB().GetUniqueStatement(sql.c_str()));
Expand Down Expand Up @@ -748,4 +777,23 @@ TEST_F(PublisherInfoDatabaseTest, GetActivityList) {
EXPECT_EQ(list_4.at(1).id, "publisher_6");
}

TEST_F(PublisherInfoDatabaseTest, Migrationv4tov5) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
CreateMigrationDatabase(&temp_dir, &db_file, "4");

ledger::PublisherInfoList list;
ledger::ActivityInfoFilter filter;
filter.excluded = ledger::EXCLUDE_FILTER::FILTER_ALL;
EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter, &list));
EXPECT_EQ(static_cast<int>(list.size()), 3);

EXPECT_EQ(list.at(0).id, "brave.com");
EXPECT_EQ(list.at(0).visits, 1u);
EXPECT_EQ(list.at(1).id, "slo-tech.com");
EXPECT_EQ(list.at(1).visits, 1u);
EXPECT_EQ(list.at(2).id, "basicattentiontoken.org");
EXPECT_EQ(list.at(2).visits, 3u);
}

} // namespace brave_rewards
Binary file not shown.

0 comments on commit 16cfc3a

Please sign in to comment.