summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/sync/test_bookmark_mirror_migration.js
blob: 86cf45eb0fa5a3a8454b90248fac3f4a4bdfabb8 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Keep in sync with `SyncedBookmarksMirror.jsm`.
const CURRENT_MIRROR_SCHEMA_VERSION = 9;

// The oldest schema version that we support. Any databases with schemas older
// than this will be dropped and recreated.
const OLDEST_SUPPORTED_MIRROR_SCHEMA_VERSION = 5;

async function getIndexNames(db, table, schema = "mirror") {
  let rows = await db.execute(`PRAGMA ${schema}.index_list(${table})`);
  let names = [];
  for (let row of rows) {
    // Column 4 is `c` if the index was created via `CREATE INDEX`, `u` if
    // via `UNIQUE`, and `pk` if via `PRIMARY KEY`.
    let wasCreated = row.getResultByIndex(3) == "c";
    if (wasCreated) {
      // Column 2 is the name of the index.
      names.push(row.getResultByIndex(1));
    }
  }
  return names.sort();
}

add_task(async function test_migrate_after_downgrade() {
  await PlacesTestUtils.markBookmarksAsSynced();

  let dbFile = await setupFixtureFile("mirror_v5.sqlite");
  let oldBuf = await SyncedBookmarksMirror.open({
    path: dbFile.path,
    recordStepTelemetry() {},
    recordValidationTelemetry() {},
  });

  info("Downgrade schema version to oldest supported");
  await oldBuf.db.setSchemaVersion(
    OLDEST_SUPPORTED_MIRROR_SCHEMA_VERSION,
    "mirror"
  );
  await oldBuf.finalize();

  let buf = await SyncedBookmarksMirror.open({
    path: dbFile.path,
    recordStepTelemetry() {},
    recordValidationTelemetry() {},
  });

  // All migrations between `OLDEST_SUPPORTED_MIRROR_SCHEMA_VERSION` should
  // be idempotent. When we downgrade, we roll back the schema version, but
  // leave the schema changes in place, since we can't anticipate what a
  // future version will change.
  let schemaVersion = await buf.db.getSchemaVersion("mirror");
  equal(
    schemaVersion,
    CURRENT_MIRROR_SCHEMA_VERSION,
    "Should upgrade downgraded mirror schema"
  );

  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});

// Migrations between 5 and 7 add three indexes.
add_task(async function test_migrate_from_5_to_current() {
  await PlacesTestUtils.markBookmarksAsSynced();

  let dbFile = await setupFixtureFile("mirror_v5.sqlite");
  let buf = await SyncedBookmarksMirror.open({
    path: dbFile.path,
    recordStepTelemetry() {},
    recordValidationTelemetry() {},
  });

  let schemaVersion = await buf.db.getSchemaVersion("mirror");
  equal(
    schemaVersion,
    CURRENT_MIRROR_SCHEMA_VERSION,
    "Should upgrade mirror schema to current version"
  );

  let itemsIndexNames = await getIndexNames(buf.db, "items");
  deepEqual(
    itemsIndexNames,
    ["itemKeywords", "itemURLs"],
    "Should add two indexes on items"
  );

  let structureIndexNames = await getIndexNames(buf.db, "structure");
  deepEqual(
    structureIndexNames,
    ["structurePositions"],
    "Should add an index on structure"
  );

  let changesToUpload = await buf.apply();
  deepEqual(changesToUpload, {}, "Shouldn't flag any items for reupload");

  await assertLocalTree(
    PlacesUtils.bookmarks.menuGuid,
    {
      guid: PlacesUtils.bookmarks.menuGuid,
      type: PlacesUtils.bookmarks.TYPE_FOLDER,
      index: 0,
      title: BookmarksMenuTitle,
      children: [
        {
          guid: "bookmarkAAAA",
          type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
          index: 0,
          title: "A",
          url: "http://example.com/a",
        },
        {
          guid: "bookmarkBBBB",
          type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
          index: 1,
          title: "B",
          url: "http://example.com/b",
          keyword: "hi",
        },
      ],
    },
    "Should apply mirror tree after migrating"
  );

  let keywordEntry = await PlacesUtils.keywords.fetch("hi");
  equal(
    keywordEntry.url.href,
    "http://example.com/b",
    "Should apply keyword from migrated mirror"
  );

  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});

// Migrations between 1 and 2 discard the entire database.
add_task(async function test_migrate_from_1_to_2() {
  let dbFile = await setupFixtureFile("mirror_v1.sqlite");
  let buf = await SyncedBookmarksMirror.open({
    path: dbFile.path,
  });
  ok(
    buf.wasCorrupt,
    "Migrating from unsupported version should mark database as corrupt"
  );
  await buf.finalize();
});

add_task(async function test_database_corrupt() {
  let corruptFile = await setupFixtureFile("mirror_corrupt.sqlite");
  let buf = await SyncedBookmarksMirror.open({
    path: corruptFile.path,
  });
  ok(buf.wasCorrupt, "Opening corrupt database should mark it as such");
  await buf.finalize();
});

add_task(async function test_migrate_v7_v9() {
  let buf = await openMirror("test_migrate_v7_v9");

  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.menuGuid,
    children: [
      {
        guid: "bookmarkAAAA",
        url: "http://example.com/a",
        title: "A",
      },
      {
        guid: "bookmarkBBBB",
        url: "http://example.com/b",
        title: "B",
      },
    ],
  });

  await buf.db.execute(
    `UPDATE moz_bookmarks
     SET syncChangeCounter = 0,
         syncStatus = ${PlacesUtils.bookmarks.SYNC_STATUS.NEW}`
  );

  // setup the mirror.
  await storeRecords(buf, [
    {
      id: "bookmarkAAAA",
      parentid: "menu",
      type: "bookmark",
      title: "B",
      bmkUri: "http://example.com/b",
    },
    {
      id: "menu",
      parentid: "places",
      type: "folder",
      children: [],
    },
  ]);

  await buf.db.setSchemaVersion(7, "mirror");
  await buf.finalize();

  // reopen it.
  buf = await openMirror("test_migrate_v7_v9");
  Assert.equal(await buf.db.getSchemaVersion("mirror"), 9, "did upgrade");

  let fields = await PlacesTestUtils.fetchBookmarkSyncFields(
    "bookmarkAAAA",
    "bookmarkBBBB",
    PlacesUtils.bookmarks.menuGuid
  );
  let [fieldsA, fieldsB, fieldsMenu] = fields;

  // 'A' was in the mirror - should now be _NORMAL
  Assert.equal(fieldsA.guid, "bookmarkAAAA");
  Assert.equal(fieldsA.syncStatus, PlacesUtils.bookmarks.SYNC_STATUS.NORMAL);
  // 'B' was not in the mirror so should be untouched.
  Assert.equal(fieldsB.guid, "bookmarkBBBB");
  Assert.equal(fieldsB.syncStatus, PlacesUtils.bookmarks.SYNC_STATUS.NEW);
  // 'menu' was in the mirror - should now be _NORMAL
  Assert.equal(fieldsMenu.guid, PlacesUtils.bookmarks.menuGuid);
  Assert.equal(fieldsMenu.syncStatus, PlacesUtils.bookmarks.SYNC_STATUS.NORMAL);
  await buf.finalize();
});

add_task(async function test_migrate_v8_v9() {
  let dbFile = await setupFixtureFile("mirror_v8.sqlite");
  let buf = await SyncedBookmarksMirror.open({
    path: dbFile.path,
    recordStepTelemetry() {},
    recordValidationTelemetry() {},
  });

  Assert.equal(await buf.db.getSchemaVersion("mirror"), 9, "did upgrade");

  // Verify the new column is there
  Assert.ok(await buf.db.execute("SELECT unknownFields FROM items"));

  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});