Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit. Probably doesn't work yet on Safari per blog comment.

  • Loading branch information...
commit a16de53646787ebd9a7fdf330c74d44f8cac1ff8 0 parents
@nanodeath authored
Showing with 107 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +7 −0 README
  3. +25 −0 migrator.js
  4. +56 −0 test.html
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Max Aller <nanodeath@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
7 README
@@ -0,0 +1,7 @@
+An HTML5 web database migrator utility.
+
+= Usage =
+See test.html or homepage.
+
+= License =
+MIT license, see LICENSE.
25 migrator.js
@@ -0,0 +1,25 @@
+function Migrator(db){
+ var migrations = [];
+ this.migration = function(number, func){
+ migrations[number] = func;
+ };
+ var doMigration = function(number){
+ if(migrations[number]){
+ db.changeVersion(db.version, String(number), function(t){
+ migrations[number](t);
+ }, function(err){
+ if(console.error) console.error("Error!: %o (while upgrading to %d)", err, number);
+ }, function(){
+ doMigration(number+1);
+ });
+ }
+ };
+ this.doIt = function(){
+ var initialVersion = parseInt(db.version) || 0;
+ try {
+ doMigration(initialVersion+1);
+ } catch(e) {
+ if(console.error) console.error(e);
+ }
+ }
+}
56 test.html
@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Migrator Test</title>
+ </head>
+ <body>
+ <!-- jQuery isn't required for migrator.js, just for my example code
+ below. You can use any JS library you want. -->
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
+ <script type="text/javascript" src="migrator.js"></script>
+ <script type="text/javascript">
+ var db = openDatabase("example_db", "", "Example Database", 100000);
+ // now db is a database object: http://dev.w3.org/html5/webdatabase/#database
+
+ var M = new Migrator(db);
+ M.migration(1, function(t){
+ // t here is a transaction object: http://dev.w3.org/html5/webdatabase/#sqltransaction
+ t.executeSql("create table user(id integer primary key, name text)");
+ t.executeSql("insert into user(name) values('max')");
+ });
+ M.migration(2, function(t){
+ t.executeSql("alter table user add column phone text");
+ t.executeSql("update user set phone = '555-5555' where name == 'max'");
+ });
+ M.migration(3, function(t){
+ t.executeSql("insert into user(name, phone) values('jeremy', '555-1234')");
+ t.executeSql("update user set phone = '555-5556' where name == 'max'");
+ });
+
+ // This executes the applicable transactions
+ M.doIt();
+ </script>
+ <h1>Address Book</h1>
+ <p>Here are all the numbers in the address book...</p>
+ <table class="address_book">
+ <tr>
+ <th>ID</th>
+ <th>Name</th>
+ <th>Phone</th>
+ </tr>
+ </table>
+ <script type="text/javascript">
+ db.readTransaction(function(t){
+ t.executeSql("select id, name, phone from user", [], function(t, res){
+ var rows = res.rows;
+ var addressTable = $("table.address_book");
+ for(var i = 0; i < rows.length; i++){
+ var row = rows.item(i);
+ var domRow = $("<tr><td>"+row.id+"</td><td>"+row.name+"</td><td>"+row.phone+"</td></tr>");
+ domRow.appendTo(addressTable);
+ }
+ });
+ });
+ </script>
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.