Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b86a018

Browse files
committedNov 20, 2018
Refactor database
- removes category column - adds visits column Resolves brave/brave-browser#2163
1 parent d2c5737 commit b86a018

File tree

2 files changed

+94
-39
lines changed

2 files changed

+94
-39
lines changed
 

‎components/brave_rewards/browser/publisher_info_database.cc

+93-39
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace brave_rewards {
2222

2323
namespace {
2424

25-
const int kCurrentVersionNumber = 2;
25+
const int kCurrentVersionNumber = 3;
2626
const int kCompatibleVersionNumber = 1;
2727

2828
} // namespace
@@ -152,10 +152,10 @@ bool PublisherInfoDatabase::CreateActivityInfoTable() {
152152
"("
153153
"publisher_id LONGVARCHAR NOT NULL,"
154154
"duration INTEGER DEFAULT 0 NOT NULL,"
155+
"visits INTEGER DEFAULT 0 NOT NULL,"
155156
"score DOUBLE DEFAULT 0 NOT NULL,"
156157
"percent INTEGER DEFAULT 0 NOT NULL,"
157158
"weight DOUBLE DEFAULT 0 NOT NULL,"
158-
"category INTEGER NOT NULL,"
159159
"month INTEGER NOT NULL,"
160160
"year INTEGER NOT NULL,"
161161
"reconcile_stamp INTEGER DEFAULT 0 NOT NULL,"
@@ -263,33 +263,31 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
263263

264264
sql::Statement activity_get(
265265
db_.GetUniqueStatement("SELECT publisher_id FROM activity_info WHERE "
266-
"publisher_id=? AND category=? "
267-
"AND month=? AND year=? AND reconcile_stamp=?"));
266+
"publisher_id=? AND month=? "
267+
"AND year=? AND reconcile_stamp=?"));
268268

269269
activity_get.BindString(0, info.id);
270-
activity_get.BindInt(1, info.category);
271-
activity_get.BindInt(2, info.month);
272-
activity_get.BindInt(3, info.year);
273-
activity_get.BindInt64(4, info.reconcile_stamp);
270+
activity_get.BindInt(1, info.month);
271+
activity_get.BindInt(2, info.year);
272+
activity_get.BindInt64(3, info.reconcile_stamp);
274273

275274
if (activity_get.Step()) {
276275
sql::Statement activity_info_update(
277276
GetDB().GetCachedStatement(SQL_FROM_HERE,
278277
"UPDATE activity_info SET "
279278
"duration=?, score=?, percent=?, "
280279
"weight=? WHERE "
281-
"publisher_id=? AND category=? "
282-
"AND month=? AND year=? AND reconcile_stamp=?"));
280+
"publisher_id=? AND month=? "
281+
"AND year=? AND reconcile_stamp=?"));
283282

284283
activity_info_update.BindInt64(0, (int)info.duration);
285284
activity_info_update.BindDouble(1, info.score);
286285
activity_info_update.BindInt64(2, (int)info.percent);
287286
activity_info_update.BindDouble(3, info.weight);
288287
activity_info_update.BindString(4, info.id);
289-
activity_info_update.BindInt(5, info.category);
290-
activity_info_update.BindInt(6, info.month);
291-
activity_info_update.BindInt(7, info.year);
292-
activity_info_update.BindInt64(8, info.reconcile_stamp);
288+
activity_info_update.BindInt(5, info.month);
289+
activity_info_update.BindInt(6, info.year);
290+
activity_info_update.BindInt64(7, info.reconcile_stamp);
293291

294292
return activity_info_update.Run();
295293
}
@@ -298,18 +296,17 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
298296
GetDB().GetCachedStatement(SQL_FROM_HERE,
299297
"INSERT INTO activity_info "
300298
"(publisher_id, duration, score, percent, "
301-
"weight, category, month, year, reconcile_stamp) "
299+
"weight, month, year, reconcile_stamp) "
302300
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"));
303301

304302
activity_info_insert.BindString(0, info.id);
305303
activity_info_insert.BindInt64(1, (int)info.duration);
306304
activity_info_insert.BindDouble(2, info.score);
307305
activity_info_insert.BindInt64(3, (int)info.percent);
308306
activity_info_insert.BindDouble(4, info.weight);
309-
activity_info_insert.BindInt(5, info.category);
310-
activity_info_insert.BindInt(6, info.month);
311-
activity_info_insert.BindInt(7, info.year);
312-
activity_info_insert.BindInt64(8, info.reconcile_stamp);
307+
activity_info_insert.BindInt(5, info.month);
308+
activity_info_insert.BindInt(6, info.year);
309+
activity_info_insert.BindInt64(7, info.reconcile_stamp);
313310

314311
return activity_info_insert.Run();
315312
}
@@ -384,7 +381,7 @@ bool PublisherInfoDatabase::Find(int start,
384381
return false;
385382

386383
std::string query = "SELECT ai.publisher_id, ai.duration, ai.score, ai.percent, "
387-
"ai.weight, pi.verified, pi.excluded, ai.category, ai.month, ai.year, pi.name, "
384+
"ai.weight, pi.verified, pi.excluded, ai.month, ai.year, pi.name, "
388385
"pi.url, pi.provider, pi.favIcon, ai.reconcile_stamp "
389386
"FROM activity_info AS ai "
390387
"INNER JOIN publisher_info AS pi ON ai.publisher_id = pi.publisher_id "
@@ -399,8 +396,8 @@ bool PublisherInfoDatabase::Find(int start,
399396
while (info_sql.Step()) {
400397
std::string id(info_sql.ColumnString(0));
401398
ledger::PUBLISHER_MONTH month(
402-
static_cast<ledger::PUBLISHER_MONTH>(info_sql.ColumnInt(8)));
403-
int year(info_sql.ColumnInt(9));
399+
static_cast<ledger::PUBLISHER_MONTH>(info_sql.ColumnInt(7)));
400+
int year(info_sql.ColumnInt(8));
404401

405402
ledger::PublisherInfo info(id, month, year);
406403
info.duration = info_sql.ColumnInt64(1);
@@ -409,15 +406,13 @@ bool PublisherInfoDatabase::Find(int start,
409406
info.percent = info_sql.ColumnInt64(3);
410407
info.weight = info_sql.ColumnDouble(4);
411408
info.verified = info_sql.ColumnBool(5);
412-
info.name = info_sql.ColumnString(10);
413-
info.url = info_sql.ColumnString(11);
414-
info.provider = info_sql.ColumnString(12);
415-
info.favicon_url = info_sql.ColumnString(13);
416-
info.reconcile_stamp = info_sql.ColumnInt64(14);
409+
info.name = info_sql.ColumnString(9);
410+
info.url = info_sql.ColumnString(10);
411+
info.provider = info_sql.ColumnString(11);
412+
info.favicon_url = info_sql.ColumnString(12);
413+
info.reconcile_stamp = info_sql.ColumnInt64(13);
417414

418415
info.excluded = static_cast<ledger::PUBLISHER_EXCLUDE>(info_sql.ColumnInt(6));
419-
info.category =
420-
static_cast<ledger::PUBLISHER_CATEGORY>(info_sql.ColumnInt(7));
421416

422417
list->push_back(info);
423418
}
@@ -459,9 +454,6 @@ std::string PublisherInfoDatabase::BuildClauses(int start,
459454
if (!filter.id.empty())
460455
clauses += " AND ai.publisher_id = ?";
461456

462-
if (filter.category != ledger::PUBLISHER_CATEGORY::ALL_CATEGORIES)
463-
clauses += " AND ai.category = ?";
464-
465457
if (filter.month != ledger::PUBLISHER_MONTH::ANY)
466458
clauses += " AND ai.month = ?";
467459

@@ -505,9 +497,6 @@ void PublisherInfoDatabase::BindFilter(sql::Statement& statement,
505497
if (!filter.id.empty())
506498
statement.BindString(column++, filter.id);
507499

508-
if (filter.category != ledger::PUBLISHER_CATEGORY::ALL_CATEGORIES)
509-
statement.BindInt(column++, filter.category);
510-
511500
if (filter.month != ledger::PUBLISHER_MONTH::ANY)
512501
statement.BindInt(column++, filter.month);
513502

@@ -751,6 +740,49 @@ bool PublisherInfoDatabase::MigrateV1toV2() {
751740
return CreateRecurringDonationIndex();
752741
}
753742

743+
bool PublisherInfoDatabase::MigrateV2toV3() {
744+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
745+
746+
// Activity info
747+
const char* activity = "activity_info";
748+
if (GetDB().DoesTableExist(activity)) {
749+
std::string sql = "ALTER TABLE activity_info RENAME TO activity_info_old;";
750+
751+
if (!GetDB().Execute(sql.c_str())) {
752+
return false;
753+
}
754+
755+
if (!CreateActivityInfoTable()) {
756+
return false;
757+
}
758+
759+
if (!CreateActivityInfoIndex()) {
760+
return false;
761+
}
762+
763+
std::string columns = "publisher_id, "
764+
"duration, "
765+
"score, "
766+
"percent, "
767+
"weight, "
768+
"month, "
769+
"year, "
770+
"reconcile_stamp";
771+
772+
sql = "PRAGMA foreign_keys=off;";
773+
sql.append("INSERT INTO activity_info (" + columns + ") "
774+
"SELECT " + columns + " "
775+
"FROM activity_info_old;");
776+
sql.append("UPDATE activity_info SET visits=5;");
777+
sql.append("DROP TABLE activity_info_old;");
778+
sql.append("PRAGMA foreign_keys=on;");
779+
780+
return GetDB().Execute(sql.c_str());
781+
}
782+
783+
return false;
784+
}
785+
754786
sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() {
755787
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
756788

@@ -763,10 +795,32 @@ sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() {
763795
const int old_version = meta_table_.GetVersionNumber();
764796
const int cur_version = GetCurrentVersion();
765797

766-
// Migration from version 1 to version 2
767-
if (old_version == 1 && cur_version == 2) {
768-
if (!MigrateV1toV2()) {
769-
LOG(ERROR) << "DB: Error with MigrateV1toV2";
798+
// Migration from version 1
799+
if (old_version == 1) {
800+
// to version 2
801+
if (cur_version < 3) {
802+
if (!MigrateV1toV2()) {
803+
LOG(ERROR) << "DB: Error with MigrateV1toV2";
804+
}
805+
}
806+
807+
// to version 3
808+
if (cur_version < 4) {
809+
if (!MigrateV2toV3()) {
810+
LOG(ERROR) << "DB: Error with MigrateV2toV3";
811+
}
812+
}
813+
814+
meta_table_.SetVersionNumber(cur_version);
815+
}
816+
817+
// Migration from version 2
818+
if (old_version == 2) {
819+
// to version 3
820+
if (cur_version < 4) {
821+
if (!MigrateV2toV3()) {
822+
LOG(ERROR) << "DB: Error with MigrateV2toV3";
823+
}
770824
}
771825

772826
meta_table_.SetVersionNumber(cur_version);

‎components/brave_rewards/browser/publisher_info_database.h

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class PublisherInfoDatabase {
8484

8585
sql::InitStatus EnsureCurrentVersion();
8686
bool MigrateV1toV2();
87+
bool MigrateV2toV3();
8788

8889
sql::Database db_;
8990
sql::MetaTable meta_table_;

0 commit comments

Comments
 (0)
Please sign in to comment.