Bug 975841 - Bootstrap schema upgrade path in HomeProvide.jsm (r=margaret)

This commit is contained in:
Lucas Rocha 2014-03-04 16:57:43 +00:00
parent 16c687aee6
commit 1e54d4ddf4
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";
// 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 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");
/*
* XXX: Add migration logic to getDatabaseConnection if you ever rev SCHEMA_VERSION.
*
* SCHEMA_VERSION history:
* 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() {
return OS.Path.join(OS.Constants.Path.profileDir, "home.sqlite");
@ -55,6 +54,9 @@ const SQL = {
"created INTEGER" +
")",
dropItemsTable:
"DROP TABLE items",
insertItem:
"INSERT INTO items (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;
/**
* 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
* is correct, performing migrations if necessary. Consumers should be sure
@ -198,13 +227,17 @@ function getDatabaseConnection() {
try {
// 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 = yield db.getSchemaVersion();
if (parseInt(dbVersion) < SCHEMA_VERSION) {
// For schema v1, create the items table and set the schema version.
yield db.execute(SQL.createItemsTable);
yield db.setSchemaVersion(SCHEMA_VERSION);
let dbVersion = parseInt(yield db.getSchemaVersion());
// getSchemaVersion() returns a 0 int if the schema
// version is undefined.
if (dbVersion === 0) {
yield createDatabase(db);
} else if (dbVersion < SCHEMA_VERSION) {
yield upgradeDatabase(db, dbVersion, SCHEMA_VERSION);
}
yield db.setSchemaVersion(SCHEMA_VERSION);
} catch(e) {
// Close the DB connection before passing the exception to the consumer.
yield db.close();