Fix DB initialization with async DB access

Also fix the initial repository check to trigger after initialization is
complete instead of waiting
This commit is contained in:
Dan Stillman 2013-08-15 17:35:16 -04:00
parent d543936307
commit 28ca0a3599
4 changed files with 260 additions and 217 deletions

View file

@ -53,7 +53,8 @@ Zotero_Preferences.General = {
updateTranslators: function () { updateTranslators: function () {
Zotero.Schema.updateFromRepository(true, function (xmlhttp, updated) { Zotero.Schema.updateFromRepository(true)
.then(function (updated) {
var button = document.getElementById('updateButton'); var button = document.getElementById('updateButton');
if (button) { if (button) {
if (updated===-1) { if (updated===-1) {
@ -71,6 +72,7 @@ Zotero_Preferences.General = {
Zotero_Preferences.Cite.refreshStylesList(); Zotero_Preferences.Cite.refreshStylesList();
} }
} }
}); })
.done();
} }
} }

View file

@ -1005,6 +1005,51 @@ Zotero.DBConnection.prototype.columnQueryAsync = function (sql, params) {
}; };
Zotero.DBConnection.prototype.tableExistsAsync = function (table) {
return this._getConnectionAsync()
.then(function () {
var sql = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name=?";
return Zotero.DB.valueQueryAsync(sql, [table]);
})
.then(function (count) {
return !!count;
});
}
/**
* Parse SQL string and execute transaction with all statements
*
* @return {Promise}
*/
Zotero.DBConnection.prototype.executeSQLFile = function (sql) {
var nonCommentRE = /^[^-]/;
var trailingCommentRE = /^(.*?)(?:--.+)?$/;
sql = sql.trim()
// Ugly hack to parse triggers with embedded semicolons
.replace(/;---/g, "TEMPSEMI")
.split("\n")
.filter(function (x) nonCommentRE.test(x))
.map(function (x) x.match(trailingCommentRE)[1])
.join("");
if (sql.substr(-1) == ";") {
sql = sql.substr(0, sql.length - 1);
}
var statements = sql.split(";")
.map(function (x) x.replace(/TEMPSEMI/g, ";"));
return this.executeTransaction(function () {
var statement;
while (statement = statements.shift()) {
yield Zotero.DB.queryAsync(statement);
}
});
}
/** /**
* Generator functions can't return values, but Task.js-style generators, * Generator functions can't return values, but Task.js-style generators,
* as used by executeTransaction(), can throw a special exception in order * as used by executeTransaction(), can throw a special exception in order

View file

@ -50,8 +50,10 @@ Zotero.Schema = new function(){
var sql = "SELECT version FROM version WHERE schema='" + schema + "'"; var sql = "SELECT version FROM version WHERE schema='" + schema + "'";
return Zotero.DB.valueQueryAsync(sql) return Zotero.DB.valueQueryAsync(sql)
.then(function (dbVersion) { .then(function (dbVersion) {
dbVersion = parseInt(dbVersion); if (dbVersion) {
_dbVersions[schema] = dbVersion; dbVersion = parseInt(dbVersion);
_dbVersions[schema] = dbVersion;
}
return dbVersion; return dbVersion;
}); });
} }
@ -409,23 +411,22 @@ Zotero.Schema = new function(){
}) })
.then(function (updated) { .then(function (updated) {
if (runRemoteUpdateWhenComplete) { if (runRemoteUpdateWhenComplete) {
var deferred = Q.defer();
if (updated) { if (updated) {
if (Zotero.Prefs.get('automaticScraperUpdates')) { if (Zotero.Prefs.get('automaticScraperUpdates')) {
Zotero.proxyAuthComplete Zotero.unlockPromise
.then(function () { .then(Zotero.proxyAuthComplete)
Zotero.Schema.updateFromRepository(2, function () deferred.resolve()); .delay(1000)
}) .then(function () Zotero.Schema.updateFromRepository(2))
.done();
} }
} }
else { else {
Zotero.proxyAuthComplete Zotero.unlockPromise
.then(function () { .then(Zotero.proxyAuthComplete)
Zotero.Schema.updateFromRepository(false, function () deferred.resolve()); .delay(1000)
}) .then(function () Zotero.Schema.updateFromRepository(false))
.done(); .done();
} }
return deferred.promise;
} }
}); });
} }
@ -896,10 +897,9 @@ Zotero.Schema = new function(){
* *
* @param {Boolean} force Force a repository query regardless of how * @param {Boolean} force Force a repository query regardless of how
* long it's been since the last check * long it's been since the last check
* @param {Function} callback
*/ */
this.updateFromRepository = function (force, callback) { this.updateFromRepository = function (force) {
Q.fcall(function () { return Q.fcall(function () {
if (force) return true; if (force) return true;
if (_remoteUpdateInProgress) { if (_remoteUpdateInProgress) {
@ -992,23 +992,21 @@ Zotero.Schema = new function(){
} }
var body = 'styles=' + encodeURIComponent(JSON.stringify(styleTimestamps)); var body = 'styles=' + encodeURIComponent(JSON.stringify(styleTimestamps));
var get = Zotero.HTTP.doPost(url, body, function (xmlhttp) { Zotero.HTTP.promise("POST", url, { body: body })
_updateFromRepositoryCallback(xmlhttp, !!force) .then(function (xmlhttp) {
.then(function (updated) { return _updateFromRepositoryCallback(xmlhttp, !!force);
if (callback) { })
callback(xmlhttp, updated); .catch(function (e) {
} if (e instanceof Zotero.HTTP.BrowserOfflineException) {
}); Zotero.debug('Browser is offline -- skipping check');
// TODO: instead, add an observer to start and stop timer on online state change
_setRepositoryTimer(ZOTERO_CONFIG.REPOSITORY_RETRY_INTERVAL);
return;
}
throw e;
}); });
// TODO: instead, add an observer to start and stop timer on online state change
if (!get) {
Zotero.debug('Browser is offline -- skipping check');
_setRepositoryTimer(ZOTERO_CONFIG.REPOSITORY_RETRY_INTERVAL);
}
}); });
}) });
.done();
} }
@ -1382,13 +1380,13 @@ Zotero.Schema = new function(){
yield Zotero.DB.queryAsync("PRAGMA auto_vacuum = 1"); yield Zotero.DB.queryAsync("PRAGMA auto_vacuum = 1");
yield _getSchemaSQL('system').then(function (sql) { yield _getSchemaSQL('system').then(function (sql) {
return Zotero.DB.queryAsync(sql); return Zotero.DB.executeSQLFile(sql);
}); });
yield _getSchemaSQL('userdata').then(function (sql) { yield _getSchemaSQL('userdata').then(function (sql) {
return Zotero.DB.queryAsync(sql); return Zotero.DB.executeSQLFile(sql);
}); });
yield _getSchemaSQL('triggers').then(function (sql) { yield _getSchemaSQL('triggers').then(function (sql) {
return Zotero.DB.queryAsync(sql); return Zotero.DB.executeSQLFile(sql);
}); });
yield Zotero.Schema.updateCustomTables(true); yield Zotero.Schema.updateCustomTables(true);
@ -1526,10 +1524,8 @@ Zotero.Schema = new function(){
// Store the timestamp provided by the server // Store the timestamp provided by the server
yield _updateDBVersion('repository', currentTime); yield _updateDBVersion('repository', currentTime);
if (!manual) { // And the local timestamp of the update time
// And the local timestamp of the update time yield _updateDBVersion('lastcheck', lastCheckTime);
yield _updateDBVersion('lastcheck', lastCheckTime);
}
}) })
.then(function () { .then(function () {
Zotero.debug('All translators and styles are up-to-date'); Zotero.debug('All translators and styles are up-to-date');
@ -1567,10 +1563,8 @@ Zotero.Schema = new function(){
// Store the timestamp provided by the server // Store the timestamp provided by the server
yield _updateDBVersion('repository', currentTime); yield _updateDBVersion('repository', currentTime);
if (!manual) { // And the local timestamp of the update time
// And the local timestamp of the update time yield _updateDBVersion('lastcheck', lastCheckTime);
yield _updateDBVersion('lastcheck', lastCheckTime);
}
}) })
.then(function () { .then(function () {
if (!manual) { if (!manual) {

File diff suppressed because it is too large Load diff