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

Removes month and year from activity_info table #1760

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
190 changes: 119 additions & 71 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ namespace brave_rewards {

namespace {

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

} // namespace

PublisherInfoDatabase::PublisherInfoDatabase(const base::FilePath& db_path) :
db_path_(db_path),
initialized_(false) {
initialized_(false),
testing_current_version_(-1) {
DETACH_FROM_SEQUENCE(sequence_checker_);
}

Expand Down Expand Up @@ -187,7 +188,7 @@ void PublisherInfoDatabase::GetTips(ledger::PublisherInfoList* list,
while (info_sql.Step()) {
std::string id(info_sql.ColumnString(0));

ledger::PublisherInfo publisher(id, ledger::ACTIVITY_MONTH::ANY, -1);
ledger::PublisherInfo publisher(id);

publisher.name = info_sql.ColumnString(1);
publisher.url = info_sql.ColumnString(2);
Expand Down Expand Up @@ -412,11 +413,9 @@ bool PublisherInfoDatabase::CreateActivityInfoTable() {
"score DOUBLE DEFAULT 0 NOT NULL,"
"percent INTEGER DEFAULT 0 NOT NULL,"
"weight DOUBLE DEFAULT 0 NOT NULL,"
"month INTEGER NOT NULL,"
"year INTEGER NOT NULL,"
"reconcile_stamp INTEGER DEFAULT 0 NOT NULL,"
"CONSTRAINT activity_unique "
"UNIQUE (publisher_id, month, year, reconcile_stamp) "
"UNIQUE (publisher_id, reconcile_stamp) "
"CONSTRAINT fk_activity_info_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
Expand Down Expand Up @@ -452,18 +451,16 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo(
GetDB().GetCachedStatement(SQL_FROM_HERE,
"INSERT OR REPLACE INTO activity_info "
"(publisher_id, duration, score, percent, "
"weight, month, year, reconcile_stamp, visits) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"));
"weight, reconcile_stamp, visits) "
"VALUES (?, ?, ?, ?, ?, ?, ?)"));

activity_info_insert.BindString(0, info.id);
activity_info_insert.BindInt64(1, static_cast<int>(info.duration));
activity_info_insert.BindDouble(2, info.score);
activity_info_insert.BindInt64(3, static_cast<int>(info.percent));
activity_info_insert.BindDouble(4, info.weight);
activity_info_insert.BindInt(5, info.month);
activity_info_insert.BindInt(6, info.year);
activity_info_insert.BindInt64(7, info.reconcile_stamp);
activity_info_insert.BindInt(8, info.visits);
activity_info_insert.BindInt64(5, info.reconcile_stamp);
activity_info_insert.BindInt(6, info.visits);

return activity_info_insert.Run();
}
Expand Down Expand Up @@ -512,7 +509,7 @@ bool PublisherInfoDatabase::GetActivityList(

std::string query = "SELECT ai.publisher_id, ai.duration, ai.score, "
"ai.percent, ai.weight, pi.verified, pi.excluded, "
"ai.month, ai.year, pi.name, pi.url, pi.provider, "
"pi.name, pi.url, pi.provider, "
"pi.favIcon, ai.reconcile_stamp, ai.visits "
"FROM activity_info AS ai "
"INNER JOIN publisher_info AS pi "
Expand All @@ -523,14 +520,6 @@ bool PublisherInfoDatabase::GetActivityList(
query += " AND ai.publisher_id = ?";
}

if (filter.month != ledger::ACTIVITY_MONTH::ANY) {
query += " AND ai.month = ?";
}

if (filter.year > 0) {
query += " AND ai.year = ?";
}

if (filter.reconcile_stamp > 0) {
query += " AND ai.reconcile_stamp = ?";
}
Expand Down Expand Up @@ -582,14 +571,6 @@ bool PublisherInfoDatabase::GetActivityList(
info_sql.BindString(column++, filter.id);
}

if (filter.month != ledger::ACTIVITY_MONTH::ANY) {
info_sql.BindInt(column++, filter.month);
}

if (filter.year > 0) {
info_sql.BindInt(column++, filter.year);
}

if (filter.reconcile_stamp > 0) {
info_sql.BindInt64(column++, filter.reconcile_stamp);
}
Expand Down Expand Up @@ -619,26 +600,21 @@ bool PublisherInfoDatabase::GetActivityList(

while (info_sql.Step()) {
std::string id(info_sql.ColumnString(0));
ledger::ACTIVITY_MONTH month(
static_cast<ledger::ACTIVITY_MONTH>(info_sql.ColumnInt(7)));
int year(info_sql.ColumnInt(8));

ledger::PublisherInfo info(id, month, year);
ledger::PublisherInfo info(id);
info.duration = info_sql.ColumnInt64(1);

info.score = info_sql.ColumnDouble(2);
info.percent = info_sql.ColumnInt64(3);
info.weight = info_sql.ColumnDouble(4);
info.verified = info_sql.ColumnBool(5);
info.name = info_sql.ColumnString(9);
info.url = info_sql.ColumnString(10);
info.provider = info_sql.ColumnString(11);
info.favicon_url = info_sql.ColumnString(12);
info.reconcile_stamp = info_sql.ColumnInt64(13);
info.visits = info_sql.ColumnInt(14);

info.excluded = static_cast<ledger::PUBLISHER_EXCLUDE>(
info_sql.ColumnInt(6));
info.name = info_sql.ColumnString(7);
info.url = info_sql.ColumnString(8);
info.provider = info_sql.ColumnString(9);
info.favicon_url = info_sql.ColumnString(10);
info.reconcile_stamp = info_sql.ColumnInt64(11);
info.visits = info_sql.ColumnInt(12);

list->push_back(info);
}
Expand Down Expand Up @@ -815,8 +791,7 @@ void PublisherInfoDatabase::GetRecurringDonations(
while (info_sql.Step()) {
std::string id(info_sql.ColumnString(0));

ledger::PublisherInfo publisher(id, ledger::ACTIVITY_MONTH::ANY, -1);

ledger::PublisherInfo publisher(id);
publisher.name = info_sql.ColumnString(1);
publisher.url = info_sql.ColumnString(2);
publisher.favicon_url = info_sql.ColumnString(3);
Expand Down Expand Up @@ -943,11 +918,19 @@ double PublisherInfoDatabase::GetReservedAmount() {

return amount;
}
// static

int PublisherInfoDatabase::GetCurrentVersion() {
if (testing_current_version_ != -1) {
return testing_current_version_;
}

return kCurrentVersionNumber;
}

void PublisherInfoDatabase::SetTestingCurrentVersion(int value) {
testing_current_version_ = value;
}

void PublisherInfoDatabase::Vacuum() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

Expand Down Expand Up @@ -981,6 +964,10 @@ sql::MetaTable& PublisherInfoDatabase::GetMetaTable() {
return meta_table_;
}

int PublisherInfoDatabase::GetTableVersionNumber() {
return meta_table_.GetVersionNumber();
}

// Migration -------------------------------------------------------------------

bool PublisherInfoDatabase::MigrateV1toV2() {
Expand Down Expand Up @@ -1106,47 +1093,108 @@ bool PublisherInfoDatabase::MigrateV4toV5() {
return transaction.Commit();
}

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

// We can't read databases newer than we were designed for.
if (meta_table_.GetCompatibleVersionNumber() > GetCurrentVersion()) {
LOG(WARNING) << "Publisher info database is too new.";
return sql::INIT_TOO_NEW;
bool PublisherInfoDatabase::MigrateV5toV6() {
sql::Transaction transaction(&GetDB());
if (!transaction.Begin()) {
return false;
}

const int old_version = meta_table_.GetVersionNumber();
const int cur_version = GetCurrentVersion();
const char* activity = "activity_info";
if (GetDB().DoesTableExist(activity)) {
std::string sql = "ALTER TABLE activity_info RENAME TO activity_info_old;";

if (!GetDB().Execute(sql.c_str())) {
return false;
}

if (!CreateActivityInfoTable()) {
return false;
}

// to version 2
if (old_version < 2 && cur_version < 3) {
if (!MigrateV1toV2()) {
LOG(ERROR) << "DB: Error with MigrateV1toV2";
if (!CreateActivityInfoIndex()) {
return false;
}
}

// to version 3
if (old_version < 3 && cur_version < 4) {
if (!MigrateV2toV3()) {
LOG(ERROR) << "DB: Error with MigrateV2toV3";
const std::string columns_insert = "publisher_id, "
"duration, "
"visits, "
"score, "
"percent, "
"weight, "
"reconcile_stamp";

const std::string columns_select = "publisher_id, "
"sum(duration) as duration, "
"sum(visits) as duration, "
"sum(score) as score, "
"percent, "
"weight, "
"reconcile_stamp";

sql = "PRAGMA foreign_keys=off;";
sql.append("INSERT INTO activity_info (" + columns_insert + ") "
"SELECT " + columns_select + " "
"FROM activity_info_old "
"GROUP BY publisher_id, reconcile_stamp;");
sql.append("DROP TABLE activity_info_old;");
sql.append("PRAGMA foreign_keys=on;");

bool result = GetDB().Execute(sql.c_str());

if (!result) {
LOG(ERROR) << "DB: Error with MigrateV5toV6";
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
}
}

// to version 4
if (old_version < 4 && cur_version < 5) {
if (!MigrateV3toV4()) {
LOG(ERROR) << "DB: Error with MigrateV3toV4";
return transaction.Commit();
}

bool PublisherInfoDatabase::Migrate(int version) {
switch (version) {
case 2: {
return MigrateV1toV2();
}
case 3: {
return MigrateV2toV3();
}
case 4: {
return MigrateV3toV4();
}
case 5: {
return MigrateV4toV5();
}
case 6: {
return MigrateV5toV6();
}
default:
return false;
}
}

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

// to version 5
if (old_version < 5 && cur_version < 6) {
if (!MigrateV4toV5()) {
LOG(ERROR) << "DB: Error with MigrateV4toV5";
// We can't read databases newer than we were designed for.
if (meta_table_.GetCompatibleVersionNumber() > GetCurrentVersion()) {
LOG(WARNING) << "Publisher info database is too new.";
return sql::INIT_TOO_NEW;
}

const int old_version = GetTableVersionNumber();
const int current_version = GetCurrentVersion();
const int start_version = old_version + 1;

int migrated_version = old_version;
for (auto i = start_version; i <= current_version; i++) {
if (!Migrate(i)) {
LOG(ERROR) << "DB: Error with MigrateV" << (i - 1) << "toV" << i;
break;
}

migrated_version = i;
}

meta_table_.SetVersionNumber(cur_version);
meta_table_.SetVersionNumber(migrated_version);
return sql::INIT_OK;
}

Expand Down
12 changes: 11 additions & 1 deletion components/brave_rewards/browser/publisher_info_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class PublisherInfoDatabase {


// Returns the current version of the publisher info database
static int GetCurrentVersion();
int GetCurrentVersion();

void SetTestingCurrentVersion(int value);

// Vacuums the database. This will cause sqlite to defragment and collect
// unused space in the file. It can be VERY SLOW.
Expand All @@ -90,6 +92,8 @@ class PublisherInfoDatabase {

bool Init();

int GetTableVersionNumber();

private:

bool CreateContributionInfoTable();
Expand All @@ -115,6 +119,7 @@ class PublisherInfoDatabase {

sql::MetaTable& GetMetaTable();


NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
bool MigrateV1toV2();

bool MigrateV2toV3();
Expand All @@ -123,12 +128,17 @@ class PublisherInfoDatabase {

bool MigrateV4toV5();

bool MigrateV5toV6();

bool Migrate(int version);

sql::InitStatus EnsureCurrentVersion();

sql::Database db_;
sql::MetaTable meta_table_;
const base::FilePath db_path_;
bool initialized_;
int testing_current_version_;

std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_;

Expand Down
Loading