Merge lp:~rpadovani/ubuntu-calculator-app/dbupgrade into lp:~ubuntu-calculator-dev/ubuntu-calculator-app/old_trunk

Proposed by Riccardo Padovani
Status: Merged
Approved by: Gustavo Pichorim Boiko
Approved revision: 119
Merged at revision: 129
Proposed branch: lp:~rpadovani/ubuntu-calculator-app/dbupgrade
Merge into: lp:~ubuntu-calculator-dev/ubuntu-calculator-app/old_trunk
Diff against target: 79 lines (+46/-17)
1 file modified
Storage.qml (+46/-17)
To merge this branch: bzr merge lp:~rpadovani/ubuntu-calculator-app/dbupgrade
Reviewer Review Type Date Requested Status
Gustavo Pichorim Boiko (community) Approve
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Review via email: mp+179023@code.launchpad.net

Commit message

Created a new function to upgrade database and avoid problems adding new version of db

Description of the change

Created a new function to upgrade database and avoid problems adding new version of db

To post a comment you must log in.
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Gustavo Pichorim Boiko (boiko) wrote :

Looks good! Thanks for the fix.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Storage.qml'
2--- Storage.qml 2013-07-30 17:06:03 +0000
3+++ Storage.qml 2013-08-07 18:03:32 +0000
4@@ -21,29 +21,58 @@
5 import "./dateutils.js" as DateUtils
6
7 Item {
8-
9 property var db: null
10
11 function openDB() {
12 if(db !== null) return;
13
14 db = LocalStorage.openDatabaseSync("ubuntu-calculator-app", "", "Default Ubuntu touch calculator", 100000);
15-
16- if (db.version == "") {
17- db.changeVersion("", "0.1",
18- function(tx) {
19- tx.executeSql('CREATE TABLE IF NOT EXISTS Calculations(id INTEGER PRIMARY KEY, calc TEXT)');
20- console.log('Database created');
21- });
22- }
23-
24- if (db.version == "0.1") {
25- db.changeVersion("0.1", "0.1.1",
26- function(tx) {
27- tx.executeSql('ALTER TABLE Calculations ADD insertDate INTEGER NOT NULL DEFAULT 0');
28- console.log('Database upgraded to 0.1.1');
29- });
30- }
31+ upgradeDB(db.version);
32+ }
33+
34+ /* We need this function because db.version is in the .ini file, that is update only by Javascript Garbage Collection.
35+ * So, we have to upgrade from actual version to the last version using only once db.changeVersion.
36+ * To avoid to have a lot of switch and spaghetti-code, this function allow to add new db version without modify old sql
37+ *
38+ * IMPORTANT: NUMBER OF VERSION HAVE TO BE INT (e.g. 0.1, not 0.1.1)
39+ */
40+ function upgradeDB() {
41+ // This is the array with all the sql code, insert your update at the last
42+ var sqlcode = [
43+ 'CREATE TABLE IF NOT EXISTS Calculations(id INTEGER PRIMARY KEY, calc TEXT)',
44+ 'ALTER TABLE Calculations ADD insertDate INTEGER NOT NULL DEFAULT 0'
45+ ]
46+
47+ // This is the last version of the DB, remember to update when you insert a new version
48+ var lastVersion = "0.2";
49+
50+ // Hack for change old numeration with new one
51+ if (db.version == "0.1.1") {
52+ db.changeVersion("0.1.1", "0.2");
53+ console.log("Fixed DB!");
54+ }
55+
56+ // So, let's start the version change...
57+ db.changeVersion(db.version, lastVersion,
58+ function(tx) {
59+ if (db.version < 0.1) {
60+ tx.executeSql(sqlcode[0]);
61+ console.log('Database upgraded to 0.1');
62+ }
63+ if (db.version < 0.2) {
64+ tx.executeSql(sqlcode[1]);
65+ console.log('Database upgraded to 0.2');
66+ }
67+
68+ /* This is the structure of the update:
69+ * n is the number of version that sql update to
70+ * m is the number of the sql element in the array. Remember that the number of the first element of array is 0 ;)
71+ if (db.version < n) {
72+ tx.executeSql(sqlcode[m]);
73+ console.log('Database upgraded to n');
74+ }
75+ */
76+ }); // Finish db.changeVersion
77 }
78
79 function getCalculations(callback){

Subscribers

People subscribed via source and target branches