Created
May 23, 2014 22:26
-
-
Save bzitzow/9056b9147698f171eb2d to your computer and use it in GitHub Desktop.
Phonegap SQLite Database Schema Migrations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//phoneGap HTML5 SQLite database schema migration example | |
var db = null; | |
var current_migration = null; | |
var current_schema_version = null; | |
// keep migration version order | |
var db_migrations = { | |
v1: { | |
version: "1.0", | |
up: function(tx) { | |
tx.executeSql('CREATE TABLE IF NOT EXISTS food (id INTEGER AUTOINCREMENT PRIMARY KEY, food_list_type SMALLINT, name VARCHAR(255));'); | |
}, | |
down: function(tx) { | |
tx.executeSql('DROP TABLE IF EXISTS food;'); | |
}, | |
}, | |
v2: { | |
version: "2.0", | |
up: function(tx) { | |
tx.executeSql('CREATE TABLE IF NOT EXISTS food_notes (food_id INTEGER NOT NULL, note TEXT, picture VARCHAR(255));'); | |
tx.executeSql('CREATE INDEX IF NOT EXISTS food_notes_food_id_idx ON food_notes(food_id);'); | |
tx.executeSql('CREATE TABLE IF NOT EXISTS food_in_food (food_id INTEGER NOT NULL, paremt_id INTEGER NOT NULL);'); | |
tx.executeSql('CREATE INDEX IF NOT EXISTS food_in_food_id_idx ON food_in_food(food_id);'); | |
tx.executeSql('CREATE UNIQUE INDEX IF NOT EXISTS food_in_food_uniq_idx ON food_in_food(paremt_id,food_id);'); | |
}, | |
down: function(tx) { | |
tx.executeSql('DROP INDEX IF EXISTS food_notes_food_id_idx;'); | |
tx.executeSql('DROP TABLE IF EXISTS food_notes;'); | |
tx.executeSql('DROP INDEX IF EXISTS food_in_food_id_idx;'); | |
tx.executeSql('DROP INDEX IF EXISTS food_in_food_uniq_idx;'); | |
tx.executeSql('DROP TABLE IF EXISTS food_in_food;'); | |
}, | |
}, | |
}; | |
function createDB(name,displayName, size) { | |
db = window.openDatabase(name, "", displayName, size); | |
for(m in db_migrations) { | |
current_migration = db_migrations[m]; | |
if (db.version < current_migration.version) { | |
db.transaction(current_migration.up, createDBError, createDBSuccess); | |
} | |
} | |
} | |
function createDBSuccess() { | |
current_schema_version = current_migration.version; | |
db.changeVersion(db.version,current_schema_version); | |
} | |
function createDBError(err) { | |
navigator.notification.alert('Error updating '+db.version+' database: '+err.message); | |
db.transaction(current_migration.down,createDBErrorFatal,createDBSuccessAll); | |
} | |
function createDBErrorFatal(err) { | |
navigator.notification.alert('Fatal Error when rollback '+current_migration.version+' database: '+err.message); | |
} | |
function createDBSuccessAll() { | |
navigator.notification.alert('Database rollback from '+current_migration.version+' version'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the hint!
I think there is a problem with your code, though:
in
createDB()
:You iterate over object properties (over db_migrations properties) and the iteration order is random.
Hence
current_migration
might bev2
for the first iteration and thenv1
for the second iteration and so on ...Possible fix: you can use arrays, instead.