Add unique index to contribution_info_publishers
This commit is contained in:
@@ -721,3 +721,44 @@ IN_PROC_BROWSER_TEST_F(
|
||||
EXPECT_EQ(expires_at, 1640995200ul);
|
||||
}
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
RewardsDatabaseBrowserTest,
|
||||
Migration_21_ContributionInfoPublishers) {
|
||||
{
|
||||
base::ScopedAllowBlockingForTesting allow_blocking;
|
||||
InitDB();
|
||||
|
||||
EXPECT_EQ(CountTableRows("contribution_info_publishers"), 3);
|
||||
|
||||
ledger::ContributionPublisherList list;
|
||||
const std::string query =
|
||||
"SELECT contribution_id, publisher_key, total_amount, "
|
||||
"contributed_amount "
|
||||
"FROM contribution_info_publishers";
|
||||
sql::Statement info_sql(db_.GetUniqueStatement(query.c_str()));
|
||||
while (info_sql.Step()) {
|
||||
auto contribution_publishers = ledger::ContributionPublisher::New();
|
||||
contribution_publishers->contribution_id = info_sql.ColumnString(0);
|
||||
contribution_publishers->publisher_key = info_sql.ColumnString(1);
|
||||
contribution_publishers->total_amount = info_sql.ColumnDouble(2);
|
||||
contribution_publishers->contributed_amount = info_sql.ColumnDouble(3);
|
||||
|
||||
list.push_back(std::move(contribution_publishers));
|
||||
}
|
||||
EXPECT_EQ(static_cast<int>(list.size()), 3);
|
||||
|
||||
EXPECT_EQ(list.at(0)->contribution_id, "1");
|
||||
EXPECT_EQ(list.at(0)->publisher_key, "123");
|
||||
EXPECT_EQ(list.at(0)->total_amount, 15.0);
|
||||
EXPECT_EQ(list.at(0)->contributed_amount, 30.0);
|
||||
EXPECT_EQ(list.at(1)->contribution_id, "2");
|
||||
EXPECT_EQ(list.at(1)->publisher_key, "456");
|
||||
EXPECT_EQ(list.at(1)->total_amount, 30.0);
|
||||
EXPECT_EQ(list.at(1)->contributed_amount, 45.0);
|
||||
EXPECT_EQ(list.at(2)->contribution_id, "3");
|
||||
EXPECT_EQ(list.at(2)->publisher_key, "789");
|
||||
EXPECT_EQ(list.at(2)->total_amount, 100.0);
|
||||
EXPECT_EQ(list.at(2)->contributed_amount, 150.0);
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -19,6 +19,7 @@ index|sku_order_items_order_item_id_index|sku_order_items|CREATE INDEX sku_order
|
||||
index|sku_transaction_order_id_index|sku_transaction|CREATE INDEX sku_transaction_order_id_index ON sku_transaction (order_id)
|
||||
index|sqlite_autoindex_activity_info_1|activity_info|
|
||||
index|sqlite_autoindex_contribution_info_1|contribution_info|
|
||||
index|sqlite_autoindex_contribution_info_publishers_1|contribution_info_publishers|
|
||||
index|sqlite_autoindex_creds_batch_1|creds_batch|
|
||||
index|sqlite_autoindex_creds_batch_2|creds_batch|
|
||||
index|sqlite_autoindex_media_publisher_info_1|media_publisher_info|
|
||||
@@ -37,7 +38,7 @@ index|unblinded_tokens_creds_id_index|unblinded_tokens|CREATE INDEX unblinded_to
|
||||
index|unblinded_tokens_redeem_id_index|unblinded_tokens|CREATE INDEX unblinded_tokens_redeem_id_index ON unblinded_tokens (redeem_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))
|
||||
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, processor INTEGER NOT NULL DEFAULT 1,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)
|
||||
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 contribution_info_publishers_unique UNIQUE (contribution_id, publisher_key))
|
||||
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)
|
||||
table|creds_batch|creds_batch|CREATE TABLE creds_batch (creds_id TEXT PRIMARY KEY NOT NULL,trigger_id TEXT NOT NULL,trigger_type INT NOT NULL,creds TEXT NOT NULL,blinded_creds TEXT NOT NULL,signed_creds TEXT,public_key TEXT,batch_proof TEXT,status INT NOT NULL DEFAULT 0,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,CONSTRAINT creds_batch_unique UNIQUE (trigger_id, trigger_type))
|
||||
|
||||
+10
@@ -167,6 +167,9 @@ bool DatabaseContributionInfo::Migrate(
|
||||
case 17: {
|
||||
return MigrateToV17(transaction);
|
||||
}
|
||||
case 21: {
|
||||
return MigrateToV21(transaction);
|
||||
}
|
||||
default: {
|
||||
return true;
|
||||
}
|
||||
@@ -379,6 +382,13 @@ bool DatabaseContributionInfo::MigrateToV17(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DatabaseContributionInfo::MigrateToV21(
|
||||
ledger::DBTransaction* transaction) {
|
||||
DCHECK(transaction);
|
||||
|
||||
return publishers_->Migrate(transaction, 21);
|
||||
}
|
||||
|
||||
void DatabaseContributionInfo::InsertOrUpdate(
|
||||
ledger::ContributionInfoPtr info,
|
||||
ledger::ResultCallback callback) {
|
||||
|
||||
+2
@@ -83,6 +83,8 @@ class DatabaseContributionInfo: public DatabaseTable {
|
||||
|
||||
bool MigrateToV17(ledger::DBTransaction* transaction);
|
||||
|
||||
bool MigrateToV21(ledger::DBTransaction* transaction);
|
||||
|
||||
void OnGetRecord(
|
||||
ledger::DBCommandResponsePtr response,
|
||||
ledger::GetContributionInfoCallback callback);
|
||||
|
||||
Vendored
+87
-13
@@ -78,6 +78,30 @@ bool DatabaseContributionInfoPublishers::CreateTableV15(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DatabaseContributionInfoPublishers::CreateTableV21(
|
||||
ledger::DBTransaction* transaction) {
|
||||
DCHECK(transaction);
|
||||
|
||||
const std::string query = base::StringPrintf(
|
||||
"CREATE TABLE %s ("
|
||||
"contribution_id TEXT NOT NULL,"
|
||||
"publisher_key TEXT NOT NULL,"
|
||||
"total_amount DOUBLE NOT NULL,"
|
||||
"contributed_amount DOUBLE,"
|
||||
"CONSTRAINT %s_unique "
|
||||
" UNIQUE (contribution_id, publisher_key)"
|
||||
")",
|
||||
kTableName,
|
||||
kTableName);
|
||||
|
||||
auto command = ledger::DBCommand::New();
|
||||
command->type = ledger::DBCommand::Type::EXECUTE;
|
||||
command->command = query;
|
||||
transaction->commands.push_back(std::move(command));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DatabaseContributionInfoPublishers::CreateIndexV11(
|
||||
ledger::DBTransaction* transaction) {
|
||||
DCHECK(transaction);
|
||||
@@ -104,6 +128,19 @@ bool DatabaseContributionInfoPublishers::CreateIndexV15(
|
||||
return this->InsertIndex(transaction, kTableName, "publisher_key");
|
||||
}
|
||||
|
||||
bool DatabaseContributionInfoPublishers::CreateIndexV21(
|
||||
ledger::DBTransaction* transaction) {
|
||||
DCHECK(transaction);
|
||||
|
||||
bool success = this->InsertIndex(transaction, kTableName, "contribution_id");
|
||||
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this->InsertIndex(transaction, kTableName, "publisher_key");
|
||||
}
|
||||
|
||||
bool DatabaseContributionInfoPublishers::Migrate(
|
||||
ledger::DBTransaction* transaction,
|
||||
const int target) {
|
||||
@@ -116,6 +153,9 @@ bool DatabaseContributionInfoPublishers::Migrate(
|
||||
case 15: {
|
||||
return MigrateToV15(transaction);
|
||||
}
|
||||
case 21: {
|
||||
return MigrateToV21(transaction);
|
||||
}
|
||||
default: {
|
||||
return true;
|
||||
}
|
||||
@@ -187,6 +227,52 @@ bool DatabaseContributionInfoPublishers::MigrateToV15(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DatabaseContributionInfoPublishers::MigrateToV21(
|
||||
ledger::DBTransaction* transaction) {
|
||||
DCHECK(transaction);
|
||||
|
||||
const std::string temp_table_name = base::StringPrintf(
|
||||
"%s_temp",
|
||||
kTableName);
|
||||
|
||||
if (!RenameDBTable(transaction, kTableName, temp_table_name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string query =
|
||||
"DROP INDEX IF EXISTS contribution_info_publishers_contribution_id_index;"
|
||||
" DROP INDEX IF EXISTS contribution_info_publishers_publisher_key_index;";
|
||||
auto command = ledger::DBCommand::New();
|
||||
command->type = ledger::DBCommand::Type::EXECUTE;
|
||||
command->command = query;
|
||||
transaction->commands.push_back(std::move(command));
|
||||
|
||||
if (!CreateTableV21(transaction)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CreateIndexV21(transaction)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string> columns = {
|
||||
{ "contribution_id", "contribution_id" },
|
||||
{ "publisher_key", "publisher_key" },
|
||||
{ "total_amount", "total_amount" },
|
||||
{ "contributed_amount", "contributed_amount" }
|
||||
};
|
||||
|
||||
if (!MigrateDBTable(
|
||||
transaction,
|
||||
temp_table_name,
|
||||
kTableName,
|
||||
columns,
|
||||
true)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DatabaseContributionInfoPublishers::InsertOrUpdate(
|
||||
ledger::DBTransaction* transaction,
|
||||
ledger::ContributionInfoPtr info) {
|
||||
@@ -196,25 +282,13 @@ void DatabaseContributionInfoPublishers::InsertOrUpdate(
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string query_delete = base::StringPrintf(
|
||||
"DELETE FROM %s WHERE contribution_id = ? AND publisher_key = ?",
|
||||
kTableName);
|
||||
|
||||
const std::string query = base::StringPrintf(
|
||||
"INSERT INTO %s "
|
||||
"INSERT OR REPLACE INTO %s "
|
||||
"(contribution_id, publisher_key, total_amount, contributed_amount) "
|
||||
"VALUES (?, ?, ?, ?)",
|
||||
kTableName);
|
||||
|
||||
for (const auto& publisher : info->publishers) {
|
||||
auto command_delete = ledger::DBCommand::New();
|
||||
command_delete->type = ledger::DBCommand::Type::RUN;
|
||||
command_delete->command = query_delete;
|
||||
|
||||
BindString(command_delete.get(), 0, publisher->contribution_id);
|
||||
BindString(command_delete.get(), 1, publisher->publisher_key);
|
||||
transaction->commands.push_back(std::move(command_delete));
|
||||
|
||||
auto command = ledger::DBCommand::New();
|
||||
command->type = ledger::DBCommand::Type::RUN;
|
||||
command->command = query;
|
||||
|
||||
Vendored
+6
@@ -42,14 +42,20 @@ class DatabaseContributionInfoPublishers: public DatabaseTable {
|
||||
|
||||
bool CreateTableV15(ledger::DBTransaction* transaction);
|
||||
|
||||
bool CreateTableV21(ledger::DBTransaction* transaction);
|
||||
|
||||
bool CreateIndexV11(ledger::DBTransaction* transaction);
|
||||
|
||||
bool CreateIndexV15(ledger::DBTransaction* transaction);
|
||||
|
||||
bool CreateIndexV21(ledger::DBTransaction* transaction);
|
||||
|
||||
bool MigrateToV11(ledger::DBTransaction* transaction);
|
||||
|
||||
bool MigrateToV15(ledger::DBTransaction* transaction);
|
||||
|
||||
bool MigrateToV21(ledger::DBTransaction* transaction);
|
||||
|
||||
void OnGetRecordByContributionList(
|
||||
ledger::DBCommandResponsePtr response,
|
||||
ContributionPublisherListCallback callback);
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace {
|
||||
|
||||
const int kCurrentVersionNumber = 20;
|
||||
const int kCurrentVersionNumber = 21;
|
||||
const int kCompatibleVersionNumber = 1;
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user