summaryrefslogtreecommitdiffstats
path: root/dom/fs/parent/datamodel/SchemaVersion002.cpp
blob: 57d4736b88fb03ba96ec139a6f6fb393fac4d18c (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SchemaVersion002.h"

#include "FileSystemFileManager.h"
#include "FileSystemHashSource.h"
#include "FileSystemHashStorageFunction.h"
#include "ResultStatement.h"
#include "StartedTransaction.h"
#include "fs/FileSystemConstants.h"
#include "mozStorageHelper.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "nsID.h"

namespace mozilla::dom::fs {

namespace {

nsresult CreateFileIds(ResultConnection& aConn) {
  return aConn->ExecuteSimpleSQL(
      "CREATE TABLE IF NOT EXISTS FileIds ( "
      "fileId BLOB PRIMARY KEY, "
      "handle BLOB, "
      "FOREIGN KEY (handle) "
      "REFERENCES Files (handle) "
      "ON DELETE SET NULL ) "
      ";"_ns);
}

nsresult CreateMainFiles(ResultConnection& aConn) {
  return aConn->ExecuteSimpleSQL(
      "CREATE TABLE IF NOT EXISTS MainFiles ( "
      "handle BLOB UNIQUE, "
      "fileId BLOB UNIQUE, "
      "FOREIGN KEY (handle) REFERENCES Files (handle) "
      "ON DELETE CASCADE, "
      "FOREIGN KEY (fileId) REFERENCES FileIds (fileId) "
      "ON DELETE SET NULL ) "
      ";"_ns);
}

nsresult PopulateFileIds(ResultConnection& aConn) {
  return aConn->ExecuteSimpleSQL(
      "INSERT OR IGNORE INTO FileIds ( fileId, handle ) "
      "SELECT handle, handle FROM Files "
      ";"_ns);
}

nsresult PopulateMainFiles(ResultConnection& aConn) {
  return aConn->ExecuteSimpleSQL(
      "INSERT OR IGNORE INTO MainFiles ( fileId, handle ) "
      "SELECT handle, handle FROM Files "
      ";"_ns);
}

Result<Ok, QMResult> ClearInvalidFileIds(
    ResultConnection& aConn, data::FileSystemFileManager& aFileManager) {
  // We cant't just clear all file ids because if a file was accessed using
  // writable file stream a new file id was created which is not the same as
  // entry id.

  // Get all file ids first.
  QM_TRY_INSPECT(
      const auto& allFileIds,
      ([&aConn]() -> Result<nsTArray<FileId>, QMResult> {
        const nsLiteralCString allFileIdsQuery =
            "SELECT fileId FROM FileIds;"_ns;

        QM_TRY_UNWRAP(ResultStatement stmt,
                      ResultStatement::Create(aConn, allFileIdsQuery));

        nsTArray<FileId> fileIds;

        while (true) {
          QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());
          if (!moreResults) {
            break;
          }

          QM_TRY_UNWRAP(FileId fileId, stmt.GetFileIdByColumn(/* Column */ 0u));

          fileIds.AppendElement(fileId);
        }

        return std::move(fileIds);
      }()));

  // Filter out file ids which have non-zero-sized files on disk.
  QM_TRY_INSPECT(const auto& invalidFileIds,
                 ([&aFileManager](const nsTArray<FileId>& aFileIds)
                      -> Result<nsTArray<FileId>, QMResult> {
                   nsTArray<FileId> fileIds;

                   for (const auto& fileId : aFileIds) {
                     QM_TRY_UNWRAP(auto file, aFileManager.GetFile(fileId));

                     QM_TRY_INSPECT(const bool& exists,
                                    QM_TO_RESULT_INVOKE_MEMBER(file, Exists));

                     if (exists) {
                       QM_TRY_INSPECT(
                           const int64_t& fileSize,
                           QM_TO_RESULT_INVOKE_MEMBER(file, GetFileSize));

                       if (fileSize != 0) {
                         continue;
                       }

                       QM_TRY(QM_TO_RESULT(file->Remove(false)));
                     }

                     fileIds.AppendElement(fileId);
                   }

                   return std::move(fileIds);
                 }(allFileIds)));

  // Finally, clear invalid file ids.
  QM_TRY(([&aConn](const nsTArray<FileId>& aFileIds) -> Result<Ok, QMResult> {
    for (const auto& fileId : aFileIds) {
      const nsLiteralCString clearFileIdsQuery =
          "DELETE FROM FileIds "
          "WHERE fileId = :fileId "
          ";"_ns;

      QM_TRY_UNWRAP(ResultStatement stmt,
                    ResultStatement::Create(aConn, clearFileIdsQuery));

      QM_TRY(QM_TO_RESULT(stmt.BindFileIdByName("fileId"_ns, fileId)));

      QM_TRY(QM_TO_RESULT(stmt.Execute()));
    }

    return Ok{};
  }(invalidFileIds)));

  return Ok{};
}

Result<Ok, QMResult> ClearInvalidMainFiles(
    ResultConnection& aConn, data::FileSystemFileManager& aFileManager) {
  // We cant't just clear all main files because if a file was accessed using
  // writable file stream a new main file was created which is not the same as
  // entry id.

  // Get all main files first.
  QM_TRY_INSPECT(
      const auto& allMainFiles,
      ([&aConn]() -> Result<nsTArray<std::pair<EntryId, FileId>>, QMResult> {
        const nsLiteralCString allMainFilesQuery =
            "SELECT handle, fileId FROM MainFiles;"_ns;

        QM_TRY_UNWRAP(ResultStatement stmt,
                      ResultStatement::Create(aConn, allMainFilesQuery));

        nsTArray<std::pair<EntryId, FileId>> mainFiles;

        while (true) {
          QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());
          if (!moreResults) {
            break;
          }

          QM_TRY_UNWRAP(EntryId entryId,
                        stmt.GetEntryIdByColumn(/* Column */ 0u));
          QM_TRY_UNWRAP(FileId fileId, stmt.GetFileIdByColumn(/* Column */ 1u));

          mainFiles.AppendElement(std::pair<EntryId, FileId>(entryId, fileId));
        }

        return std::move(mainFiles);
      }()));

  // Filter out main files which have non-zero-sized files on disk.
  QM_TRY_INSPECT(
      const auto& invalidMainFiles,
      ([&aFileManager](const nsTArray<std::pair<EntryId, FileId>>& aMainFiles)
           -> Result<nsTArray<std::pair<EntryId, FileId>>, QMResult> {
        nsTArray<std::pair<EntryId, FileId>> mainFiles;

        for (const auto& mainFile : aMainFiles) {
          QM_TRY_UNWRAP(auto file, aFileManager.GetFile(mainFile.second));

          QM_TRY_INSPECT(const bool& exists,
                         QM_TO_RESULT_INVOKE_MEMBER(file, Exists));

          if (exists) {
            QM_TRY_INSPECT(const int64_t& fileSize,
                           QM_TO_RESULT_INVOKE_MEMBER(file, GetFileSize));

            if (fileSize != 0) {
              continue;
            }

            QM_TRY(QM_TO_RESULT(file->Remove(false)));
          }

          mainFiles.AppendElement(mainFile);
        }

        return std::move(mainFiles);
      }(allMainFiles)));

  // Finally, clear invalid main files.
  QM_TRY(([&aConn](const nsTArray<std::pair<EntryId, FileId>>& aMainFiles)
              -> Result<Ok, QMResult> {
    for (const auto& mainFile : aMainFiles) {
      const nsLiteralCString clearMainFilesQuery =
          "DELETE FROM MainFiles "
          "WHERE handle = :entryId AND fileId = :fileId "
          ";"_ns;

      QM_TRY_UNWRAP(ResultStatement stmt,
                    ResultStatement::Create(aConn, clearMainFilesQuery));

      QM_TRY(
          QM_TO_RESULT(stmt.BindEntryIdByName("entryId"_ns, mainFile.first)));
      QM_TRY(QM_TO_RESULT(stmt.BindFileIdByName("fileId"_ns, mainFile.second)));

      QM_TRY(QM_TO_RESULT(stmt.Execute()));
    }

    return Ok{};
  }(invalidMainFiles)));

  return Ok{};
}

nsresult ConnectUsagesToFileIds(ResultConnection& aConn) {
  QM_TRY(
      MOZ_TO_RESULT(aConn->ExecuteSimpleSQL("PRAGMA foreign_keys = OFF;"_ns)));

  auto turnForeignKeysBackOn = MakeScopeExit([&aConn]() {
    QM_WARNONLY_TRY(
        MOZ_TO_RESULT(aConn->ExecuteSimpleSQL("PRAGMA foreign_keys = ON;"_ns)));
  });

  QM_TRY_UNWRAP(auto transaction, StartedTransaction::Create(aConn));

  QM_TRY(MOZ_TO_RESULT(
      aConn->ExecuteSimpleSQL("DROP TABLE IF EXISTS migrateUsages ;"_ns)));

  QM_TRY(MOZ_TO_RESULT(aConn->ExecuteSimpleSQL(
      "CREATE TABLE migrateUsages ( "
      "handle BLOB PRIMARY KEY, "
      "usage INTEGER NOT NULL DEFAULT 0, "
      "tracked BOOLEAN NOT NULL DEFAULT 0 CHECK (tracked IN (0, 1)), "
      "CONSTRAINT handles_are_fileIds "
      "FOREIGN KEY (handle) "
      "REFERENCES FileIds (fileId) "
      "ON DELETE CASCADE ) "
      ";"_ns)));

  QM_TRY(MOZ_TO_RESULT(aConn->ExecuteSimpleSQL(
      "INSERT INTO migrateUsages ( handle, usage, tracked ) "
      "SELECT handle, usage, tracked FROM Usages ;"_ns)));

  QM_TRY(MOZ_TO_RESULT(aConn->ExecuteSimpleSQL("DROP TABLE Usages;"_ns)));

  QM_TRY(MOZ_TO_RESULT(aConn->ExecuteSimpleSQL(
      "ALTER TABLE migrateUsages RENAME TO Usages;"_ns)));

  QM_TRY(
      MOZ_TO_RESULT(aConn->ExecuteSimpleSQL("PRAGMA foreign_key_check;"_ns)));

  QM_TRY(MOZ_TO_RESULT(transaction.Commit()));

  return NS_OK;
}

nsresult CreateEntryNamesView(ResultConnection& aConn) {
  return aConn->ExecuteSimpleSQL(
      "CREATE VIEW IF NOT EXISTS EntryNames AS "
      "SELECT isFile, handle, parent, name FROM Entries INNER JOIN ( "
      "SELECT 1 AS isFile, handle, name FROM Files UNION "
      "SELECT 0, handle, name FROM Directories ) "
      "USING (handle) "
      ";"_ns);
}

nsresult FixEntryIds(const ResultConnection& aConnection,
                     const EntryId& aRootEntry) {
  const nsLiteralCString calculateHashesQuery =
      "CREATE TEMPORARY TABLE EntryMigrationTable AS "
      "WITH RECURSIVE "
      "rehashMap( depth, isFile, handle, parent, name, hash ) AS ( "
      "SELECT 0, isFile, handle, parent, name, hashEntry( :rootEntry, name ) "
      "FROM EntryNames WHERE parent = :rootEntry UNION SELECT "
      "1 + depth, EntryNames.isFile, EntryNames.handle, EntryNames.parent, "
      "EntryNames.name, hashEntry( rehashMap.hash, EntryNames.name ) "
      "FROM rehashMap, EntryNames WHERE rehashMap.handle = EntryNames.parent ) "
      "SELECT depth, isFile, handle, parent, name, hash FROM rehashMap "
      ";"_ns;

  const nsLiteralCString createIndexByDepthQuery =
      "CREATE INDEX indexOnDepth ON EntryMigrationTable ( depth ); "_ns;

  // To avoid constraint violation, new entries are inserted under a temporary
  // parent.

  const nsLiteralCString insertTemporaryParentEntry =
      "INSERT INTO Entries ( handle, parent ) "
      "VALUES ( :tempParent, :rootEntry ) ;"_ns;

  const nsLiteralCString flagTemporaryParentAsDir =
      "INSERT INTO Directories ( handle, name ) "
      "VALUES ( :tempParent, 'temp' ) ;"_ns;

  const nsLiteralCString insertNewEntriesQuery =
      "INSERT INTO Entries ( handle, parent ) "
      "SELECT hash, :tempParent FROM EntryMigrationTable WHERE hash != handle "
      ";"_ns;

  const nsLiteralCString insertNewDirectoriesQuery =
      "INSERT INTO Directories ( handle, name ) "
      "SELECT hash, name FROM EntryMigrationTable "
      "WHERE isFile = 0 AND hash != handle "
      "ORDER BY depth "
      ";"_ns;

  const nsLiteralCString insertNewFilesQuery =
      "INSERT INTO Files ( handle, type, name ) "
      "SELECT EntryMigrationTable.hash, Files.type, EntryMigrationTable.name "
      "FROM EntryMigrationTable INNER JOIN Files USING (handle) "
      "WHERE EntryMigrationTable.isFile = 1 AND hash != handle "
      ";"_ns;

  const nsLiteralCString updateFileMappingsQuery =
      "UPDATE FileIds SET handle = hash "
      "FROM ( SELECT handle, hash FROM EntryMigrationTable WHERE hash != "
      "handle ) "
      "AS replacement WHERE FileIds.handle = replacement.handle "
      ";"_ns;

  const nsLiteralCString updateMainFilesQuery =
      "UPDATE MainFiles SET handle = hash "
      "FROM ( SELECT handle, hash FROM EntryMigrationTable WHERE hash != "
      "handle ) "
      "AS replacement WHERE MainFiles.handle = replacement.handle "
      ";"_ns;

  // Now fix the parents.
  const nsLiteralCString updateEntryMappingsQuery =
      "UPDATE Entries SET parent = hash "
      "FROM ( SELECT Lhs.hash AS handle, Rhs.hash AS hash, Lhs.depth AS depth "
      "FROM EntryMigrationTable AS Lhs "
      "INNER JOIN EntryMigrationTable AS Rhs "
      "ON Rhs.handle = Lhs.parent ORDER BY depth ) AS replacement "
      "WHERE Entries.handle = replacement.handle "
      "AND Entries.parent = :tempParent "
      ";"_ns;

  const nsLiteralCString cleanupOldEntriesQuery =
      "DELETE FROM Entries WHERE handle IN "
      "( SELECT handle FROM EntryMigrationTable WHERE hash != handle ) "
      ";"_ns;

  const nsLiteralCString cleanupTemporaryParent =
      "DELETE FROM Entries WHERE handle = :tempParent ;"_ns;

  const nsLiteralCString dropIndexByDepthQuery =
      "DROP INDEX indexOnDepth ; "_ns;

  // Index is automatically deleted
  const nsLiteralCString cleanupTemporaries =
      "DROP TABLE EntryMigrationTable ;"_ns;

  EntryId tempParent(nsCString(nsID::GenerateUUID().ToString().get()));

  nsCOMPtr<mozIStorageFunction> rehashFunction =
      new data::FileSystemHashStorageFunction();
  QM_TRY(MOZ_TO_RESULT(aConnection->CreateFunction("hashEntry"_ns,
                                                   /* number of arguments */ 2,
                                                   rehashFunction)));
  auto finallyRemoveFunction = MakeScopeExit([&aConnection]() {
    QM_WARNONLY_TRY(MOZ_TO_RESULT(aConnection->RemoveFunction("hashEntry"_ns)));
  });

  // We need this to make sure the old entries get removed
  QM_TRY(MOZ_TO_RESULT(
      aConnection->ExecuteSimpleSQL("PRAGMA foreign_keys = ON;"_ns)));

  QM_TRY_UNWRAP(auto transaction, StartedTransaction::Create(aConnection));

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(aConnection, calculateHashesQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("rootEntry"_ns, aRootEntry)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  QM_TRY(QM_TO_RESULT(aConnection->ExecuteSimpleSQL(createIndexByDepthQuery)));

  {
    QM_TRY_UNWRAP(
        ResultStatement stmt,
        ResultStatement::Create(aConnection, insertTemporaryParentEntry));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("tempParent"_ns, tempParent)));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("rootEntry"_ns, aRootEntry)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(
        ResultStatement stmt,
        ResultStatement::Create(aConnection, flagTemporaryParentAsDir));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("tempParent"_ns, tempParent)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(aConnection, insertNewEntriesQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("tempParent"_ns, tempParent)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(
        ResultStatement stmt,
        ResultStatement::Create(aConnection, insertNewDirectoriesQuery));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(aConnection, insertNewFilesQuery));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(
        ResultStatement stmt,
        ResultStatement::Create(aConnection, updateFileMappingsQuery));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(aConnection, updateMainFilesQuery));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(
        ResultStatement stmt,
        ResultStatement::Create(aConnection, updateEntryMappingsQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("tempParent"_ns, tempParent)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(aConnection, cleanupOldEntriesQuery));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(aConnection, cleanupTemporaryParent));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("tempParent"_ns, tempParent)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  QM_TRY(QM_TO_RESULT(aConnection->ExecuteSimpleSQL(dropIndexByDepthQuery)));

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(aConnection, cleanupTemporaries));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  QM_TRY(QM_TO_RESULT(transaction.Commit()));

  QM_WARNONLY_TRY(QM_TO_RESULT(aConnection->ExecuteSimpleSQL("VACUUM;"_ns)));

  return NS_OK;
}

}  // namespace

Result<DatabaseVersion, QMResult> SchemaVersion002::InitializeConnection(
    ResultConnection& aConn, data::FileSystemFileManager& aFileManager,
    const Origin& aOrigin) {
  QM_TRY_UNWRAP(const bool wasEmpty, CheckIfEmpty(aConn));

  DatabaseVersion currentVersion = 0;

  if (wasEmpty) {
    QM_TRY(QM_TO_RESULT(SetEncoding(aConn)));
  } else {
    QM_TRY(QM_TO_RESULT(aConn->GetSchemaVersion(&currentVersion)));
  }

  if (currentVersion < sVersion) {
    MOZ_ASSERT_IF(0 != currentVersion, 1 == currentVersion);

    QM_TRY_UNWRAP(auto transaction, StartedTransaction::Create(aConn));

    if (0 == currentVersion) {
      QM_TRY(QM_TO_RESULT(SchemaVersion001::CreateTables(aConn, aOrigin)));
    }

    QM_TRY(QM_TO_RESULT(CreateFileIds(aConn)));

    if (!wasEmpty) {
      QM_TRY(QM_TO_RESULT(PopulateFileIds(aConn)));
    }

    QM_TRY(QM_TO_RESULT(ConnectUsagesToFileIds(aConn)));

    QM_TRY(QM_TO_RESULT(CreateMainFiles(aConn)));
    if (!wasEmpty) {
      QM_TRY(QM_TO_RESULT(PopulateMainFiles(aConn)));
    }

    QM_TRY(QM_TO_RESULT(CreateEntryNamesView(aConn)));

    QM_TRY(QM_TO_RESULT(aConn->SetSchemaVersion(sVersion)));

    QM_TRY(QM_TO_RESULT(transaction.Commit()));

    if (!wasEmpty) {
      QM_TRY(QM_TO_RESULT(aConn->ExecuteSimpleSQL("VACUUM;"_ns)));
    }
  }

  // The upgrade from version 1 to version 2 was buggy, so we have to check if
  // the Usages table still references the Files table which is a sign that
  // the upgrade wasn't complete. This extra query has only negligible perf
  // impact. See bug 1847989.
  auto UsagesTableRefsFilesTable = [&aConn]() -> Result<bool, QMResult> {
    const nsLiteralCString query =
        "SELECT pragma_foreign_key_list.'table'=='Files' "
        "FROM pragma_foreign_key_list('Usages');"_ns;

    QM_TRY_UNWRAP(ResultStatement stmt, ResultStatement::Create(aConn, query));

    return stmt.YesOrNoQuery();
  };

  QM_TRY_UNWRAP(auto usagesTableRefsFilesTable, UsagesTableRefsFilesTable());

  if (usagesTableRefsFilesTable) {
    QM_TRY_UNWRAP(auto transaction, StartedTransaction::Create(aConn));

    // The buggy upgrade didn't call PopulateFileIds, ConnectUsagesToFileIds
    // and PopulateMainFiles was completely missing. Since invalid file ids
    // and main files could be inserted when the profile was broken, we need
    // to clear them before populating.
    QM_TRY(ClearInvalidFileIds(aConn, aFileManager));
    QM_TRY(QM_TO_RESULT(PopulateFileIds(aConn)));
    QM_TRY(QM_TO_RESULT(ConnectUsagesToFileIds(aConn)));
    QM_TRY(ClearInvalidMainFiles(aConn, aFileManager));
    QM_TRY(QM_TO_RESULT(PopulateMainFiles(aConn)));

    QM_TRY(QM_TO_RESULT(transaction.Commit()));

    QM_TRY(QM_TO_RESULT(aConn->ExecuteSimpleSQL("VACUUM;"_ns)));

    QM_TRY_UNWRAP(usagesTableRefsFilesTable, UsagesTableRefsFilesTable());
    MOZ_ASSERT(!usagesTableRefsFilesTable);
  }

  // In schema version 001, entryId was unique but not necessarily related to
  // a path. For schema 002, we have to fix all entryIds to be derived from
  // the underlying path.
  auto OneTimeRehashingDone = [&aConn]() -> Result<bool, QMResult> {
    const nsLiteralCString query =
        "SELECT EXISTS (SELECT 1 FROM sqlite_master "
        "WHERE type='table' AND name='RehashedFrom001to002' ) ;"_ns;

    QM_TRY_UNWRAP(ResultStatement stmt, ResultStatement::Create(aConn, query));

    return stmt.YesOrNoQuery();
  };

  QM_TRY_UNWRAP(auto oneTimeRehashingDone, OneTimeRehashingDone());

  if (!oneTimeRehashingDone) {
    const nsLiteralCString findRootEntry =
        "SELECT handle FROM Entries WHERE parent IS NULL ;"_ns;

    EntryId rootId;
    {
      QM_TRY_UNWRAP(ResultStatement stmt,
                    ResultStatement::Create(aConn, findRootEntry));

      QM_TRY_UNWRAP(DebugOnly<bool> moreResults, stmt.ExecuteStep());
      MOZ_ASSERT(moreResults);

      QM_TRY_UNWRAP(rootId, stmt.GetEntryIdByColumn(/* Column */ 0u));
    }

    MOZ_ASSERT(!rootId.IsEmpty());

    QM_TRY(QM_TO_RESULT(FixEntryIds(aConn, rootId)));

    QM_TRY(QM_TO_RESULT(aConn->ExecuteSimpleSQL(
        "CREATE TABLE RehashedFrom001to002 (id INTEGER PRIMARY KEY);"_ns)));

    QM_TRY_UNWRAP(DebugOnly<bool> isDoneNow, OneTimeRehashingDone());
    MOZ_ASSERT(isDoneNow);
  }

  QM_TRY(QM_TO_RESULT(aConn->ExecuteSimpleSQL("PRAGMA foreign_keys = ON;"_ns)));

  QM_TRY(QM_TO_RESULT(aConn->GetSchemaVersion(&currentVersion)));

  return currentVersion;
}

}  // namespace mozilla::dom::fs