Skip to content

Commit

Permalink
Adds claim date to promotion table
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Dec 11, 2019
1 parent e719a0f commit f122644
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 8 deletions.
103 changes: 97 additions & 6 deletions components/brave_rewards/browser/database/database_promotion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ bool DatabasePromotion::CreateTable(sql::Database* db) {
return true;
}

return CreateTableV10(db);
const int version = GetCurrentDBVersion();

if (version >= 13) {
return CreateTableV13(db);
}

if (version >= 10) {
return CreateTableV10(db);
}

NOTREACHED();
return false;
}

bool DatabasePromotion::CreateTableV10(sql::Database* db) {
Expand All @@ -84,15 +95,92 @@ bool DatabasePromotion::CreateTableV10(sql::Database* db) {
return db->Execute(query.c_str());
}

bool DatabasePromotion::CreateTableV13(sql::Database* db) {
const std::string query = base::StringPrintf(
"CREATE TABLE %s ("
"%s_id TEXT NOT NULL,"
"version INTEGER NOT NULL,"
"type INTEGER NOT NULL,"
"public_keys TEXT NOT NULL,"
"suggestions INTEGER NOT NULL DEFAULT 0,"
"approximate_value DOUBLE NOT NULL DEFAULT 0,"
"status INTEGER NOT NULL DEFAULT 0,"
"expires_at TIMESTAMP NOT NULL,"
"claimed_at TIMESTAMP,"
"created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,"
"PRIMARY KEY (%s_id)"
")",
table_name_,
table_name_,
table_name_);

return db->Execute(query.c_str());
}

bool DatabasePromotion::CreateIndex(sql::Database* db) {
return CreateIndexV10(db);
return CreateIndexV13(db);
}

bool DatabasePromotion::CreateIndexV10(sql::Database* db) {
const std::string id = base::StringPrintf("%s_id", table_name_);
return this->InsertIndex(db, table_name_, id);
}

bool DatabasePromotion::CreateIndexV13(sql::Database* db) {
const std::string id = base::StringPrintf("%s_id", table_name_);
return this->InsertIndex(db, table_name_, id);
}

bool DatabasePromotion::Migrate(sql::Database* db, const int target) {
switch (target) {
case 10: {
return MigrateToV10(db);
}
case 13: {
return MigrateToV13(db);
}
default: {
NOTREACHED();
return false;
}
}
}

bool DatabasePromotion::MigrateToV10(sql::Database* db) {
if (!CreateTableV10(db)) {
return false;
}

if (!CreateIndexV10(db)) {
return false;
}

return true;
}

bool DatabasePromotion::MigrateToV13(sql::Database* db) {
if (!db->DoesTableExist(table_name_)) {
if (!CreateTableV13(db)) {
return false;
}
}

const char column[] = "claimed_at";
if (!db->DoesColumnExist(table_name_, column)) {
const std::string query = base::StringPrintf(
"ALTER TABLE %s ADD %s TIMESTAMP",
table_name_,
column);

sql::Statement statement(
db->GetCachedStatement(SQL_FROM_HERE, query.c_str()));

return statement.Run();
}

return true;
}

bool DatabasePromotion::InsertOrUpdate(
sql::Database* db,
ledger::PromotionPtr info) {
Expand All @@ -108,8 +196,8 @@ bool DatabasePromotion::InsertOrUpdate(
const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s "
"(%s_id, version, type, public_keys, suggestions, "
"approximate_value, status, expires_at) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
"approximate_value, status, expires_at, claimed_at) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
table_name_,
table_name_);

Expand All @@ -124,6 +212,7 @@ bool DatabasePromotion::InsertOrUpdate(
statement.BindDouble(5, info->approximate_value);
statement.BindInt(6, static_cast<int>(info->status));
statement.BindInt64(7, info->expires_at);
statement.BindInt64(8, info->claimed_at);

if (!statement.Run()) {
return false;
Expand All @@ -147,7 +236,7 @@ ledger::PromotionPtr DatabasePromotion::GetRecord(

const std::string query = base::StringPrintf(
"SELECT %s_id, version, type, public_keys, suggestions, "
"approximate_value, status, expires_at FROM %s WHERE %s_id=?",
"approximate_value, status, expires_at, claimed_at FROM %s WHERE %s_id=?",
table_name_,
table_name_,
table_name_);
Expand All @@ -168,6 +257,7 @@ ledger::PromotionPtr DatabasePromotion::GetRecord(
info->approximate_value = statement.ColumnDouble(5);
info->status = static_cast<ledger::PromotionStatus>(statement.ColumnInt(6));
info->expires_at = statement.ColumnInt64(7);
info->claimed_at = statement.ColumnInt64(8);
info->credentials = creds_->GetRecord(db, info->id);

return info;
Expand All @@ -176,7 +266,7 @@ ledger::PromotionPtr DatabasePromotion::GetRecord(
ledger::PromotionMap DatabasePromotion::GetAllRecords(sql::Database* db) {
const std::string query = base::StringPrintf(
"SELECT %s_id, version, type, public_keys, suggestions, "
"approximate_value, status, expires_at FROM %s",
"approximate_value, status, expires_at, claimed_at FROM %s",
table_name_,
table_name_);

Expand All @@ -194,6 +284,7 @@ ledger::PromotionMap DatabasePromotion::GetAllRecords(sql::Database* db) {
info->approximate_value = statement.ColumnDouble(5);
info->status = static_cast<ledger::PromotionStatus>(statement.ColumnInt(6));
info->expires_at = statement.ColumnInt64(7);
info->claimed_at = statement.ColumnInt64(8);
info->credentials = creds_->GetRecord(db, info->id);

map.insert(std::make_pair(info->id, std::move(info)));
Expand Down
10 changes: 10 additions & 0 deletions components/brave_rewards/browser/database/database_promotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class DatabasePromotion: public DatabaseTable {

bool CreateIndex(sql::Database* db) override;

bool Migrate(sql::Database* db, const int target);

bool InsertOrUpdate(sql::Database* db, ledger::PromotionPtr info);

ledger::PromotionPtr GetRecord(sql::Database* db, const std::string& id);
Expand All @@ -39,6 +41,14 @@ class DatabasePromotion: public DatabaseTable {

bool CreateIndexV10(sql::Database* db);

bool CreateTableV13(sql::Database* db);

bool CreateIndexV13(sql::Database* db);

bool MigrateToV10(sql::Database* db);

bool MigrateToV13(sql::Database* db);

std::unique_ptr<DatabasePromotionCreds> creds_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace brave_rewards {

namespace {

const int kCurrentVersionNumber = 12;
const int kCurrentVersionNumber = 13;
const int kCompatibleVersionNumber = 1;

} // namespace
Expand Down Expand Up @@ -761,6 +761,19 @@ bool PublisherInfoDatabase::MigrateV11toV12() {
return transaction.Commit();
}

bool PublisherInfoDatabase::MigrateV12toV13() {
sql::Transaction transaction(&GetDB());
if (!transaction.Begin()) {
return false;
}

if (!promotion_->Migrate(&GetDB(), 13)) {
return false;
}

return transaction.Commit();
}

bool PublisherInfoDatabase::Migrate(int version) {
switch (version) {
case 2: {
Expand Down Expand Up @@ -796,7 +809,11 @@ bool PublisherInfoDatabase::Migrate(int version) {
case 12: {
return MigrateV11toV12();
}
case 13: {
return MigrateV12toV13();
}
default:
NOTREACHED();
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class PublisherInfoDatabase {

bool MigrateV11toV12();

bool MigrateV12toV13();

bool Migrate(int version);

sql::InitStatus EnsureCurrentVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,26 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv11tov12_ContributionInfo) {
EXPECT_EQ(list.at(3)->publisher_key, "reddit.com");
}

TEST_F(PublisherInfoDatabaseTest, Migrationv12tov13) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
CreateMigrationDatabase(&temp_dir, &db_file, 12, 13);
EXPECT_TRUE(publisher_info_database_->Init());

ASSERT_EQ(publisher_info_database_->GetTableVersionNumber(), 13);

const std::string schema = publisher_info_database_->GetSchema();
EXPECT_EQ(schema, GetSchemaString(13));
}

TEST_F(PublisherInfoDatabaseTest, Migrationv12tov13_Promotion) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
CreateMigrationDatabase(&temp_dir, &db_file, 12, 13);
EXPECT_TRUE(publisher_info_database_->Init());
EXPECT_EQ(CountTableRows("promotion"), 1);
}

TEST_F(PublisherInfoDatabaseTest, DeleteActivityInfo) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
Expand Down
Binary file not shown.
42 changes: 42 additions & 0 deletions test/data/rewards-data/migration/publisher_info_schema_v13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
index|activity_info_publisher_id_index|activity_info|CREATE INDEX activity_info_publisher_id_index ON activity_info (publisher_id)
index|contribution_info_publishers_contribution_id_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_contribution_id_index ON contribution_info_publishers (contribution_id)
index|contribution_info_publishers_publisher_key_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_publisher_key_index ON contribution_info_publishers (publisher_key)
index|pending_contribution_publisher_id_index|pending_contribution|CREATE INDEX pending_contribution_publisher_id_index ON pending_contribution (publisher_id)
index|promotion_creds_promotion_id_index|promotion_creds|CREATE INDEX promotion_creds_promotion_id_index ON promotion_creds (promotion_id)
index|promotion_promotion_id_index|promotion|CREATE INDEX promotion_promotion_id_index ON promotion (promotion_id)
index|recurring_donation_publisher_id_index|recurring_donation|CREATE INDEX recurring_donation_publisher_id_index ON recurring_donation (publisher_id)
index|server_publisher_amounts_publisher_key_index|server_publisher_amounts|CREATE INDEX server_publisher_amounts_publisher_key_index ON server_publisher_amounts (publisher_key)
index|server_publisher_banner_publisher_key_index|server_publisher_banner|CREATE INDEX server_publisher_banner_publisher_key_index ON server_publisher_banner (publisher_key)
index|server_publisher_info_publisher_key_index|server_publisher_info|CREATE INDEX server_publisher_info_publisher_key_index ON server_publisher_info (publisher_key)
index|server_publisher_links_publisher_key_index|server_publisher_links|CREATE INDEX server_publisher_links_publisher_key_index ON server_publisher_links (publisher_key)
index|sqlite_autoindex_activity_info_1|activity_info|
index|sqlite_autoindex_contribution_info_1|contribution_info|
index|sqlite_autoindex_media_publisher_info_1|media_publisher_info|
index|sqlite_autoindex_meta_1|meta|
index|sqlite_autoindex_promotion_1|promotion|
index|sqlite_autoindex_promotion_creds_1|promotion_creds|
index|sqlite_autoindex_publisher_info_1|publisher_info|
index|sqlite_autoindex_recurring_donation_1|recurring_donation|
index|sqlite_autoindex_server_publisher_amounts_1|server_publisher_amounts|
index|sqlite_autoindex_server_publisher_banner_1|server_publisher_banner|
index|sqlite_autoindex_server_publisher_info_1|server_publisher_info|
index|sqlite_autoindex_server_publisher_links_1|server_publisher_links|
index|unblinded_tokens_token_id_index|unblinded_tokens|CREATE INDEX unblinded_tokens_token_id_index ON unblinded_tokens (token_id)
table|activity_info|activity_info|CREATE TABLE activity_info(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_old" (publisher_id) ON DELETE CASCADE)
table|contribution_info|contribution_info|CREATE TABLE contribution_info (contribution_id TEXT NOT NULL,amount DOUBLE NOT NULL,type INTEGER NOT NULL,step INTEGER NOT NULL DEFAULT -1,retry_count INTEGER NOT NULL DEFAULT -1,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (contribution_id))
table|contribution_info_publishers|contribution_info_publishers|CREATE TABLE contribution_info_publishers (contribution_id TEXT NOT NULL,publisher_key TEXT NOT NULL,total_amount DOUBLE NOT NULL,contributed_amount DOUBLE,CONSTRAINT fk_contribution_info_publishers_contribution_id FOREIGN KEY (contribution_id) REFERENCES "contribution_info_temp" (contribution_id) ON DELETE CASCADE,CONSTRAINT fk_contribution_info_publishers_publisher_id FOREIGN KEY (publisher_key) REFERENCES publisher_info (publisher_id))
table|contribution_queue|contribution_queue|CREATE TABLE contribution_queue (contribution_queue_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,type INTEGER NOT NULL,amount DOUBLE NOT NULL,partial INTEGER NOT NULL DEFAULT 0,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)
table|contribution_queue_publishers|contribution_queue_publishers|CREATE TABLE contribution_queue_publishers (contribution_queue_id INTEGER NOT NULL,publisher_key TEXT NOT NULL,amount_percent DOUBLE NOT NULL,CONSTRAINT fk_contribution_queue_publishers_publisher_key FOREIGN KEY (publisher_key) REFERENCES publisher_info (publisher_id),CONSTRAINT fk_contribution_queue_publishers_id FOREIGN KEY (contribution_queue_id) REFERENCES contribution_queue (contribution_queue_id) ON DELETE CASCADE)
table|media_publisher_info|media_publisher_info|CREATE TABLE media_publisher_info(media_key TEXT NOT NULL PRIMARY KEY UNIQUE,publisher_id LONGVARCHAR NOT NULL,CONSTRAINT fk_media_publisher_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE)
table|meta|meta|CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)
table|pending_contribution|pending_contribution|CREATE TABLE pending_contribution (pending_contribution_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,publisher_id LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,added_date INTEGER DEFAULT 0 NOT NULL,viewing_id LONGVARCHAR NOT NULL,type INTEGER NOT NULL,CONSTRAINT fk_pending_contribution_publisher_id FOREIGN KEY (publisher_id) REFERENCES publisher_info (publisher_id) ON DELETE CASCADE)
table|promotion|promotion|CREATE TABLE promotion (promotion_id TEXT NOT NULL,version INTEGER NOT NULL,type INTEGER NOT NULL,public_keys TEXT NOT NULL,suggestions INTEGER NOT NULL DEFAULT 0,approximate_value DOUBLE NOT NULL DEFAULT 0,status INTEGER NOT NULL DEFAULT 0,expires_at TIMESTAMP NOT NULL,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, claimed_at TIMESTAMP,PRIMARY KEY (promotion_id))
table|promotion_creds|promotion_creds|CREATE TABLE promotion_creds (promotion_id TEXT UNIQUE NOT NULL,tokens TEXT NOT NULL,blinded_creds TEXT NOT NULL,signed_creds TEXT,public_key TEXT,batch_proof TEXT,claim_id TEXT,CONSTRAINT fk_promotion_creds_promotion_id FOREIGN KEY (promotion_id) REFERENCES promotion (promotion_id) ON DELETE CASCADE)
table|publisher_info|publisher_info|CREATE TABLE publisher_info(publisher_id LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,excluded INTEGER DEFAULT 0 NOT NULL,name TEXT NOT NULL,favIcon TEXT NOT NULL,url TEXT NOT NULL,provider TEXT NOT NULL)
table|recurring_donation|recurring_donation|CREATE TABLE recurring_donation(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_old" (publisher_id) ON DELETE CASCADE)
table|server_publisher_amounts|server_publisher_amounts|CREATE TABLE server_publisher_amounts (publisher_key LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,CONSTRAINT server_publisher_amounts_unique UNIQUE (publisher_key, amount) CONSTRAINT fk_server_publisher_amounts_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE)
table|server_publisher_banner|server_publisher_banner|CREATE TABLE server_publisher_banner (publisher_key LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,title TEXT,description TEXT,background TEXT,logo TEXT,CONSTRAINT fk_server_publisher_banner_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE)
table|server_publisher_info|server_publisher_info|CREATE TABLE server_publisher_info (publisher_key LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,status INTEGER DEFAULT 0 NOT NULL,excluded INTEGER DEFAULT 0 NOT NULL,address TEXT NOT NULL)
table|server_publisher_links|server_publisher_links|CREATE TABLE server_publisher_links (publisher_key LONGVARCHAR NOT NULL,provider TEXT,link TEXT,CONSTRAINT server_publisher_links_unique UNIQUE (publisher_key, provider) CONSTRAINT fk_server_publisher_links_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE)
table|sqlite_sequence|sqlite_sequence|CREATE TABLE sqlite_sequence(name,seq)
table|unblinded_tokens|unblinded_tokens|CREATE TABLE unblinded_tokens (token_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,token_value TEXT,public_key TEXT,value DOUBLE NOT NULL DEFAULT 0,promotion_id TEXT,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,CONSTRAINT fk_unblinded_tokens_promotion_id FOREIGN KEY (promotion_id) REFERENCES promotion (promotion_id) ON DELETE CASCADE)
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,9 @@ struct Promotion {
double approximate_value;
PromotionStatus status;
uint64 expires_at;
PromotionCreds? credentials;
uint64 claimed_at;
bool legacy_claimed;
PromotionCreds? credentials;
};

struct PromotionCreds {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,10 @@ void Promotion::FinishPromotion(
[](const ledger::Result _){});
}

const uint64_t current_time =
static_cast<uint64_t>(base::Time::Now().ToDoubleT());
promotion->status = ledger::PromotionStatus::FINISHED;
promotion->claimed_at = current_time;
ledger_->InsertOrUpdatePromotion(
std::move(promotion),
[](const ledger::Result _){});
Expand Down

0 comments on commit f122644

Please sign in to comment.