In this post we present a solution to a common problem when developing Adobe AIR applications with local persistency via SQLite. The problem is how to update the SQL schema of the client application.
After the application has been released to clients, we might need to push and update to clients, who have installed the AIR application. This is achieved via the
AIR update framework. The updated application might contain certain changes of the SQLite schema, which might cause failure when the old SQLite database on the client machine is used. We need to upgrade the existing database and its schema. In this post we present an approach, where the schema upgrades are incorporated in the AIR application, and are version aware.
Example scenario:User has installed version 1.0.0 of an AIR application which has a DB table named USER with certain fields in it. In the next version 1.0.1 we introduce new field in the USER table, called for example LAST_UPDATE of type DATE. We use
DAO approach to access and manipulate the DB. When the user updates its AIR application to 1.0.1, the DB access layer won't be able to access the DB anymore.
We handle the problem in the following way:
1. When application is started, we check whether it has been updated. We do this, by comparing the current application version to the last stored version. We store the version number somewhere in the application storage folder for example.
2. When the new application version is greater than the current one, we execute a DB upgrade routine. The routine reads an XML file in this form:
<upgrades>
<upgrade>
<version>1.0.1</version>
<statement>ALTER TABLE `USER` ADD COLUMN `LAST_UPDATE` DATE NULL;</statement>
</upgrade>
</upgrades>
The XML file contains sequence of upgrade SQL statements with the respective version. The routine checks whether the current version is less than the upgrade version and executes the statement if needed.
Notes:All future DB upgrades are included with their respective versions in the XML file. For example if the client updates the AIR application from version 1.0.0 to version 1.2.0 then all the relevant upgrades will be executed (for example 1.0.1, 1.0.2, 1.0.5 and 1.2.0).
The routine would need a version comparison function, which determines whether version x.y.z is greater than version a.b.c.
If no last stored version is available, then we must create the DB schema from scratch - this means the application is started for the first time.