diff --git a/toolkit/mozapps/extensions/AddonRepository.jsm b/toolkit/mozapps/extensions/AddonRepository.jsm index 54c739ea5c0c..826ce24278b4 100644 --- a/toolkit/mozapps/extensions/AddonRepository.jsm +++ b/toolkit/mozapps/extensions/AddonRepository.jsm @@ -1331,7 +1331,7 @@ var AddonDatabase = { 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) @@ -1339,7 +1339,7 @@ var AddonDatabase = { if (dbfile.exists()) dbfile.remove(false); return this.openConnection(true); - } + }).bind(this); try { this.connection = Services.storage.openUnsharedDatabase(dbfile); diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_migrateAddonRepository.js b/toolkit/mozapps/extensions/test/xpcshell/test_migrateAddonRepository.js index 9132f15c983b..d177ee02f5db 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/test_migrateAddonRepository.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_migrateAddonRepository.js @@ -2,13 +2,15 @@ * 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). @@ -83,11 +85,11 @@ function run_test() { 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); @@ -102,3 +104,37 @@ function run_test() { 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(); + }); +}