Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement