diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 41c0bb6b79ee..4a7145a8252c 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -24,7 +24,7 @@ namespace brave_rewards { namespace { -const int kCurrentVersionNumber = 4; +const int kCurrentVersionNumber = 5; const int kCompatibleVersionNumber = 1; } // namespace @@ -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_); @@ -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; } diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index c539c3d2d57e..6632498957e9 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -121,6 +121,8 @@ class PublisherInfoDatabase { bool MigrateV3toV4(); + bool MigrateV4toV5(); + sql::InitStatus EnsureCurrentVersion(); sql::Database db_; diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc index 5e567a32faa6..5fec80bc1653 100644 --- a/components/brave_rewards/browser/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -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" @@ -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(*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())); @@ -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(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 diff --git a/test/data/rewards-data/migration/publisher_info_db_v4 b/test/data/rewards-data/migration/publisher_info_db_v4 new file mode 100644 index 000000000000..244d4dbac054 Binary files /dev/null and b/test/data/rewards-data/migration/publisher_info_db_v4 differ