Skip to content

Commit 177b00a

Browse files
committed
Refactor database
- removes category column - adds visits column Resolves brave/brave-browser#2163
1 parent 9c45f2f commit 177b00a

File tree

1 file changed

+74
-35
lines changed

1 file changed

+74
-35
lines changed

components/brave_rewards/browser/publisher_info_database.cc

+74-35
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace brave_rewards {
2222

2323
namespace {
2424

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

2828
} // namespace
@@ -154,10 +154,10 @@ bool PublisherInfoDatabase::CreateActivityInfoTable() {
154154
"("
155155
"publisher_id LONGVARCHAR NOT NULL,"
156156
"duration INTEGER DEFAULT 0 NOT NULL,"
157+
"visits INTEGER DEFAULT 0 NOT NULL,"
157158
"score DOUBLE DEFAULT 0 NOT NULL,"
158159
"percent INTEGER DEFAULT 0 NOT NULL,"
159160
"weight DOUBLE DEFAULT 0 NOT NULL,"
160-
"category INTEGER NOT NULL,"
161161
"month INTEGER NOT NULL,"
162162
"year INTEGER NOT NULL,"
163163
"reconcile_stamp INTEGER DEFAULT 0 NOT NULL,"
@@ -265,33 +265,31 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
265265

266266
sql::Statement activity_get(
267267
db_.GetUniqueStatement("SELECT publisher_id FROM activity_info WHERE "
268-
"publisher_id=? AND category=? "
269-
"AND month=? AND year=? AND reconcile_stamp=?"));
268+
"publisher_id=? AND month=? "
269+
"AND year=? AND reconcile_stamp=?"));
270270

271271
activity_get.BindString(0, info.id);
272-
activity_get.BindInt(1, info.category);
273-
activity_get.BindInt(2, info.month);
274-
activity_get.BindInt(3, info.year);
275-
activity_get.BindInt64(4, info.reconcile_stamp);
272+
activity_get.BindInt(1, info.month);
273+
activity_get.BindInt(2, info.year);
274+
activity_get.BindInt64(3, info.reconcile_stamp);
276275

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

286285
activity_info_update.BindInt64(0, (int)info.duration);
287286
activity_info_update.BindDouble(1, info.score);
288287
activity_info_update.BindInt64(2, (int)info.percent);
289288
activity_info_update.BindDouble(3, info.weight);
290289
activity_info_update.BindString(4, info.id);
291-
activity_info_update.BindInt(5, info.category);
292-
activity_info_update.BindInt(6, info.month);
293-
activity_info_update.BindInt(7, info.year);
294-
activity_info_update.BindInt64(8, info.reconcile_stamp);
290+
activity_info_update.BindInt(5, info.month);
291+
activity_info_update.BindInt(6, info.year);
292+
activity_info_update.BindInt64(7, info.reconcile_stamp);
295293

296294
return activity_info_update.Run();
297295
}
@@ -300,18 +298,17 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
300298
GetDB().GetCachedStatement(SQL_FROM_HERE,
301299
"INSERT INTO activity_info "
302300
"(publisher_id, duration, score, percent, "
303-
"weight, category, month, year, reconcile_stamp) "
301+
"weight, month, year, reconcile_stamp) "
304302
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"));
305303

306304
activity_info_insert.BindString(0, info.id);
307305
activity_info_insert.BindInt64(1, (int)info.duration);
308306
activity_info_insert.BindDouble(2, info.score);
309307
activity_info_insert.BindInt64(3, (int)info.percent);
310308
activity_info_insert.BindDouble(4, info.weight);
311-
activity_info_insert.BindInt(5, info.category);
312-
activity_info_insert.BindInt(6, info.month);
313-
activity_info_insert.BindInt(7, info.year);
314-
activity_info_insert.BindInt64(8, info.reconcile_stamp);
309+
activity_info_insert.BindInt(5, info.month);
310+
activity_info_insert.BindInt(6, info.year);
311+
activity_info_insert.BindInt64(7, info.reconcile_stamp);
315312

316313
return activity_info_insert.Run();
317314
}
@@ -386,7 +383,7 @@ bool PublisherInfoDatabase::Find(int start,
386383
return false;
387384

388385
std::string query = "SELECT ai.publisher_id, ai.duration, ai.score, ai.percent, "
389-
"ai.weight, pi.verified, pi.excluded, ai.category, ai.month, ai.year, pi.name, "
386+
"ai.weight, pi.verified, pi.excluded, ai.month, ai.year, pi.name, "
390387
"pi.url, pi.provider, pi.favIcon, ai.reconcile_stamp "
391388
"FROM activity_info AS ai "
392389
"INNER JOIN publisher_info AS pi ON ai.publisher_id = pi.publisher_id "
@@ -401,8 +398,8 @@ bool PublisherInfoDatabase::Find(int start,
401398
while (info_sql.Step()) {
402399
std::string id(info_sql.ColumnString(0));
403400
ledger::PUBLISHER_MONTH month(
404-
static_cast<ledger::PUBLISHER_MONTH>(info_sql.ColumnInt(8)));
405-
int year(info_sql.ColumnInt(9));
401+
static_cast<ledger::PUBLISHER_MONTH>(info_sql.ColumnInt(7)));
402+
int year(info_sql.ColumnInt(8));
406403

407404
ledger::PublisherInfo info(id, month, year);
408405
info.duration = info_sql.ColumnInt64(1);
@@ -411,15 +408,13 @@ bool PublisherInfoDatabase::Find(int start,
411408
info.percent = info_sql.ColumnInt64(3);
412409
info.weight = info_sql.ColumnDouble(4);
413410
info.verified = info_sql.ColumnBool(5);
414-
info.name = info_sql.ColumnString(10);
415-
info.url = info_sql.ColumnString(11);
416-
info.provider = info_sql.ColumnString(12);
417-
info.favicon_url = info_sql.ColumnString(13);
418-
info.reconcile_stamp = info_sql.ColumnInt64(14);
411+
info.name = info_sql.ColumnString(9);
412+
info.url = info_sql.ColumnString(10);
413+
info.provider = info_sql.ColumnString(11);
414+
info.favicon_url = info_sql.ColumnString(12);
415+
info.reconcile_stamp = info_sql.ColumnInt64(13);
419416

420417
info.excluded = static_cast<ledger::PUBLISHER_EXCLUDE>(info_sql.ColumnInt(6));
421-
info.category =
422-
static_cast<ledger::PUBLISHER_CATEGORY>(info_sql.ColumnInt(7));
423418

424419
list->push_back(info);
425420
}
@@ -461,9 +456,6 @@ std::string PublisherInfoDatabase::BuildClauses(int start,
461456
if (!filter.id.empty())
462457
clauses += " AND ai.publisher_id = ?";
463458

464-
if (filter.category != ledger::PUBLISHER_CATEGORY::ALL_CATEGORIES)
465-
clauses += " AND ai.category = ?";
466-
467459
if (filter.month != ledger::PUBLISHER_MONTH::ANY)
468460
clauses += " AND ai.month = ?";
469461

@@ -515,9 +507,6 @@ void PublisherInfoDatabase::BindFilter(sql::Statement& statement,
515507
if (!filter.id.empty())
516508
statement.BindString(column++, filter.id);
517509

518-
if (filter.category != ledger::PUBLISHER_CATEGORY::ALL_CATEGORIES)
519-
statement.BindInt(column++, filter.category);
520-
521510
if (filter.month != ledger::PUBLISHER_MONTH::ANY)
522511
statement.BindInt(column++, filter.month);
523512

@@ -865,6 +854,49 @@ bool PublisherInfoDatabase::MigrateV2toV3() {
865854
return CreatePendingContributionsIndex();
866855
}
867856

857+
bool PublisherInfoDatabase::MigrateV3toV4() {
858+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
859+
860+
// Activity info
861+
const char* activity = "activity_info";
862+
if (GetDB().DoesTableExist(activity)) {
863+
std::string sql = "ALTER TABLE activity_info RENAME TO activity_info_old;";
864+
865+
if (!GetDB().Execute(sql.c_str())) {
866+
return false;
867+
}
868+
869+
if (!CreateActivityInfoTable()) {
870+
return false;
871+
}
872+
873+
if (!CreateActivityInfoIndex()) {
874+
return false;
875+
}
876+
877+
std::string columns = "publisher_id, "
878+
"duration, "
879+
"score, "
880+
"percent, "
881+
"weight, "
882+
"month, "
883+
"year, "
884+
"reconcile_stamp";
885+
886+
sql = "PRAGMA foreign_keys=off;";
887+
sql.append("INSERT INTO activity_info (" + columns + ") "
888+
"SELECT " + columns + " "
889+
"FROM activity_info_old;");
890+
sql.append("UPDATE activity_info SET visits=5;");
891+
sql.append("DROP TABLE activity_info_old;");
892+
sql.append("PRAGMA foreign_keys=on;");
893+
894+
return GetDB().Execute(sql.c_str());
895+
}
896+
897+
return false;
898+
}
899+
868900
sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() {
869901
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
870902

@@ -891,6 +923,13 @@ sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() {
891923
}
892924
}
893925

926+
// to version 4
927+
if (old_version < 4 && cur_version < 5) {
928+
if (!MigrateV2toV3()) {
929+
LOG(ERROR) << "DB: Error with MigrateV3toV4";
930+
}
931+
}
932+
894933
meta_table_.SetVersionNumber(cur_version);
895934
return sql::INIT_OK;
896935
}

0 commit comments

Comments
 (0)