Not quite as exciting as a bowl of a really good dip.
Google announced Gears. Which has nifty features such as providing a local server for asset files, a workerpool for shoving things out of the main “thread” (background those db syncs, for example), and accessible-by-javascript sqlite databases. SQL based local data store? Sign me up! Awesome.
Except for one thing. I hate the idea of having to keep track of what version of the database is on the client, and I really wanted something like Migrations from Rails. Not being the kind to just wait around and wait for someone else, I came up with Gearshift (name by Alan). It doesn’t do the “express in ruby have sql made for you”, but it does handle go up and down the version history.
Click on through to the Gearshift homepage for the download and a simple demo.
You need gears, obviously, and that’s about it. Right now it needs something that provides the $A “toArray” function, and provides a .each() iterator for Array. Prototype or mootools would both do. Future versions should drop this requirement.
Define some rules - it’s up to you to return true if your rule has succeeded, false if not.
Gearshift.rules[1] = {
// create the demo table
up: function() {
return this.e("CREATE TABLE demo_table (
id INTEGER PRIMARY KEY
AUTOINCREMENT,
name VARCHAR(30),
movie VARCHAR(30)
)").success;
},
down: function() {
return this.e("DROP TABLE demo_table").success;
}
};
Gearshift.rules[2] = {
up: function() {
a = this.e("INSERT INTO demo_table (name, movie)
VALUES (?, ?)", ['Trey Parker',
'Team America: World Police']).success;
b = this.e("INSERT INTO demo_table (name, movie)
VALUES (?, ?)", ['Kristen Miller',
'Puff, Puff, Pass']).success;
c = this.e("INSERT INTO demo_table (name, movie)
VALUES (?, ?)", ['Michael Madsen',
'Buttermilk Sky']).success;
return a && b && c;
},
down: function() {
return this.e("DELETE FROM demo_table");
}
};
And then:
// db = google.gears.factory.create('beta.database', '1.0');
db.open("com.pftqg.testing");
Gearshift.init(db, true);
The init function takes two parameters, a suitable database and a boolean that dictates the auto migrate functionality - if true it’ll always attempt to take you up to the latest rule.
There’s some manual stuff too, though:
Gearshift.whatIsMyVersion()
Gearshift.latestVersion() // highest rule possible
Gearshift.migrateTo(version) // manually migrate