2006-06-07 01:02:59 +00:00
|
|
|
Scholar.Schema = new function(){
|
2006-06-07 15:27:21 +00:00
|
|
|
var _dbVersions = [];
|
|
|
|
var _schemaVersions = [];
|
2006-06-07 01:02:59 +00:00
|
|
|
|
|
|
|
this.updateSchema = updateSchema;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks if the DB schema exists and is up-to-date, updating if necessary
|
|
|
|
*/
|
|
|
|
function updateSchema(){
|
|
|
|
var dbVersion = _getDBVersion();
|
|
|
|
var schemaVersion = _getSchemaSQLVersion();
|
|
|
|
|
2006-06-07 15:27:21 +00:00
|
|
|
if (dbVersion == schemaVersion){
|
|
|
|
if (SCHOLAR_CONFIG['DB_REBUILD']){
|
|
|
|
if (confirm('Erase all data and recreate database from schema?')){
|
|
|
|
_initializeSchema();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateScrapers();
|
|
|
|
return;
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
2006-06-07 15:27:21 +00:00
|
|
|
// If DB version is less than schema file, create or update
|
2006-06-07 01:02:59 +00:00
|
|
|
else if (dbVersion < schemaVersion){
|
|
|
|
if (!dbVersion){
|
|
|
|
Scholar.debug('Database does not exist -- creating\n');
|
2006-06-07 15:27:21 +00:00
|
|
|
_initializeSchema();
|
|
|
|
return;
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
|
|
|
|
2006-06-07 15:27:21 +00:00
|
|
|
_migrateSchema(dbVersion);
|
|
|
|
_updateScrapers();
|
|
|
|
return;
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
2006-06-07 15:27:21 +00:00
|
|
|
else {
|
|
|
|
throw("Scholar DB version is newer than schema version");
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Private methods
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the DB schema version
|
|
|
|
*/
|
2006-06-07 15:27:21 +00:00
|
|
|
function _getDBVersion(schema){
|
|
|
|
// Default to schema.sql
|
|
|
|
if (!schema){
|
|
|
|
schema = 'schema';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dbVersions[schema]){
|
|
|
|
return _dbVersions[schema];
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Scholar.DB.tableExists('version')){
|
2006-06-07 15:27:21 +00:00
|
|
|
try {
|
|
|
|
var dbVersion = Scholar.DB.valueQuery("SELECT version FROM "
|
2006-06-08 00:16:35 +00:00
|
|
|
+ "version WHERE schema='" + schema + "'");
|
2006-06-07 15:27:21 +00:00
|
|
|
}
|
|
|
|
// DEBUG: this is temporary to handle version table schema change
|
|
|
|
catch(e){
|
|
|
|
if (e=='no such column: schema'){
|
|
|
|
Scholar.debug(e, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If some other problem, bail
|
|
|
|
throw(e);
|
|
|
|
}
|
|
|
|
_dbVersions[schema] = dbVersion;
|
2006-06-07 01:02:59 +00:00
|
|
|
return dbVersion;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-06-07 15:27:21 +00:00
|
|
|
* Retrieve the version from the top line of the schema SQL file
|
2006-06-07 01:02:59 +00:00
|
|
|
*/
|
2006-06-07 15:27:21 +00:00
|
|
|
function _getSchemaSQLVersion(schema){
|
|
|
|
// Default to schema.sql
|
|
|
|
if (!schema){
|
|
|
|
schema = 'schema';
|
|
|
|
}
|
|
|
|
|
|
|
|
var schemaFile = schema + '.sql';
|
|
|
|
|
|
|
|
if (_schemaVersions[schema]){
|
|
|
|
return _schemaVersions[schema];
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var file = Components.classes["@mozilla.org/extensions/manager;1"]
|
|
|
|
.getService(Components.interfaces.nsIExtensionManager)
|
|
|
|
.getInstallLocation(SCHOLAR_CONFIG['GUID'])
|
|
|
|
.getItemLocation(SCHOLAR_CONFIG['GUID']);
|
2006-06-07 15:27:21 +00:00
|
|
|
file.append(schemaFile);
|
2006-06-07 01:02:59 +00:00
|
|
|
|
|
|
|
// Open an input stream from file
|
|
|
|
var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]
|
|
|
|
.createInstance(Components.interfaces.nsIFileInputStream);
|
|
|
|
istream.init(file, 0x01, 0444, 0);
|
|
|
|
istream.QueryInterface(Components.interfaces.nsILineInputStream);
|
|
|
|
|
|
|
|
var line = {};
|
|
|
|
|
|
|
|
// Fetch the schema version from the first line of the file
|
|
|
|
istream.readLine(line);
|
|
|
|
var schemaVersion = line.value.match(/-- ([0-9]+)/)[1];
|
|
|
|
istream.close();
|
|
|
|
|
2006-06-07 15:27:21 +00:00
|
|
|
_schemaVersions[schema] = schemaVersion;
|
2006-06-07 01:02:59 +00:00
|
|
|
return schemaVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load in SQL schema
|
|
|
|
*
|
|
|
|
* Returns an _array_ of SQL statements for feeding into query()
|
|
|
|
*/
|
2006-06-07 15:27:21 +00:00
|
|
|
function _getSchemaSQL(schema){
|
|
|
|
// Default to schema.sql
|
|
|
|
if (!schema){
|
|
|
|
schema = 'schema';
|
|
|
|
}
|
|
|
|
|
|
|
|
var schemaFile = schema + '.sql';
|
|
|
|
|
2006-06-07 01:02:59 +00:00
|
|
|
// We pull the schema from an external file so we only have to process
|
|
|
|
// it when necessary
|
|
|
|
var file = Components.classes["@mozilla.org/extensions/manager;1"]
|
|
|
|
.getService(Components.interfaces.nsIExtensionManager)
|
|
|
|
.getInstallLocation(SCHOLAR_CONFIG['GUID'])
|
|
|
|
.getItemLocation(SCHOLAR_CONFIG['GUID']);
|
2006-06-07 15:27:21 +00:00
|
|
|
file.append(schemaFile);
|
2006-06-07 01:02:59 +00:00
|
|
|
|
|
|
|
// Open an input stream from file
|
|
|
|
var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]
|
|
|
|
.createInstance(Components.interfaces.nsIFileInputStream);
|
|
|
|
istream.init(file, 0x01, 0444, 0);
|
|
|
|
istream.QueryInterface(Components.interfaces.nsILineInputStream);
|
|
|
|
|
|
|
|
var line = {}, sql = '', hasmore;
|
|
|
|
|
|
|
|
// Skip the first line, which contains the schema version
|
|
|
|
istream.readLine(line);
|
|
|
|
//var schemaVersion = line.value.match(/-- ([0-9]+)/)[1];
|
|
|
|
|
|
|
|
do {
|
|
|
|
hasmore = istream.readLine(line);
|
|
|
|
sql += line.value + "\n";
|
|
|
|
} while(hasmore);
|
|
|
|
|
|
|
|
istream.close();
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create new DB schema
|
|
|
|
*/
|
|
|
|
function _initializeSchema(){
|
|
|
|
try {
|
2006-06-07 14:35:04 +00:00
|
|
|
Scholar.DB.beginTransaction();
|
2006-06-07 15:27:21 +00:00
|
|
|
Scholar.DB.query(_getSchemaSQL());
|
|
|
|
Scholar.DB.query("INSERT INTO version VALUES ('schema', "
|
|
|
|
+ _getSchemaSQLVersion() + ")");
|
|
|
|
Scholar.DB.query(_getSchemaSQL('scrapers'));
|
|
|
|
Scholar.DB.query("INSERT INTO version VALUES ('scrapers', "
|
|
|
|
+ _getSchemaSQLVersion('scrapers') + ")");
|
2006-06-07 14:35:04 +00:00
|
|
|
Scholar.DB.commitTransaction();
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
alert(e);
|
2006-06-07 14:35:04 +00:00
|
|
|
Scholar.DB.rollbackTransaction();
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-07 15:27:21 +00:00
|
|
|
/*
|
|
|
|
* Update a DB schema version tag in an existing database
|
|
|
|
*/
|
|
|
|
function _updateDBVersion(schema, version){
|
|
|
|
return Scholar.DB.query("UPDATE version SET version=" + version
|
|
|
|
+ " WHERE schema='" + schema + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the scrapers in the DB to the latest bundled versions
|
|
|
|
*/
|
|
|
|
function _updateScrapers(){
|
|
|
|
var dbVersion = _getDBVersion('scrapers');
|
|
|
|
var schemaVersion = _getSchemaSQLVersion('scrapers');
|
|
|
|
|
|
|
|
if (dbVersion == schemaVersion){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (dbVersion < schemaVersion){
|
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
Scholar.DB.query(_getSchemaSQL('scrapers'));
|
|
|
|
_updateDBVersion('scrapers', schemaVersion);
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw("Scraper set in DB is newer than schema version");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-07 01:02:59 +00:00
|
|
|
/*
|
|
|
|
* Migrate schema from an older version, preserving data
|
|
|
|
*/
|
|
|
|
function _migrateSchema(fromVersion){
|
|
|
|
//
|
|
|
|
// Change this value to match the schema version
|
|
|
|
//
|
2006-06-12 15:43:24 +00:00
|
|
|
var toVersion = 18;
|
2006-06-07 01:02:59 +00:00
|
|
|
|
|
|
|
if (toVersion != _getSchemaSQLVersion()){
|
|
|
|
throw('Schema version does not match version in _migrateSchema()');
|
|
|
|
}
|
|
|
|
|
|
|
|
Scholar.debug('Updating DB from version ' + fromVersion + ' to ' + toVersion + '\n');
|
|
|
|
|
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
|
|
|
// Step through version changes until we reach the current version
|
|
|
|
//
|
|
|
|
// Each block performs the changes necessary to move from the
|
|
|
|
// previous revision to that one.
|
|
|
|
for (var i=parseInt(fromVersion) + 1; i<=toVersion; i++){
|
2006-06-12 15:43:24 +00:00
|
|
|
if (i==18){
|
2006-06-08 00:16:35 +00:00
|
|
|
_initializeSchema();
|
2006-06-07 01:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-07 15:27:21 +00:00
|
|
|
_updateDBVersion('schema', i-1);
|
2006-06-07 01:02:59 +00:00
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
}
|
|
|
|
}
|