Bug 975841 - Bootstrap schema upgrade path in HomeProvide.jsm

This commit is contained in:
Lucas Rocha 2014-03-04 16:52:13 +00:00
parent 10d98500b6
commit d7fa164d02
2 changed files with 43 additions and 10 deletions

View File

@ -25,7 +25,7 @@ public class HomeProvider extends SQLiteBridgeContentProvider {
private static final String LOGTAG = "GeckoHomeProvider"; private static final String LOGTAG = "GeckoHomeProvider";
// This should be kept in sync with the db version in mobile/android/modules/HomeProvider.jsm // This should be kept in sync with the db version in mobile/android/modules/HomeProvider.jsm
private static int DB_VERSION = 1; private static int DB_VERSION = 2;
private static String DB_FILENAME = "home.sqlite"; private static String DB_FILENAME = "home.sqlite";
private static final String TABLE_ITEMS = "items"; private static final String TABLE_ITEMS = "items";

View File

@ -17,12 +17,11 @@ Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/XPCOMUtils.jsm");
/* /*
* XXX: Add migration logic to getDatabaseConnection if you ever rev SCHEMA_VERSION.
*
* SCHEMA_VERSION history: * SCHEMA_VERSION history:
* 1: Create HomeProvider (bug 942288) * 1: Create HomeProvider (bug 942288)
* 2: Add filter column to items table (bug 942295/975841)
*/ */
const SCHEMA_VERSION = 1; const SCHEMA_VERSION = 2;
XPCOMUtils.defineLazyGetter(this, "DB_PATH", function() { XPCOMUtils.defineLazyGetter(this, "DB_PATH", function() {
return OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite"); return OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite");
@ -55,6 +54,9 @@ const SQL = {
"created INTEGER" + "created INTEGER" +
")", ")",
dropItemsTable:
"DROP TABLE items",
insertItem: insertItem:
"INSERT INTO items (dataset_id, url, title, description, image_url, filter, created) " + "INSERT INTO items (dataset_id, url, title, description, image_url, filter, created) " +
"VALUES (:dataset_id, :url, :title, :description, :image_url, :filter, :created)", "VALUES (:dataset_id, :url, :title, :description, :image_url, :filter, :created)",
@ -181,6 +183,33 @@ this.HomeProvider = Object.freeze({
var gDatabaseEnsured = false; var gDatabaseEnsured = false;
/**
* Creates the database schema.
*/
function createDatabase(db) {
return Task.spawn(function create_database_task() {
yield db.execute(SQL.createItemsTable);
});
}
/**
* Migrates the database schema to a new version.
*/
function upgradeDatabase(db, oldVersion, newVersion) {
return Task.spawn(function upgrade_database_task() {
for (let v = oldVersion + 1; v <= newVersion; v++) {
switch(v) {
case 2:
// Recreate the items table discarding any
// existing data.
yield db.execute(SQL.dropItemsTable);
yield db.execute(SQL.createItemsTable);
break;
}
}
});
}
/** /**
* Opens a database connection and makes sure that the database schema version * Opens a database connection and makes sure that the database schema version
* is correct, performing migrations if necessary. Consumers should be sure * is correct, performing migrations if necessary. Consumers should be sure
@ -198,13 +227,17 @@ function getDatabaseConnection() {
try { try {
// Check to see if we need to perform any migrations. // Check to see if we need to perform any migrations.
// XXX: We will need to add migration logic if we ever rev SCHEMA_VERSION. let dbVersion = parseInt(yield db.getSchemaVersion());
let dbVersion = yield db.getSchemaVersion();
if (parseInt(dbVersion) < SCHEMA_VERSION) { // getSchemaVersion() returns a 0 int if the schema
// For schema v1, create the items table and set the schema version. // version is undefined.
yield db.execute(SQL.createItemsTable); if (dbVersion === 0) {
yield db.setSchemaVersion(SCHEMA_VERSION); yield createDatabase(db);
} else if (dbVersion < SCHEMA_VERSION) {
yield upgradeDatabase(db, dbVersion, SCHEMA_VERSION);
} }
yield db.setSchemaVersion(SCHEMA_VERSION);
} catch(e) { } catch(e) {
// Close the DB connection before passing the exception to the consumer. // Close the DB connection before passing the exception to the consumer.
yield db.close(); yield db.close();