Skip to content

Instantly share code, notes, and snippets.

@toolness
Created February 11, 2013 22:24
Show Gist options
  • Select an option

  • Save toolness/4758190 to your computer and use it in GitHub Desktop.

Select an option

Save toolness/4758190 to your computer and use it in GitHub Desktop.
funky migration js api
var upgrayedd = require('../index');
var test = require('tap').test;
test('error thrown on setVersion() failure', function(t) {
upgrayedd.migrate({
toVersion: 4,
storage: {
getVersion: function(cb) { cb(null, 1); },
setVersion: function(v, cb) { cb("SETVERSION FAIL"); }
},
upgrade: null
}, function(err) {
t.same(err, "SETVERSION FAIL", "exception from setVersion() propagated");
t.end();
});
});
test('error thrown on getVersion() failure', function(t) {
upgrayedd.migrate({
toVersion: 1,
storage: {
getVersion: function(cb) {
process.nextTick(function() { cb("GETVERSION FAIL"); });
}
},
upgrade: null
}, function(err) {
t.same(err, "GETVERSION FAIL", "exception from getVersion() propagated");
t.end();
});
});
test('upgrade not called if at toVersion', function(t) {
var setVersions = [];
upgrayedd.migrate({
toVersion: 4,
storage: {
getVersion: function(cb) { cb(null, 4); },
setVersion: function(v, cb) { setVersions.push(v); cb(null); }
},
upgrade: null
}, function(err, newVersion) {
t.same(err, null, "no error is reported");
t.same(newVersion, 4, "upgraded to latest version");
t.same(setVersions, [4], "setVersion set to 4");
t.end();
});
});
test('upgrade called multiple times if needed', function(t) {
var version = 0;
var upgradePath = [];
upgrayedd.migrate({
toVersion: 4,
storage: {
getVersion: function(cb) {
process.nextTick(function() { cb(null, version); });
},
setVersion: function(newVersion, cb) {
process.nextTick(function() {
version = newVersion;
upgradePath.push(version);
cb(null);
});
}
},
upgrade: function(currVersion, maxVersion, done) {
if (currVersion == 0) return done(null, 1);
if (currVersion == 1) return done(null, 3);
if (currVersion == 3) return done(null, 4);
}
}, function(err, newVersion) {
t.same(err, null, "no error is reported");
t.same(newVersion, 4, "upgraded to latest version");
t.deepEqual(upgradePath, [0, 1, 3, 4], "upgrade path is correct");
t.end();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment