From 16cfc3ad8bccf301eeaec498e4acd57b20ebbdb3 Mon Sep 17 00:00:00 2001 From: Nejc Zdovc Date: Mon, 18 Feb 2019 18:43:17 +0100 Subject: [PATCH] Merge pull request #1626 from brave/min-visit-upgrade Adds migration for min visits --- .../browser/publisher_info_database.cc | 36 ++++++++++++- .../browser/publisher_info_database.h | 2 + .../publisher_info_database_unittest.cc | 48 ++++++++++++++++++ .../migration/publisher_info_db_v4 | Bin 0 -> 69632 bytes 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 test/data/rewards-data/migration/publisher_info_db_v4 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 0000000000000000000000000000000000000000..244d4dbac054983ed5ef97f124c255db018e35fa GIT binary patch literal 69632 zcmeI*+iTla90zbIa&5<#9G5K^7Fe_vlX|#yO&3aMO5-|e7UtTklXSfdjAAQ^sBe{J zw}G+YG=3{rm!quOlzj@QN(`1M{ z#$O4Nf4Gnk=EypEF!36BJ8;(*3zWUzj6e4Hy&DHUbvYBRR)YV$GP;ai5)y9UR71UC}G26j(UP>)X;$n7Qx+MO` zg2Y_bHmi8VywPZTig>~wCir%;vw2M^HZ{$NvsA0f4l!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