Bug 692078 - Second attempt to open addons.sqlite doesn't work; r=dtownsend a=asa
authorGeoff Lankow <geoff@darktrojan.net>
Thu, 06 Oct 2011 01:08:30 +1300
changeset 79081 87be7145a73e10110a8c93e669a5ed9ae7109232
parent 79080 5f1156bde9ef1652fdc3ca3e729a657e2ccd4756
child 79082 939e5e3e201580bed2a2d8988dc67a403801ad73
push id78
push userclegnitto@mozilla.com
push dateFri, 16 Dec 2011 17:32:24 +0000
treeherdermozilla-release@79d24e644fdd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdtownsend, asa
bugs692078
milestone9.0a2
Bug 692078 - Second attempt to open addons.sqlite doesn't work; r=dtownsend a=asa
toolkit/mozapps/extensions/AddonRepository.jsm
toolkit/mozapps/extensions/test/xpcshell/test_migrateAddonRepository.js
--- a/toolkit/mozapps/extensions/AddonRepository.jsm
+++ b/toolkit/mozapps/extensions/AddonRepository.jsm
@@ -1321,25 +1321,25 @@ var AddonDatabase = {
    */
   openConnection: function AD_openConnection(aSecondAttempt) {
     this.initialized = true;
     delete this.connection;
 
     let dbfile = FileUtils.getFile(KEY_PROFILEDIR, [FILE_DATABASE], true);
     let dbMissing = !dbfile.exists();
 
-    function tryAgain() {
+    var tryAgain = (function() {
       LOG("Deleting database, and attempting openConnection again");
       this.initialized = false;
       if (this.connection.connectionReady)
         this.connection.close();
       if (dbfile.exists())
         dbfile.remove(false);
       return this.openConnection(true);
-    }
+    }).bind(this);
 
     try {
       this.connection = Services.storage.openUnsharedDatabase(dbfile);
     } catch (e) {
       this.initialized = false;
       ERROR("Failed to open database", e);
       if (aSecondAttempt || dbMissing) {
         this.databaseOk = false;
--- a/toolkit/mozapps/extensions/test/xpcshell/test_migrateAddonRepository.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_migrateAddonRepository.js
@@ -1,19 +1,21 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
+const EXPECTED_SCHEMA_VERSION = 2;
+let dbfile;
 
 function run_test() {
   do_test_pending();
   createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
 
   // Write out a minimal database.
-  let dbfile = gProfD.clone();
+  dbfile = gProfD.clone();
   dbfile.append("addons.sqlite");
   let db = AM_Cc["@mozilla.org/storage/service;1"].
            getService(AM_Ci.mozIStorageService).
            openDatabase(dbfile);
 
   db.createTable("addon",
                  "internal_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                  "id TEXT UNIQUE, " +
@@ -78,27 +80,61 @@ function run_test() {
 
   Services.obs.addObserver({
     observe: function () {
       Services.obs.removeObserver(this, "addon-repository-shutdown");
       // Check the DB schema has changed once AddonRepository has freed it.
       db = AM_Cc["@mozilla.org/storage/service;1"].
            getService(AM_Ci.mozIStorageService).
            openDatabase(dbfile);
-      do_check_eq(db.schemaVersion, 2);
+      do_check_eq(db.schemaVersion, EXPECTED_SCHEMA_VERSION);
       do_check_true(db.indexExists("developer_idx"));
       do_check_true(db.indexExists("screenshot_idx"));
       db.close();
-      do_test_finished();
+      run_test_2();
     }
   }, "addon-repository-shutdown", null);
 
   Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", true);
   AddonRepository.getCachedAddonByID("test1@tests.mozilla.org", function (aAddon) {
     do_check_neq(aAddon, null);
     do_check_eq(aAddon.screenshots.length, 1);
     do_check_true(aAddon.screenshots[0].width === null);
     do_check_true(aAddon.screenshots[0].height === null);
     do_check_true(aAddon.screenshots[0].thumbnailWidth === null);
     do_check_true(aAddon.screenshots[0].thumbnailHeight === null);
     AddonRepository.shutdown();
   });
 }
+
+function run_test_2() {
+  // Write out a minimal database.
+  let db = AM_Cc["@mozilla.org/storage/service;1"].
+           getService(AM_Ci.mozIStorageService).
+           openDatabase(dbfile);
+
+  db.createTable("futuristicSchema",
+                 "id INTEGER, " +
+                 "sharks TEXT, " +
+                 "lasers TEXT");
+
+  db.schemaVersion = 1000;
+  db.close();
+
+  Services.obs.addObserver({
+    observe: function () {
+      Services.obs.removeObserver(this, "addon-repository-shutdown");
+      // Check the DB schema has changed once AddonRepository has freed it.
+      db = AM_Cc["@mozilla.org/storage/service;1"].
+           getService(AM_Ci.mozIStorageService).
+           openDatabase(dbfile);
+      do_check_eq(db.schemaVersion, EXPECTED_SCHEMA_VERSION);
+      db.close();
+      do_test_finished();
+    }
+  }, "addon-repository-shutdown", null);
+
+  // Force a connection to the addon database to be opened.
+  Services.prefs.setBoolPref("extensions.getAddons.cache.enabled", true);
+  AddonRepository.getCachedAddonByID("test1@tests.mozilla.org", function (aAddon) {
+    AddonRepository.shutdown();
+  });
+}