Axway Titanium™ SDK : versioning your application

When you’re develop your application at first, sometimes you didn’t think about the evolution of your app. If you use a database, you need a way to add some table or field on existing table and you need to execute this script only on the update of your app. To do so, i use a simple technique to save a version of my app.

First, you need to create a database. You can do it with create a file into lib folder. I call it db.js, here is the content :

module.exports = Ti.Database.open(‘databaseName’);

Then, create another file into lib folder and call it install.js

(function(){
var db = require('db');
/**
* var _exports
*
* @return {type}
*/
var _exports = function(){
var version = Ti.App.Properties.getInt('versionDatabase'),
versionOrigine = version;
if(!version){
version = 0;
}
if(version < 1){
Ti.App.Properties.setBool('isConnected', false);
db.execute('CREATE TABLE IF NOT EXISTS variable (id INTEGER PRIMARY KEY AUTOINCREMENT, key_ VARCHAR(255) NOT NULL, value TEXT NULL)');
version = 1;
}
if(version > versionOrigine){
Ti.App.Properties.setInt('versionDatabase', version);
}
};
module.exports = _exports;
})();

In this file, you can see that i get a properties called versionDatabase. On the first installation, this variable is null, so i set it to version = 0. The first condition will be execute after the installation. Here, you can see i’m doing to thing :

  1. Create a bool isConnected and set it to false. It’s to determine is a user is connected or not
  2. I execute a SQLite request to create a table named variable. I use this table to store persistent properties.
  3. I set the variable version to 1

The statement after is useful to save the current version of your application. Now, if you relaunch your app, this file will be called, but your version variable will be equal to 1 so it will be nothing.

What next when you need to publish a new version of your app ?

On the next version, you need to edit the install.js file and add a new statement like this :

if(version < 2){
//do anything you want, add new table, add new fields on existing table, execute some script
version = 2;

}

One more thing. You need to call your install.js file on alloy.js file :

require('install')();

And, voilà !

You can find the whole code here :

About me

I’m a french mobile developper. I use Axway Appcelerator since 2012 and i love it ! I’m actually the CTO of Squirrel.fr, a mobile agence in Paris and Reunion Island and official partner of Axway.