File: btm_database_migrator.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (336 lines) | stat: -rw-r--r-- 11,614 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 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/btm/btm_database_migrator.h"

#include "base/check_deref.h"
#include "base/strings/stringprintf.h"
#include "content/browser/btm/btm_database.h"
#include "sql/meta_table.h"
#include "sql/statement.h"

namespace content {

using internal::BtmDatabaseMigrator;

BtmDatabaseMigrator::BtmDatabaseMigrator(sql::Database* const db,
                                         sql::MetaTable* const meta_table)
    : db_(CHECK_DEREF(db)), meta_table_(CHECK_DEREF(meta_table)) {}

bool MigrateBtmSchemaToLatestVersion(sql::Database& db,
                                     sql::MetaTable& meta_table) {
  BtmDatabaseMigrator migrator(&db, &meta_table);

  for (int next_version = meta_table.GetVersionNumber() + 1;
       next_version <= BtmDatabase::kLatestSchemaVersion; next_version++) {
    switch (next_version) {
      case 2:
        if (!migrator.MigrateSchemaVersionFrom1To2()) {
          return false;
        }
        break;
      case 3:
        if (!migrator.MigrateSchemaVersionFrom2To3()) {
          return false;
        }
        break;
      case 4:
        if (!migrator.MigrateSchemaVersionFrom3To4()) {
          return false;
        }
        break;
      case 5:
        if (!migrator.MigrateSchemaVersionFrom4To5()) {
          return false;
        }
        break;
      case 6:
        if (!migrator.MigrateSchemaVersionFrom5To6()) {
          return false;
        }
        break;
      case 7:
        if (!migrator.MigrateSchemaVersionFrom6To7()) {
          return false;
        }
        break;
      case 8:
        if (!migrator.MigrateSchemaVersionFrom7To8()) {
          return false;
        }
        break;
      case 9:
        if (!migrator.MigrateSchemaVersionFrom8To9()) {
          return false;
        }
        break;
    }
  }
  return true;
}

bool BtmDatabaseMigrator::MigrateSchemaVersionFrom1To2() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(db_->HasActiveTransactions());
  // First make a new table that allows for null values in the timestamps
  // columns.
  static constexpr char kNewTableSql[] =  // clang-format off
      "CREATE TABLE new_bounces("
          "site TEXT PRIMARY KEY NOT NULL,"
          "first_site_storage_time INTEGER,"
          "last_site_storage_time INTEGER,"
          "first_user_interaction_time INTEGER,"
          "last_user_interaction_time INTEGER,"
          "first_stateful_bounce_time INTEGER,"
          "last_stateful_bounce_time INTEGER,"
          "first_stateless_bounce_time INTEGER,"
          "last_stateless_bounce_time INTEGER)";
  // clang-format on
  DCHECK(db_->IsSQLValid(kNewTableSql));
  if (!db_->Execute(kNewTableSql)) {
    return false;
  }

  static constexpr char kCopyEverythingSql[] =
      "INSERT INTO new_bounces "
      "SELECT * FROM bounces";
  DCHECK(db_->IsSQLValid(kCopyEverythingSql));
  if (!db_->Execute(kCopyEverythingSql)) {
    return false;
  }

  const std::array<std::string, 8> timestamp_columns{
      "first_site_storage_time",     "last_site_storage_time",
      "first_user_interaction_time", "last_user_interaction_time",
      "first_stateless_bounce_time", "last_stateless_bounce_time",
      "first_stateful_bounce_time",  "last_stateful_bounce_time"};

  for (const std::string& column : timestamp_columns) {
    std::string command = base::StringPrintf(
        "UPDATE new_bounces "
        "SET %s=NULL "
        "WHERE %s=0 ",
        column.c_str(), column.c_str());
    sql::Statement s_nullify(db_->GetUniqueStatement(command));

    if (!s_nullify.Run()) {
      return false;
    }
  }

  // Replace the first_stateless_bounce with the first bounce overall.
  // We have to first case on whether either of the bounce fields are NULL,
  // since MIN will return NULL if either are NULL.
  static constexpr char kReplaceFirstStatelessBounceSql[] =  // clang-format off
    "UPDATE new_bounces "
      "SET first_stateless_bounce_time = "
        "CASE "
          "WHEN first_stateful_bounce_time IS NULL "
            "THEN first_stateless_bounce_time "
          "WHEN first_stateless_bounce_time IS NULL "
            "THEN first_stateful_bounce_time "
          "ELSE MIN(first_stateful_bounce_time,first_stateless_bounce_time) "
        "END";
  // clang-format on
  DCHECK(db_->IsSQLValid(kReplaceFirstStatelessBounceSql));
  if (!db_->Execute(kReplaceFirstStatelessBounceSql)) {
    return false;
  }

  // Replace the last_stateless_bounce with the last bounce overall.
  // We have to first case on whether either of the bounce fields are NULL,
  // since MAX will return NULL if either are NULL.
  static constexpr char kReplaceLastStatelessBounceSql[] =  // clang-format off
      "UPDATE new_bounces "
        "SET last_stateless_bounce_time = "
          "CASE "
            "WHEN last_stateful_bounce_time IS NULL "
              "THEN last_stateless_bounce_time "
            "WHEN last_stateless_bounce_time IS NULL "
              "THEN last_stateful_bounce_time "
            "ELSE MAX(last_stateful_bounce_time,last_stateless_bounce_time) "
          "END";
  // clang-format on
  DCHECK(db_->IsSQLValid(kReplaceLastStatelessBounceSql));
  if (!db_->Execute(kReplaceLastStatelessBounceSql)) {
    return false;
  }
  // Rename this column to be reflect its new purpose.
  static constexpr char kRenameFirstStatelessBounceTimeSql[] =
      "ALTER TABLE new_bounces RENAME COLUMN first_stateless_bounce_time TO "
      "first_bounce_time";
  DCHECK(db_->IsSQLValid(kRenameFirstStatelessBounceTimeSql));
  if (!db_->Execute(kRenameFirstStatelessBounceTimeSql)) {
    return false;
  }

  // Rename this column to be reflect its new purpose.
  static constexpr char kRenameLastStatelessBounceTimeSql[] =
      "ALTER TABLE new_bounces RENAME COLUMN last_stateless_bounce_time TO "
      "last_bounce_time";
  if (!db_->Execute(kRenameLastStatelessBounceTimeSql)) {
    return false;
  }

  // Replace the old `bounces` table with the new one.
  static constexpr char kDropOldTableSql[] = "DROP TABLE bounces";
  if (!db_->Execute(kDropOldTableSql)) {
    return false;
  }

  static constexpr char kReplaceOldTable[] =
      "ALTER TABLE new_bounces RENAME TO bounces";
  if (!db_->Execute(kReplaceOldTable)) {
    return false;
  }

  return meta_table_->SetVersionNumber(2) &&
         meta_table_->SetCompatibleVersionNumber(
             std::min(2, BtmDatabase::kMinCompatibleSchemaVersion));
}

bool BtmDatabaseMigrator::MigrateSchemaVersionFrom2To3() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(db_->HasActiveTransactions());

  return db_->Execute(
             "ALTER TABLE bounces ADD COLUMN first_web_authn_assertion_time "
             "INTEGER DEFAULT NULL") &&
         db_->Execute(
             "ALTER TABLE bounces ADD COLUMN last_web_authn_assertion_time "
             "INTEGER DEFAULT NULL") &&
         meta_table_->SetVersionNumber(3) &&
         meta_table_->SetCompatibleVersionNumber(
             std::min(3, BtmDatabase::kMinCompatibleSchemaVersion));
}

bool BtmDatabaseMigrator::MigrateSchemaVersionFrom3To4() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(db_->HasActiveTransactions());

  static constexpr char kCreatePopupsTableSql[] =  // clang-format off
    "CREATE TABLE popups("
      "opener_site TEXT NOT NULL,"
      "popup_site TEXT NOT NULL,"
      "access_id INT64,"
      "last_popup_time INTEGER,"
      "PRIMARY KEY (`opener_site`,`popup_site`)"
    ")";
  // clang-format on
  DCHECK(db_->IsSQLValid(kCreatePopupsTableSql));

  return db_->Execute(kCreatePopupsTableSql) &&
         meta_table_->SetVersionNumber(4) &&
         meta_table_->SetCompatibleVersionNumber(
             std::min(4, BtmDatabase::kMinCompatibleSchemaVersion));
}

bool BtmDatabaseMigrator::MigrateSchemaVersionFrom4To5() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(db_->HasActiveTransactions());

  return db_->Execute(
             "ALTER TABLE popups ADD COLUMN is_current_interaction "
             "BOOLEAN DEFAULT NULL") &&
         meta_table_->SetVersionNumber(5) &&
         meta_table_->SetCompatibleVersionNumber(
             std::min(5, BtmDatabase::kMinCompatibleSchemaVersion));
}

bool BtmDatabaseMigrator::MigrateSchemaVersionFrom5To6() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(db_->HasActiveTransactions());

  static constexpr char kCreateConfigTableSql[] =  // clang-format off
    "CREATE TABLE config("
      "key TEXT NOT NULL,"
      "int_value INTEGER,"
      "PRIMARY KEY (`key`)"
    ")";
  // clang-format on
  DCHECK(db_->IsSQLValid(kCreateConfigTableSql));

  if (!db_->Execute(kCreateConfigTableSql)) {
    return false;
  }

  if (int result;
      meta_table_->GetValue(BtmDatabase::kPrepopulatedKey, &result)) {
    static constexpr char kInsertValueSql[] =
        "INSERT OR REPLACE INTO config(key,int_value) VALUES(?,?)";
    DCHECK(db_->IsSQLValid(kInsertValueSql));
    sql::Statement statement(db_->GetUniqueStatement(kInsertValueSql));
    statement.BindString(0, BtmDatabase::kPrepopulatedKey);
    statement.BindInt64(1, result);

    if (!statement.Run()) {
      return false;
    }

    if (!meta_table_->DeleteKey(BtmDatabase::kPrepopulatedKey)) {
      return false;
    }
  }

  return meta_table_->SetVersionNumber(6) &&
         meta_table_->SetCompatibleVersionNumber(
             std::min(6, BtmDatabase::kMinCompatibleSchemaVersion));
}

bool BtmDatabaseMigrator::MigrateSchemaVersionFrom6To7() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  CHECK(db_->HasActiveTransactions());

  static constexpr char kDeleteConfigSql[] = "DELETE FROM config WHERE key = ?";
  CHECK(db_->IsSQLValid(kDeleteConfigSql));
  sql::Statement statement(db_->GetUniqueStatement(kDeleteConfigSql));
  statement.BindString(0, BtmDatabase::kPrepopulatedKey);

  if (!statement.Run()) {
    return false;
  }

  return meta_table_->SetVersionNumber(7) &&
         meta_table_->SetCompatibleVersionNumber(
             std::min(6, BtmDatabase::kMinCompatibleSchemaVersion));
}

bool BtmDatabaseMigrator::MigrateSchemaVersionFrom7To8() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(db_->HasActiveTransactions());

  return db_->Execute(
             "ALTER TABLE popups ADD COLUMN is_authentication_interaction "
             "BOOLEAN DEFAULT NULL") &&
         meta_table_->SetVersionNumber(8) &&
         meta_table_->SetCompatibleVersionNumber(
             std::min(8, BtmDatabase::kMinCompatibleSchemaVersion));
}

bool BtmDatabaseMigrator::MigrateSchemaVersionFrom8To9() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(db_->HasActiveTransactions());

  static constexpr char kRenameFirstUserInteractionTimeSql[] =
      "ALTER TABLE bounces RENAME COLUMN first_user_interaction_time TO "
      "first_user_activation_time";
  DCHECK(db_->IsSQLValid(kRenameFirstUserInteractionTimeSql));
  if (!db_->Execute(kRenameFirstUserInteractionTimeSql)) {
    return false;
  }

  static constexpr char kRenameLastUserInteractionTimeSql[] =
      "ALTER TABLE bounces RENAME COLUMN last_user_interaction_time TO "
      "last_user_activation_time";
  DCHECK(db_->IsSQLValid(kRenameLastUserInteractionTimeSql));
  if (!db_->Execute(kRenameLastUserInteractionTimeSql)) {
    return false;
  }

  return meta_table_->SetVersionNumber(9) &&
         meta_table_->SetCompatibleVersionNumber(
             std::min(9, BtmDatabase::kMinCompatibleSchemaVersion));
}

}  // namespace content