Advertisement
Guest User

phoneGap HTML5 SQLite database schema migration example

a guest
Oct 10th, 2011
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //phoneGap HTML5 SQLite database schema migration example
  2. var db = null;
  3. var current_migration = null;
  4. var current_schema_version = null;
  5.  
  6. // keep migration version order
  7. var db_migrations = {
  8.   v1: {
  9.     version: "1.0",
  10.     up: function(tx) {
  11.       tx.executeSql('CREATE TABLE IF NOT EXISTS food (id INTEGER AUTOINCREMENT PRIMARY KEY, food_list_type SMALLINT, name VARCHAR(255));');
  12.     },
  13.     down: function(tx) {
  14.       tx.executeSql('DROP TABLE IF EXISTS food;');
  15.     },
  16.   },
  17.  
  18.   v2: {
  19.     version: "2.0",
  20.     up: function(tx) {
  21.       tx.executeSql('CREATE TABLE IF NOT EXISTS food_notes (food_id INTEGER NOT NULL, note TEXT, picture VARCHAR(255));');
  22.       tx.executeSql('CREATE INDEX IF NOT EXISTS food_notes_food_id_idx ON food_notes(food_id);');
  23.  
  24.       tx.executeSql('CREATE TABLE IF NOT EXISTS food_in_food (food_id INTEGER NOT NULL, paremt_id INTEGER NOT NULL);');
  25.       tx.executeSql('CREATE INDEX IF NOT EXISTS food_in_food_id_idx ON food_in_food(food_id);');
  26.       tx.executeSql('CREATE UNIQUE INDEX IF NOT EXISTS food_in_food_uniq_idx ON food_in_food(paremt_id,food_id);');
  27.  
  28.     },
  29.     down: function(tx) {
  30.       tx.executeSql('DROP INDEX IF EXISTS food_notes_food_id_idx;');
  31.       tx.executeSql('DROP TABLE IF EXISTS food_notes;');
  32.  
  33.       tx.executeSql('DROP INDEX IF EXISTS food_in_food_id_idx;');
  34.       tx.executeSql('DROP INDEX IF EXISTS food_in_food_uniq_idx;');
  35.       tx.executeSql('DROP TABLE IF EXISTS food_in_food;');
  36.     },
  37.   },
  38.  
  39. };
  40.  
  41. function createDB(name,displayName, size) {
  42.   db = window.openDatabase(name, "", displayName, size);
  43.   for(m in db_migrations) {
  44.     current_migration = db_migrations[m];
  45.     if (db.version < current_migration.version) {
  46.       db.transaction(current_migration.up, createDBError, createDBSuccess);
  47.     }
  48.   }
  49. }
  50.  
  51. function createDBSuccess() {
  52.   current_schema_version = current_migration.version;
  53.   db.changeVersion(db.version,current_schema_version);
  54. }
  55.  
  56. function createDBError(err) {
  57.   navigator.notification.alert('Error updating '+db.version+' database: '+err.message);
  58.   db.transaction(current_migration.down,createDBErrorFatal,createDBSuccessAll);
  59. }
  60.  
  61.  
  62. function createDBErrorFatal(err) {
  63.   navigator.notification.alert('Fatal Error when rollback '+current_migration.version+' database: '+err.message);
  64. }
  65.  
  66. function createDBSuccessAll() {
  67.   navigator.notification.alert('Database rollback from '+current_migration.version+' version');
  68. }
  69.  
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement