@@ -25,8 +25,8 @@ namespace brave_ads {
25
25
26
26
namespace {
27
27
28
- const int kCurrentVersionNumber = 6 ;
29
- const int kCompatibleVersionNumber = 6 ;
28
+ const int kCurrentVersionNumber = 7 ;
29
+ const int kCompatibleVersionNumber = 7 ;
30
30
31
31
} // namespace
32
32
@@ -342,24 +342,27 @@ bool BundleStateDatabase::CreateAdConversionsTable() {
342
342
// new constraints to the schema
343
343
const std::string sql = base::StringPrintf (
344
344
" CREATE TABLE %s "
345
- " (id INTEGER PRIMARY KEY, "
346
- " creative_set_id LONGVARCHAR NOT NULL, "
345
+ " (creative_set_id LONGVARCHAR NOT NULL, "
347
346
" type LONGVARCHAR NOT NULL, "
348
347
" url_pattern LONGVARCHAR NOT NULL, "
349
- " observation_window INTEGER NOT NULL)" ,
348
+ " observation_window INTEGER NOT NULL, "
349
+ " expiry_timestamp TIMESTAMP, "
350
+ " UNIQUE(creative_set_id, type, url_pattern), "
351
+ " PRIMARY KEY(creative_set_id, type, url_pattern))" ,
350
352
table_name);
351
353
352
354
return GetDB ().Execute (sql.c_str ());
353
355
}
354
356
355
- bool BundleStateDatabase::TruncateAdConversionsTable () {
357
+ bool BundleStateDatabase::PurgeExpiredAdConversions () {
356
358
DCHECK_CALLED_ON_VALID_SEQUENCE (sequence_checker_);
357
359
358
360
const bool is_initialized = Init ();
359
361
DCHECK (is_initialized);
360
362
361
363
const std::string sql =
362
- " DELETE FROM ad_conversions" ;
364
+ " DELETE FROM ad_conversions "
365
+ " WHERE strftime('%s','now') >= expiry_timestamp" ;
363
366
364
367
sql::Statement statement (GetDB ().GetUniqueStatement (sql.c_str ()));
365
368
@@ -378,16 +381,18 @@ bool BundleStateDatabase::InsertOrUpdateAdConversion(
378
381
" (creative_set_id, "
379
382
" type, "
380
383
" url_pattern, "
381
- " observation_window) VALUES (%s)" ,
382
- CreateBindingParameterPlaceholders (4 ).c_str ());
384
+ " observation_window, "
385
+ " expiry_timestamp) VALUES (%s)" ,
386
+ CreateBindingParameterPlaceholders (5 ).c_str ());
383
387
384
388
sql::Statement statement (GetDB ().GetUniqueStatement (sql.c_str ()));
385
389
386
390
statement.BindString (0 , info.creative_set_id );
387
391
statement.BindString (1 , info.type );
388
392
statement.BindString (2 , info.url_pattern );
389
- // Use BindInt64 for uint32_t types to avoid uint32_t to int32_t cast.
393
+ // Use BindInt64 for uint32_t types to avoid uint32_t to int32_t cast
390
394
statement.BindInt64 (3 , info.observation_window );
395
+ statement.BindInt64 (4 , info.expiry_timestamp );
391
396
392
397
return statement.Run ();
393
398
}
@@ -403,11 +408,10 @@ bool BundleStateDatabase::SaveBundleState(
403
408
return false ;
404
409
}
405
410
406
- // We are completely replacing the database here so truncate all the tables
407
411
if (!TruncateCategoriesTable () ||
408
412
!TruncateCreativeAdNotificationCategoriesTable () ||
409
413
!TruncateCreativeAdNotificationsTable () ||
410
- !TruncateAdConversionsTable ()) {
414
+ !PurgeExpiredAdConversions ()) {
411
415
GetDB ().RollbackTransaction ();
412
416
return false ;
413
417
}
@@ -525,7 +529,8 @@ bool BundleStateDatabase::GetAdConversions(
525
529
" ac.creative_set_id, "
526
530
" ac.type, "
527
531
" ac.url_pattern, "
528
- " ac.observation_window "
532
+ " ac.observation_window, "
533
+ " ac.expiry_timestamp "
529
534
" FROM ad_conversions AS ac" ;
530
535
531
536
sql::Statement statement (db_.GetUniqueStatement (sql.c_str ()));
@@ -536,6 +541,7 @@ bool BundleStateDatabase::GetAdConversions(
536
541
info.type = statement.ColumnString (1 );
537
542
info.url_pattern = statement.ColumnString (2 );
538
543
info.observation_window = statement.ColumnInt (3 );
544
+ info.expiry_timestamp = statement.ColumnInt64 (4 );
539
545
ad_conversions->emplace_back (info);
540
546
}
541
547
@@ -642,6 +648,11 @@ bool BundleStateDatabase::Migrate() {
642
648
break ;
643
649
}
644
650
651
+ case 6 : {
652
+ success = MigrateV6toV7 ();
653
+ break ;
654
+ }
655
+
645
656
default : {
646
657
NOTREACHED ();
647
658
break ;
@@ -781,4 +792,24 @@ bool BundleStateDatabase::MigrateV5toV6() {
781
792
return GetDB ().Execute (create_ad_info_table_sql.c_str ());
782
793
}
783
794
795
+ bool BundleStateDatabase::MigrateV6toV7 () {
796
+ const std::string drop_ad_conversions_table_sql =
797
+ " DROP TABLE IF EXISTS ad_conversions" ;
798
+ if (!GetDB ().Execute (drop_ad_conversions_table_sql.c_str ())) {
799
+ return false ;
800
+ }
801
+
802
+ const std::string create_ad_conversions_table_sql =
803
+ " CREATE TABLE ad_conversions "
804
+ " (creative_set_id LONGVARCHAR NOT NULL, "
805
+ " type LONGVARCHAR NOT NULL, "
806
+ " url_pattern LONGVARCHAR NOT NULL, "
807
+ " observation_window INTEGER NOT NULL, "
808
+ " expiry_timestamp TIMESTAMP, "
809
+ " UNIQUE(creative_set_id, type, url_pattern), "
810
+ " PRIMARY KEY(creative_set_id, type, url_pattern))" ;
811
+
812
+ return GetDB ().Execute (create_ad_conversions_table_sql.c_str ());
813
+ }
814
+
784
815
} // namespace brave_ads
0 commit comments