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

Browser no longer crashes when upgrading from a v3 to a v5+ database #2122

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Changes from all 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
139 changes: 125 additions & 14 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1045,48 +1045,139 @@ bool PublisherInfoDatabase::MigrateV1toV2() {
return false;
}

if (!CreateContributionInfoTable()) {
const char* name = "contribution_info";
sql = "CREATE TABLE ";
sql.append(name);
sql.append(
"("
"publisher_id LONGVARCHAR,"
"probi TEXT \"0\" NOT NULL,"
"date INTEGER NOT NULL,"
"category INTEGER NOT NULL,"
"month INTEGER NOT NULL,"
"year INTEGER NOT NULL,"
"CONSTRAINT fk_contribution_info_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
" ON DELETE CASCADE)");
if (!GetDB().Execute(sql.c_str())) {
return false;
}

if (!GetDB().Execute(
"CREATE INDEX IF NOT EXISTS contribution_info_publisher_id_index "
"ON contribution_info (publisher_id)")) {
return false;
}

if (!CreateContributionInfoIndex()) {
// Recurring_donation
name = "recurring_donation";
if (GetDB().DoesTableExist(name)) {
sql = " DROP TABLE ";
sql.append(name);
sql.append(" ; ");
}

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

if (!CreateRecurringTipsTable()) {
sql = "CREATE TABLE ";
sql.append(name);
sql.append(
"("
"publisher_id LONGVARCHAR NOT NULL PRIMARY KEY UNIQUE,"
"amount DOUBLE DEFAULT 0 NOT NULL,"
"added_date INTEGER DEFAULT 0 NOT NULL,"
"CONSTRAINT fk_recurring_donation_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
" ON DELETE CASCADE)");
if (!GetDB().Execute(sql.c_str())) {
return false;
}

return CreateRecurringTipsIndex();
return GetDB().Execute(
"CREATE INDEX IF NOT EXISTS recurring_donation_publisher_id_index "
"ON recurring_donation (publisher_id)");
}

bool PublisherInfoDatabase::MigrateV2toV3() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

if (!CreatePendingContributionsTable()) {
std::string sql;
const char* name = "pending_contribution";
// pending_contribution
const char* pending_contribution = "pending_contribution";
if (GetDB().DoesTableExist(pending_contribution)) {
sql.append(" DROP TABLE ");
sql.append(pending_contribution);
sql.append(" ; ");
}
if (!GetDB().Execute(sql.c_str())) {
return false;
}

sql = "CREATE TABLE ";
sql.append(name);
sql.append(
"("
"publisher_id LONGVARCHAR NOT NULL,"
"amount DOUBLE DEFAULT 0 NOT NULL,"
"added_date INTEGER DEFAULT 0 NOT NULL,"
"viewing_id LONGVARCHAR NOT NULL,"
"category INTEGER NOT NULL,"
"CONSTRAINT fk_pending_contribution_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
" ON DELETE CASCADE)");
if (!GetDB().Execute(sql.c_str())) {
return false;
}

return CreatePendingContributionsIndex();
return GetDB().Execute(
"CREATE INDEX IF NOT EXISTS pending_contribution_publisher_id_index "
"ON pending_contribution (publisher_id)");
}

bool PublisherInfoDatabase::MigrateV3toV4() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

// Activity info
const char* activity = "activity_info";
if (GetDB().DoesTableExist(activity)) {
const char* name = "activity_info";
if (GetDB().DoesTableExist(name)) {
std::string sql = "ALTER TABLE activity_info RENAME TO activity_info_old;";

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

if (!CreateActivityInfoTable()) {
sql = "CREATE TABLE ";
sql.append(name);
sql.append(
"("
"publisher_id LONGVARCHAR NOT NULL,"
"duration INTEGER DEFAULT 0 NOT NULL,"
"visits INTEGER DEFAULT 0 NOT NULL,"
"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) "
"CONSTRAINT fk_activity_info_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
" ON DELETE CASCADE)");
if (!GetDB().Execute(sql.c_str())) {
return false;
}

if (!CreateActivityInfoIndex()) {
if (!GetDB().Execute(
"CREATE INDEX IF NOT EXISTS activity_info_publisher_id_index "
"ON activity_info (publisher_id)")) {
return false;
}

Expand Down Expand Up @@ -1146,19 +1237,39 @@ bool PublisherInfoDatabase::MigrateV5toV6() {
return false;
}

const char* activity = "activity_info";
if (GetDB().DoesTableExist(activity)) {
const char* name = "activity_info";
if (GetDB().DoesTableExist(name)) {
std::string sql = "ALTER TABLE activity_info RENAME TO activity_info_old;";

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

if (!CreateActivityInfoTable()) {
sql = "CREATE TABLE ";
sql.append(name);
sql.append(
"("
"publisher_id LONGVARCHAR NOT NULL,"
"duration INTEGER DEFAULT 0 NOT NULL,"
"visits INTEGER DEFAULT 0 NOT NULL,"
"score DOUBLE DEFAULT 0 NOT NULL,"
"percent INTEGER DEFAULT 0 NOT NULL,"
"weight DOUBLE DEFAULT 0 NOT NULL,"
"reconcile_stamp INTEGER DEFAULT 0 NOT NULL,"
"CONSTRAINT activity_unique "
"UNIQUE (publisher_id, reconcile_stamp) "
"CONSTRAINT fk_activity_info_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
" ON DELETE CASCADE)");

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

if (!CreateActivityInfoIndex()) {
if (!GetDB().Execute(
"CREATE INDEX IF NOT EXISTS activity_info_publisher_id_index "
"ON activity_info (publisher_id)")) {
return false;
}

Expand Down