-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
attribution_storage_sql_migrations.cc
686 lines (597 loc) · 22.7 KB
/
attribution_storage_sql_migrations.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/attribution_reporting/attribution_storage_sql_migrations.h"
#include "base/check.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "components/aggregation_service/aggregation_service.mojom.h"
#include "content/browser/attribution_reporting/attribution_report.h"
#include "content/browser/attribution_reporting/common_source_info.h"
#include "content/browser/attribution_reporting/rate_limit_table.h"
#include "sql/database.h"
#include "sql/meta_table.h"
#include "sql/statement.h"
#include "sql/transaction.h"
namespace content {
namespace {
bool MigrateToVersion34(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. This results in smaller
// transactions, so it's less likely that a transaction's buffer will need to
// spill to disk. Also, if the database grows a lot and Chrome stops (user
// quit, process kill, etc.) during the migration process, per-migration
// transactions make it more likely that we'll make forward progress each time
// Chrome stops.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
// Create the new aggregatable_report_metadata table with initial_report_time.
// This follows the steps documented at
// https://sqlite.org/lang_altertable.html#otheralter. Other approaches, like
// using "ALTER ... ADD COLUMN" require setting a DEFAULT value for the column
// which is undesirable.
static constexpr char kNewAggregatableReportMetadataTableSql[] =
"CREATE TABLE new_aggregatable_report_metadata("
"aggregation_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"source_id INTEGER NOT NULL,"
"trigger_time INTEGER NOT NULL,"
"debug_key INTEGER,"
"external_report_id TEXT NOT NULL,"
"report_time INTEGER NOT NULL,"
"failed_send_attempts INTEGER NOT NULL,"
"initial_report_time INTEGER NOT NULL)";
if (!db->Execute(kNewAggregatableReportMetadataTableSql)) {
return false;
}
// Transfer the existing aggregatable_report_metadata rows to the new table,
// using the report_time value for initial_report_time.
static constexpr char kPopulateNewAggregatableReportMetadataSql[] =
"INSERT INTO new_aggregatable_report_metadata SELECT "
"aggregation_id,source_id,trigger_time,debug_key,external_report_id,"
"report_time,failed_send_attempts,report_time "
"FROM aggregatable_report_metadata";
if (!db->Execute(kPopulateNewAggregatableReportMetadataSql)) {
return false;
}
static constexpr char kDropOldAggregatableReportMetadataTableSql[] =
"DROP TABLE aggregatable_report_metadata";
if (!db->Execute(kDropOldAggregatableReportMetadataTableSql)) {
return false;
}
static constexpr char kRenameAggregatableReportMetadataTableSql[] =
"ALTER TABLE new_aggregatable_report_metadata "
"RENAME TO aggregatable_report_metadata";
if (!db->Execute(kRenameAggregatableReportMetadataTableSql)) {
return false;
}
// Create the pre-existing aggregatable_report_metadata table indices on the
// new table.
static constexpr char kAggregateSourceIdIndexSql[] =
"CREATE INDEX aggregate_source_id_idx "
"ON aggregatable_report_metadata(source_id)";
if (!db->Execute(kAggregateSourceIdIndexSql)) {
return false;
}
static constexpr char kAggregateTriggerTimeIndexSql[] =
"CREATE INDEX aggregate_trigger_time_idx "
"ON aggregatable_report_metadata(trigger_time)";
if (!db->Execute(kAggregateTriggerTimeIndexSql)) {
return false;
}
static constexpr char kAggregateReportTimeIndexSql[] =
"CREATE INDEX aggregate_report_time_idx "
"ON aggregatable_report_metadata(report_time)";
if (!db->Execute(kAggregateReportTimeIndexSql)) {
return false;
}
meta_table->SetVersionNumber(34);
return transaction.Commit();
}
bool MigrateToVersion35(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
static constexpr char kNewRateLimitsTableSql[] =
"CREATE TABLE new_rate_limits("
"id INTEGER PRIMARY KEY NOT NULL,"
"scope INTEGER NOT NULL,"
"source_id INTEGER NOT NULL,"
"source_site TEXT NOT NULL,"
"source_origin TEXT NOT NULL,"
"destination_site TEXT NOT NULL,"
"destination_origin TEXT NOT NULL,"
"reporting_origin TEXT NOT NULL,"
"time INTEGER NOT NULL,"
"expiry_time INTEGER NOT NULL)";
if (!db->Execute(kNewRateLimitsTableSql)) {
return false;
}
// Transfer the existing rows to the new table, inserting `base::Time()`
// as default values for the expiry_time column.
static constexpr char kPopulateNewRateLimitsTableSql[] =
"INSERT INTO new_rate_limits SELECT "
"id,scope,source_id,source_site,source_origin,destination_site,"
"destination_origin,reporting_origin,time,0 "
"FROM rate_limits";
if (!db->Execute(kPopulateNewRateLimitsTableSql)) {
return false;
}
static constexpr char kDropOldRateLimitsTableSql[] = "DROP TABLE rate_limits";
if (!db->Execute(kDropOldRateLimitsTableSql)) {
return false;
}
static constexpr char kRenameRateLimitsTableSql[] =
"ALTER TABLE new_rate_limits RENAME TO rate_limits";
if (!db->Execute(kRenameRateLimitsTableSql)) {
return false;
}
// Update the expiry_time column for the existing source rows.
static constexpr char kUpdateSourceExpiryTimeSql[] =
"UPDATE rate_limits SET expiry_time=? WHERE id=?";
sql::Statement update_source_expiry_time_statement(
db->GetUniqueStatement(kUpdateSourceExpiryTimeSql));
static_assert(static_cast<int>(RateLimitTable::Scope::kSource) == 0,
"update `scope=0` clause below");
static constexpr char kGetRateLimitsSql[] =
"SELECT R.id,R.time,I.expiry_time "
"FROM rate_limits R "
"LEFT JOIN sources I ON R.source_id=I.source_id "
"WHERE R.scope=0 "
"ORDER BY R.id";
sql::Statement get_rate_limits_statement(
db->GetUniqueStatement(kGetRateLimitsSql));
while (get_rate_limits_statement.Step()) {
base::Time source_expiry_time;
if (get_rate_limits_statement.GetColumnType(2) == sql::ColumnType::kNull) {
// Use maximum expiry time if there's no matching source.
source_expiry_time = get_rate_limits_statement.ColumnTime(1) +
kDefaultAttributionSourceExpiry;
} else {
source_expiry_time = get_rate_limits_statement.ColumnTime(2);
}
update_source_expiry_time_statement.Reset(/*clear_bound_vars=*/true);
update_source_expiry_time_statement.BindTime(0, source_expiry_time);
update_source_expiry_time_statement.BindInt64(
1, get_rate_limits_statement.ColumnInt64(0));
if (!update_source_expiry_time_statement.Run()) {
return false;
}
}
if (!get_rate_limits_statement.Succeeded()) {
return false;
}
// Create the rate_limits table indices on the new table.
static constexpr char kRateLimitSourceSiteReportingOriginIndexSql[] =
"CREATE INDEX rate_limit_source_site_reporting_origin_idx "
"ON rate_limits"
"(scope,source_site,reporting_origin)";
if (!db->Execute(kRateLimitSourceSiteReportingOriginIndexSql)) {
return false;
}
static constexpr char kRateLimitReportingOriginIndexSql[] =
"CREATE INDEX rate_limit_reporting_origin_idx "
"ON rate_limits(scope,destination_site,source_site)";
if (!db->Execute(kRateLimitReportingOriginIndexSql)) {
return false;
}
static constexpr char kRateLimitTimeIndexSql[] =
"CREATE INDEX rate_limit_time_idx ON rate_limits(time)";
if (!db->Execute(kRateLimitTimeIndexSql)) {
return false;
}
static constexpr char kRateLimitImpressionIdIndexSql[] =
"CREATE INDEX rate_limit_source_id_idx "
"ON rate_limits(source_id)";
if (!db->Execute(kRateLimitImpressionIdIndexSql)) {
return false;
}
meta_table->SetVersionNumber(35);
return transaction.Commit();
}
bool MigrateToVersion36(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
static constexpr char kDropOldIndexSql[] = "DROP INDEX sources_by_origin";
if (!db->Execute(kDropOldIndexSql)) {
return false;
}
static constexpr char kCreateNewIndexSql[] =
"CREATE INDEX active_sources_by_source_origin "
"ON sources(source_origin)"
"WHERE event_level_active=1 OR aggregatable_active=1";
if (!db->Execute(kCreateNewIndexSql)) {
return false;
}
meta_table->SetVersionNumber(36);
return transaction.Commit();
}
bool MigrateToVersion37(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
static constexpr char kNewDedupKeyTableSql[] =
"CREATE TABLE new_dedup_keys("
"source_id INTEGER NOT NULL,"
"report_type INTEGER NOT NULL,"
"dedup_key INTEGER NOT NULL,"
"PRIMARY KEY(source_id,report_type,dedup_key))WITHOUT ROWID";
if (!db->Execute(kNewDedupKeyTableSql)) {
return false;
}
static_assert(static_cast<int>(AttributionReport::Type::kEventLevel) == 0,
"update the report type value `0` below");
// Transfer the existing rows to the new table, inserting
// `Attribution::Type::kEventLevel` as default values for the
// report_type column.
static constexpr char kPopulateNewDedupKeyTableSql[] =
"INSERT INTO new_dedup_keys SELECT "
"source_id,0,dedup_key "
"FROM dedup_keys";
if (!db->Execute(kPopulateNewDedupKeyTableSql)) {
return false;
}
static constexpr char kDropOldDedupKeyTableSql[] = "DROP TABLE dedup_keys";
if (!db->Execute(kDropOldDedupKeyTableSql)) {
return false;
}
static constexpr char kRenameDedupKeyTableSql[] =
"ALTER TABLE new_dedup_keys RENAME TO dedup_keys";
if (!db->Execute(kRenameDedupKeyTableSql)) {
return false;
}
meta_table->SetVersionNumber(37);
return transaction.Commit();
}
bool MigrateToVersion38(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
static constexpr char kNewSourceTableSql[] =
"CREATE TABLE new_sources("
"source_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"source_event_id INTEGER NOT NULL,"
"source_origin TEXT NOT NULL,"
"destination_origin TEXT NOT NULL,"
"reporting_origin TEXT NOT NULL,"
"source_time INTEGER NOT NULL,"
"expiry_time INTEGER NOT NULL,"
"event_report_window_time INTEGER NOT NULL,"
"aggregatable_report_window_time INTEGER NOT NULL,"
"num_attributions INTEGER NOT NULL,"
"event_level_active INTEGER NOT NULL,"
"aggregatable_active INTEGER NOT NULL,"
"destination_site TEXT NOT NULL,"
"source_type INTEGER NOT NULL,"
"attribution_logic INTEGER NOT NULL,"
"priority INTEGER NOT NULL,"
"source_site TEXT NOT NULL,"
"debug_key INTEGER,"
"aggregatable_budget_consumed INTEGER NOT NULL,"
"aggregatable_source BLOB NOT NULL,"
"filter_data BLOB NOT NULL)";
if (!db->Execute(kNewSourceTableSql)) {
return false;
}
// Transfer the existing rows to the new table, inserting
// `expiry_time` as default values for the event_report_window_time
// and aggregatable_report_window_time columns.
static constexpr char kPopulateNewSourceTableSql[] =
"INSERT INTO new_sources SELECT "
"source_id,source_event_id,source_origin,destination_origin,"
"reporting_origin,source_time,expiry_time,expiry_time,expiry_time,"
"num_attributions,event_level_active,aggregatable_active,"
"destination_site,source_type,attribution_logic,priority,"
"source_site,debug_key,aggregatable_budget_consumed,"
"aggregatable_source,filter_data "
"FROM sources";
if (!db->Execute(kPopulateNewSourceTableSql)) {
return false;
}
static constexpr char kDropOldSourceTableSql[] = "DROP TABLE sources";
if (!db->Execute(kDropOldSourceTableSql)) {
return false;
}
static constexpr char kRenameSourceTableSql[] =
"ALTER TABLE new_sources RENAME TO sources";
if (!db->Execute(kRenameSourceTableSql)) {
return false;
}
// Create the sources table indices on the new table.
static constexpr char kConversionDestinationIndexSql[] =
"CREATE INDEX sources_by_active_destination_site_reporting_origin "
"ON sources"
"(event_level_active,aggregatable_active,destination_site,"
"reporting_origin)";
if (!db->Execute(kConversionDestinationIndexSql)) {
return false;
}
static constexpr char kImpressionExpiryIndexSql[] =
"CREATE INDEX sources_by_expiry_time "
"ON sources(expiry_time)";
if (!db->Execute(kImpressionExpiryIndexSql)) {
return false;
}
static constexpr char kImpressionOriginIndexSql[] =
"CREATE INDEX active_sources_by_source_origin "
"ON sources(source_origin)"
"WHERE event_level_active=1 OR aggregatable_active=1";
if (!db->Execute(kImpressionOriginIndexSql)) {
return false;
}
static constexpr char kImpressionSiteReportingOriginIndexSql[] =
"CREATE INDEX active_unattributed_sources_by_site_reporting_origin "
"ON sources(source_site,reporting_origin)"
"WHERE event_level_active=1 AND num_attributions=0 AND "
"aggregatable_active=1 AND aggregatable_budget_consumed=0";
if (!db->Execute(kImpressionSiteReportingOriginIndexSql)) {
return false;
}
meta_table->SetVersionNumber(38);
return transaction.Commit();
}
bool MigrateToVersion39(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
// Create the new aggregatable_report_metadata table with
// aggregation_coordinator. This follows the steps documented at
// https://sqlite.org/lang_altertable.html#otheralter. Other approaches, like
// using "ALTER ... ADD COLUMN" require setting a DEFAULT value for the column
// which is undesirable.
static constexpr char kNewAggregatableReportMetadataTableSql[] =
"CREATE TABLE new_aggregatable_report_metadata("
"aggregation_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"source_id INTEGER NOT NULL,"
"trigger_time INTEGER NOT NULL,"
"debug_key INTEGER,"
"external_report_id TEXT NOT NULL,"
"report_time INTEGER NOT NULL,"
"failed_send_attempts INTEGER NOT NULL,"
"initial_report_time INTEGER NOT NULL,"
"aggregation_coordinator INTEGER NOT NULL)";
if (!db->Execute(kNewAggregatableReportMetadataTableSql)) {
return false;
}
// Transfer the existing aggregatable_report_metadata rows to the new table,
// using
// `aggregation_service::mojom::AggregationCoordinator::kDefault`
// for aggregation_coordinator.
static_assert(
static_cast<int>(
::aggregation_service::mojom::AggregationCoordinator::kDefault) == 0,
"update the statement below");
static constexpr char kPopulateNewAggregatableReportMetadataSql[] =
"INSERT INTO new_aggregatable_report_metadata SELECT "
"aggregation_id,source_id,trigger_time,debug_key,external_report_id,"
"report_time,failed_send_attempts,initial_report_time,0 "
"FROM aggregatable_report_metadata";
if (!db->Execute(kPopulateNewAggregatableReportMetadataSql)) {
return false;
}
static constexpr char kDropOldAggregatableReportMetadataTableSql[] =
"DROP TABLE aggregatable_report_metadata";
if (!db->Execute(kDropOldAggregatableReportMetadataTableSql)) {
return false;
}
static constexpr char kRenameAggregatableReportMetadataTableSql[] =
"ALTER TABLE new_aggregatable_report_metadata "
"RENAME TO aggregatable_report_metadata";
if (!db->Execute(kRenameAggregatableReportMetadataTableSql)) {
return false;
}
// Create the pre-existing aggregatable_report_metadata table indices on the
// new table.
static constexpr char kAggregateSourceIdIndexSql[] =
"CREATE INDEX aggregate_source_id_idx "
"ON aggregatable_report_metadata(source_id)";
if (!db->Execute(kAggregateSourceIdIndexSql)) {
return false;
}
static constexpr char kAggregateTriggerTimeIndexSql[] =
"CREATE INDEX aggregate_trigger_time_idx "
"ON aggregatable_report_metadata(trigger_time)";
if (!db->Execute(kAggregateTriggerTimeIndexSql)) {
return false;
}
static constexpr char kAggregateReportTimeIndexSql[] =
"CREATE INDEX aggregate_report_time_idx "
"ON aggregatable_report_metadata(report_time)";
if (!db->Execute(kAggregateReportTimeIndexSql)) {
return false;
}
meta_table->SetVersionNumber(39);
return transaction.Commit();
}
bool MigrateToVersion40(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
// Create the new aggregatable_contributions table with desired primary-key
// structure.
static constexpr char kCreateNewTableSql[] =
"CREATE TABLE new_aggregatable_contributions("
"aggregation_id INTEGER NOT NULL,"
"contribution_id INTEGER NOT NULL,"
"key_high_bits INTEGER NOT NULL,"
"key_low_bits INTEGER NOT NULL,"
"value INTEGER NOT NULL,"
"PRIMARY KEY(aggregation_id,contribution_id))WITHOUT ROWID";
if (!db->Execute(kCreateNewTableSql)) {
return false;
}
// Transfer the existing aggregatable_contributions rows to the new table,
static constexpr char kPopulateNewTableSql[] =
"INSERT INTO new_aggregatable_contributions SELECT "
"aggregation_id,contribution_id,key_high_bits,key_low_bits,value "
"FROM aggregatable_contributions";
if (!db->Execute(kPopulateNewTableSql)) {
return false;
}
// This implicitly drops the contribution_aggregation_id_idx index.
static constexpr char kDropOldTableSql[] =
"DROP TABLE aggregatable_contributions";
if (!db->Execute(kDropOldTableSql)) {
return false;
}
static constexpr char kRenameTableSql[] =
"ALTER TABLE new_aggregatable_contributions "
"RENAME TO aggregatable_contributions";
if (!db->Execute(kRenameTableSql)) {
return false;
}
meta_table->SetVersionNumber(40);
return transaction.Commit();
}
bool MigrateToVersion41(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
static constexpr char kAddAttestationHeaderColumnSql[] =
"ALTER TABLE aggregatable_report_metadata "
"ADD COLUMN attestation_token TEXT";
if (!db->Execute(kAddAttestationHeaderColumnSql)) {
return false;
}
meta_table->SetVersionNumber(41);
return transaction.Commit();
}
bool MigrateToVersion42(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
static constexpr char kRenameDestinationOriginSql[] =
"ALTER TABLE rate_limits "
"RENAME COLUMN destination_origin TO context_origin";
if (!db->Execute(kRenameDestinationOriginSql)) {
return false;
}
static_assert(static_cast<int>(RateLimitTable::Scope::kSource) == 0);
static constexpr char kSetContextOriginSql[] =
"UPDATE rate_limits SET context_origin=source_origin WHERE scope=0";
if (!db->Execute(kSetContextOriginSql)) {
return false;
}
static constexpr char kDropSourceOriginSql[] =
"ALTER TABLE rate_limits DROP COLUMN source_origin";
if (!db->Execute(kDropSourceOriginSql)) {
return false;
}
meta_table->SetVersionNumber(42);
return transaction.Commit();
}
bool MigrateToVersion43(sql::Database* db, sql::MetaTable* meta_table) {
// Wrap each migration in its own transaction. See comment in
// `MigrateToVersion34`.
sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
static constexpr char kRenameExpiryTimeSql[] =
"ALTER TABLE rate_limits "
"RENAME COLUMN expiry_time TO source_expiry_or_attribution_time";
if (!db->Execute(kRenameExpiryTimeSql)) {
return false;
}
static_assert(static_cast<int>(RateLimitTable::Scope::kAttribution) == 1);
static constexpr char kSetAttributionTimeSql[] =
"UPDATE rate_limits "
"SET source_expiry_or_attribution_time=time WHERE scope=1";
if (!db->Execute(kSetAttributionTimeSql)) {
return false;
}
meta_table->SetVersionNumber(43);
return transaction.Commit();
}
} // namespace
bool UpgradeAttributionStorageSqlSchema(sql::Database* db,
sql::MetaTable* meta_table) {
DCHECK(db);
DCHECK(meta_table);
base::ThreadTicks start_timestamp;
if (base::ThreadTicks::IsSupported()) {
start_timestamp = base::ThreadTicks::Now();
}
if (meta_table->GetVersionNumber() == 33) {
if (!MigrateToVersion34(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 34) {
if (!MigrateToVersion35(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 35) {
if (!MigrateToVersion36(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 36) {
if (!MigrateToVersion37(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 37) {
if (!MigrateToVersion38(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 38) {
if (!MigrateToVersion39(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 39) {
if (!MigrateToVersion40(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 40) {
if (!MigrateToVersion41(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 41) {
if (!MigrateToVersion42(db, meta_table)) {
return false;
}
}
if (meta_table->GetVersionNumber() == 42) {
if (!MigrateToVersion43(db, meta_table)) {
return false;
}
}
// Add similar if () blocks for new versions here.
if (base::ThreadTicks::IsSupported()) {
base::UmaHistogramMediumTimes("Conversions.Storage.MigrationTime",
base::ThreadTicks::Now() - start_timestamp);
}
return true;
}
} // namespace content