Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds migration for min visits #1713

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.