Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to create split migrations for same version. #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions migrations_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,16 @@ Migrations._migrateTo = function(version, rerun) {
return;
}

var startIdx = this._findIndexByVersion(currentVersion);
var endIdx = this._findIndexByVersion(version);
var startIdx = this._findMigrationsByVersion(currentVersion);
var endIdx = this._findMigrationsByVersion(version);

if(currentVersion < version){
startIdx = startIdx[startIdx.length-1];
endIdx = endIdx[endIdx.length-1];
}else{
startIdx = startIdx[startIdx.length-1];
endIdx = endIdx[0];
}

// log.info('startIdx:' + startIdx + ' endIdx:' + endIdx);
log.info('Migrating from version ' + this._list[startIdx].version
Expand Down Expand Up @@ -253,14 +261,18 @@ Migrations._setControl = function(control) {
return control;
}

// returns the migration index in _list or throws if not found
Migrations._findIndexByVersion = function(version) {
// returns the migrations indexs in _list or throws if not found
Migrations._findMigrationsByVersion = function(version) {
var migrationIndex = [];
for (var i = 0;i < this._list.length;i++) {
if (this._list[i].version === version)
return i;
migrationIndex.push(i);
}

throw new Meteor.Error('Can\'t find migration version ' + version);
if(migrationIndex.length === 0)
throw new Meteor.Error('Can\'t find migration version ' + version);

return migrationIndex.sort();
}

//reset (mainly intended for tests)
Expand Down
65 changes: 65 additions & 0 deletions migrations_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ Tinytest.add('Migrates up several times.', function(test) {
test.equal(Migrations.getVersion(), 4);
});

Tinytest.add('Migrates up several times with multiple Migrations of same version.', function(test) {
var run = []; //keeps track of migrations in here
Migrations._reset();

// first one
Migrations.add({up: function () {run.push('u1-2');}, version: 1});
Migrations.add({up: function () {run.push('u1-1');}, version: 1});

// migrates once
Migrations.migrateTo('latest');
test.equal(run, ['u1-2','u1-1']);
test.equal(Migrations.getVersion(), 1);

// add two more, out of order
Migrations.add({up: function () {run.push('u4-1');}, version: 4});
Migrations.add({up: function () {run.push('u4-2');}, version: 4});

Migrations.add({up: function () {run.push('u3-2');}, version: 3});
Migrations.add({up: function () {run.push('u3-1');}, version: 3});

// should run the next two nicely in order
Migrations.migrateTo('latest');
test.equal(run, ['u1-2', 'u1-1', 'u3-2', 'u3-1', 'u4-1', 'u4-2']);

test.equal(Migrations.getVersion(), 4);
});

Tinytest.add('Tests migrating down', function(test) {
var run = []; //keeps track of migrations in here
Migrations._reset();
Expand Down Expand Up @@ -91,6 +118,44 @@ Tinytest.add('Tests migrating down', function(test) {
test.equal(Migrations.getVersion(), 2);
});

Tinytest.add('Tests migrating down with multiple Migrations of same version.', function(test) {
var run = []; //keeps track of migrations in here
Migrations._reset();

// add the migrations
Migrations.add({up: function () {run.push('u1');}, version: 1});
Migrations.add({up: function () {run.push('u2');}, version: 2});
Migrations.add({
up: function () {run.push('u3-1');},
down: function () {run.push('d3-1');},
version: 3,
name: 'Down Migration - 1' //give it a name, just for shits
});
Migrations.add({
up: function () {run.push('u3-2');},
down: function () {run.push('d3-2');},
version: 3,
name: 'Down Migration - 2' //give it a name, just for shits
});

// migrates up
Migrations.migrateTo('latest');
test.equal(run, ['u1', 'u2', 'u3-1', 'u3-2']);
test.equal(Migrations.getVersion(), 3);

// migrates down
Migrations.migrateTo('2');
test.equal(run, ['u1', 'u2', 'u3-1', 'u3-2', 'd3-2', 'd3-1']);
test.equal(Migrations.getVersion(), 2);

// should throw as migration u2 has no down method and remain at the save ver
test.throws(function() {
Migrations.migrateTo('1');
}, /Cannot migrate/);
test.equal(run, ['u1', 'u2', 'u3-1', 'u3-2', 'd3-2', 'd3-1']);
test.equal(Migrations.getVersion(), 2);
});

Tinytest.add('Tests migrating down to version 0', function(test) {
var run = []; //keeps track of migrations in here
Migrations._reset();
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
summary: "Define and run db migrations.",
version: "0.9.6",
version: "0.9.7",
name: "percolate:migrations",
git: "https://github.com/percolatestudio/meteor-migrations.git"
});
Expand Down