summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contentprefs/tests/unit_cps2/test_migrationToSchema4.js
blob: 85d23e355817ce691767e204997c05a571ed7711 (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
/* 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/. */

// Dump of version we migrate from
var schema_version3 = `
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
  CREATE TABLE groups (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
  INSERT INTO "groups" VALUES(1,'foo.com');
  INSERT INTO "groups" VALUES(2,'bar.com');

  CREATE TABLE settings (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
  INSERT INTO "settings" VALUES(1,'zoom-setting');
  INSERT INTO "settings" VALUES(2,'dir-setting');

  CREATE TABLE prefs (id INTEGER PRIMARY KEY, groupID INTEGER REFERENCES groups(id), settingID INTEGER NOT NULL REFERENCES settings(id), value BLOB);
  INSERT INTO "prefs" VALUES(1,1,1,0.5);
  INSERT INTO "prefs" VALUES(2,1,2,'/download/dir');
  INSERT INTO "prefs" VALUES(3,2,1,0.3);
  INSERT INTO "prefs" VALUES(4,NULL,1,0.1);

  CREATE INDEX groups_idx ON groups(name);
  CREATE INDEX settings_idx ON settings(name);
  CREATE INDEX prefs_idx ON prefs(groupID, settingID);
COMMIT;`;

function prepareVersion3Schema(callback) {
  var dirService = Cc["@mozilla.org/file/directory_service;1"].
                       getService(Ci.nsIProperties);

  var dbFile = dirService.get("ProfD", Ci.nsIFile);
  dbFile.append("content-prefs.sqlite");

  var dbService = Cc["@mozilla.org/storage/service;1"].
                  getService(Ci.mozIStorageService);
  ok(!dbFile.exists(), "Db should not exist yet.");

  var dbConnection = dbService.openDatabase(dbFile);
  equal(dbConnection.schemaVersion, 0);

  dbConnection.executeSimpleSQL(schema_version3);
  dbConnection.schemaVersion = 3;

  dbConnection.close();
}

function run_test() {
  prepareVersion3Schema();
  runAsyncTests(tests, true);
}


// WARNING: Database will reset after every test. This limitation comes from
// the fact that we ContentPrefService constructor is run only once per test file
// and so migration will be run only once.
var tests = [
  function* testMigration() {
    // Test migrated db content.
    schemaVersionIs(4);
    let dbExpectedState = [
      [null, "zoom-setting", 0.1],
      ["bar.com", "zoom-setting", 0.3],
      ["foo.com", "zoom-setting", 0.5],
      ["foo.com", "dir-setting", "/download/dir"],
    ];
    yield dbOK(dbExpectedState);

    // Migrated fields should have timestamp set to 0.
    yield cps.removeAllDomainsSince(1000, null, makeCallback());
    yield dbOK(dbExpectedState);

    yield cps.removeAllDomainsSince(0, null, makeCallback());
    yield dbOK([[null, "zoom-setting", 0.1]]);

    // Test that dates are present after migration (column is added).
    const timestamp = 1234;
    yield setWithDate("a.com", "pref-name", "val", timestamp);
    let actualTimestamp = yield getDate("a.com", "pref-name");
    equal(actualTimestamp, timestamp);
  }
];