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

Fixes users are not served ads due to failure to initialize Brave Ads when migrating to database schema 21 or above - 1.38.x #13186

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
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
Binary file modified vendor/bat-native-ads/data/test/database_schema_17.sqlite
Binary file not shown.
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace ads {
namespace database {

int32_t version() {
return 21;
return 22;
}

int32_t compatible_version() {
return 21;
return 22;
}

} // namespace database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ void ConversionQueue::MigrateToV21(mojom::DBTransaction* transaction) {
"conversion_id TEXT, "
"advertiser_public_key TEXT, "
"timestamp TIMESTAMP NOT NULL, "
"was_processed INTEGER NOT NULL DEFAULT 0)";
"was_processed INTEGER DEFAULT 0)";

mojom::DBCommandPtr command = mojom::DBCommand::New();
command->type = mojom::DBCommand::Type::EXECUTE;
Expand All @@ -503,7 +503,7 @@ void ConversionQueue::MigrateToV21(mojom::DBTransaction* transaction) {
const std::string& update_query = base::StringPrintf(
"UPDATE conversion_queue "
"SET ad_type = 'ad_notification' "
"WHERE ad_type == ''");
"WHERE ad_type IS NULL");

mojom::DBCommandPtr update_command = mojom::DBCommand::New();
update_command->type = mojom::DBCommand::Type::EXECUTE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,8 @@ void Conversions::Migrate(mojom::DBTransaction* transaction,
DCHECK(transaction);

switch (to_version) {
case 1: {
MigrateToV1(transaction);
break;
}

case 10: {
MigrateToV10(transaction);
break;
}

case 20: {
MigrateToV20(transaction);
case 22: {
MigrateToV22(transaction);
break;
}

Expand Down Expand Up @@ -224,54 +214,13 @@ void Conversions::OnGetConversions(mojom::DBCommandResponsePtr response,
callback(/* success */ true, conversions);
}

void Conversions::MigrateToV1(mojom::DBTransaction* transaction) {
void Conversions::MigrateToV22(mojom::DBTransaction* transaction) {
DCHECK(transaction);

util::Drop(transaction, "ad_conversions");

const std::string& query =
"CREATE TABLE ad_conversions "
"(creative_set_id TEXT NOT NULL, "
"type TEXT NOT NULL, "
"url_pattern TEXT NOT NULL, "
"observation_window INTEGER NOT NULL, "
"expiry_timestamp TIMESTAMP NOT NULL, "
"UNIQUE(creative_set_id, type, url_pattern) ON CONFLICT REPLACE, "
"PRIMARY KEY(creative_set_id, type, url_pattern))";

mojom::DBCommandPtr command = mojom::DBCommand::New();
command->type = mojom::DBCommand::Type::EXECUTE;
command->command = query;

transaction->commands.push_back(std::move(command));

util::CreateIndex(transaction, "ad_conversions", "creative_set_id");
}

void Conversions::MigrateToV10(mojom::DBTransaction* transaction) {
DCHECK(transaction);

util::Rename(transaction, "ad_conversions", "creative_ad_conversions");

const std::string& query =
"ALTER TABLE creative_ad_conversions "
"ADD COLUMN advertiser_public_key TEXT";

mojom::DBCommandPtr command = mojom::DBCommand::New();
command->type = mojom::DBCommand::Type::EXECUTE;
command->command = query;
transaction->commands.push_back(std::move(command));

util::CreateIndex(transaction, "creative_ad_conversions", "creative_set_id");
}

void Conversions::MigrateToV20(mojom::DBTransaction* transaction) {
DCHECK(transaction);

const std::string& temp_table_name = "creative_ad_conversions_temp";

const std::string& query =
"CREATE TABLE creative_ad_conversions_temp "
const std::string query =
"CREATE TABLE IF NOT EXISTS creative_ad_conversions "
"(creative_set_id TEXT NOT NULL, "
"type TEXT NOT NULL, "
"url_pattern TEXT NOT NULL, "
Expand All @@ -286,18 +235,6 @@ void Conversions::MigrateToV20(mojom::DBTransaction* transaction) {
command->command = query;

transaction->commands.push_back(std::move(command));

// Copy columns to temporary table
const std::vector<std::string>& columns = {
"creative_set_id", "type",
"url_pattern", "advertiser_public_key",
"observation_window", "expiry_timestamp"};

util::CopyColumns(transaction, "creative_ad_conversions", temp_table_name,
columns, /* should_drop */ true);

// Rename temporary table
util::Rename(transaction, temp_table_name, "creative_ad_conversions");
}

} // namespace table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ class Conversions final : public Table {
void OnGetConversions(mojom::DBCommandResponsePtr response,
GetConversionsCallback callback);

void MigrateToV1(mojom::DBTransaction* transaction);
void MigrateToV10(mojom::DBTransaction* transaction);
void MigrateToV20(mojom::DBTransaction* transaction);
void MigrateToV22(mojom::DBTransaction* transaction);
};

} // namespace table
Expand Down