• Stars
    star
    245
  • Rank 165,264 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple migration system for Meteor

percolate:migrations

Build Status

A simple migration system for Meteor supporting up/downwards migrations and command line usage. There is also a fork available for use outside of Meteor.

Installation

Meteor Migrations can be installed through Meteor's package manager. Type:

$ meteor add percolate:migrations

API

Basics

To write a simple migration, somewhere in the server section of your project define:

Migrations.add({
  version: 1,
  up: function() {//code to migrate up to version 1}
});

To run this migration from within your app call:

Meteor.startup(() => {
  Migrations.migrateTo('latest');
});

Advanced

A more complete set of migrations might look like:

Migrations.add({
  version: 1,
  name: 'Adds pants to some people in the db.',
  up: function() {//code to migrate up to version 1}
  down: function() {//code to migrate down to version 0}
});

Migrations.add({
  version: 2,
  name: 'Adds a hat to all people in the db who are wearing pants.',
  up: function() {//code to migrate up to version 2}
  down: function() {//code to migrate down to version 1}
});

As in 'Basics', you can migrate to the latest by running:

Meteor.startup(function() {
  Migrations.migrateTo('latest');
});

Note: Migrations should be run from Meteor.startup to allow for log output configuration.

By specifying a version, you can migrate directly to that version (if possible). The migrations system will automatically determine which direction to migrate in.

In the above example, you could migrate directly to version 2 by running:

Migrations.migrateTo(2);

If you wanted to undo all of your migrations, you could migrate back down to version 0 by running:

Migrations.migrateTo(0);

Sometimes (usually when somethings gone awry), you may need to re-run a migration. You can do this with the rerun subcommand, like:

Migrations.migrateTo('3,rerun');

NOTE: You cannot create your own migration at version 0. This version is reserved by migrations for a 'vanilla' system, that is, one without any migrations applied.

To see what version the database is at, call:

Migrations.getVersion();

Configuration

You can configure Migrations with the config method. Defaults are:

Migrations.config({
  // Log job run details to console
  log: true,

  // Use a custom logger function (defaults to Meteor's logging package)
  logger: null,

  // Enable/disable logging "Not migrating, already at version {number}"
  logIfLatest: true,

  // migrations collection name to use in the database
  collectionName: "migrations"
});

Logging

Migrations uses Meteor's logging package by default. If you want to use your own logger (for sending to other consumers or similar) you can do so by configuring the logger option.

Migrations expects a function as logger, and will pass arguments to it for you to take action on.

var MyLogger = function(opts) {
  console.log('Level', opts.level);
  console.log('Message', opts.message);
  console.log('Tag', opts.tag);
}

Migrations.config({
  logger: MyLogger
});

Migrations.add({ name: 'Test Job', ... });

The opts object passed to MyLogger above includes level, message, and tag.

  • level will be one of info, warn, error, debug.
  • message is something like Finished migrating..
  • tag will always be "Migrations" (handy for filtering).

Custom collection name

By default, the collection name is migrations. There may be cases where this is inadequate such as using the same Mongo database for multiple Meteor applications that each have their own set of migrations that need to be run.

Command line use

*** DEPRECATED ***

This info is for pre 0.9 users as post 0.9 the migrate.sh script is no longer included in the package folder.

You can also run migrations from the command line using the included shell script. This will

  1. Launch your Meteor app
  2. Call Migrations.migrateTo(version)
  3. Exit your app

For instance, from your project's root, run:

$ ./packages/percolatestudio-migrations/migrate.sh latest

You can also specify additional arguments to be passed into meteor, like:

$ ./packages/percolatestudio-migrations/migrate.sh latest --settings ./setting.json

Errors

  1. Not migrating, control is locked

Migrations set a lock when they are migrating, to prevent multiple instances of your clustered app from running migrations simultaneously. If your migrations throw an exception, you will need to manually remove the lock (and ensure your db is still consistent) before re-running the migration.

From the mongo shell update the migrations collection like this:

$ meteor mongo

db.migrations.update({_id:"control"}, {$set:{"locked":false}});
exit

Alternatively you can unlock the collection from either server code or the meteor shell using:

Migrations.unlock();

Threading and Callbacks

The following is example code to wait for asynchronous code to complete prior to going on to next migration.

Migrations.add({
  version: 1,
  up: Meteor.wrapAsync(async (_, next) => {
    await doSomethingAsynchonously();
    next();
  }),
  down: Meteor.wrapAsync(async (_, next) => {
    await doDownAsynchronously();
    next();
  }),
});
  • Note: You may want to to call migration after startup in case your host (such as Heroku) limits the amount of time given for startup
Meteor.startup(function() {
  setTimetout("Migrations.migrateTo('latest')", 0);
});

Contributing

  1. Write some code.

  2. Write some tests.

  3. From this package's local directory, start the test runner:

    $ meteor test-packages ./
    
  4. Open http://localhost:3000/ in your browser to see the test results.

License

MIT. (c) Percolate Studio, maintained by Zoltan Olah (@zol).

Meteor Migrations was developed as part of the Verso project.

More Repositories

1

meteor-synced-cron

A simple cron system for Meteor. It supports syncronizing jobs between multiple processes.
JavaScript
501
star
2

percolatestudio.com

JavaScript
249
star
3

publish-counts

Meteor package to help you publish the count of a cursor, in real time
JavaScript
200
star
4

ground-control

A next generation blog, built in Meteor.
CSS
183
star
5

meteor-momentum

Reactive animation package for Meteorjs
JavaScript
167
star
6

meteor-famous-demos

Demos from our devshop talk on Meteor+Famous
JavaScript
92
star
7

react-leaderboard

Meteor's leaderboard example app, rewritten in React.
JavaScript
74
star
8

atmosphere

Atmosphere - Meteor Packages
59
star
9

meteor-server-info

Query your Meteor app for diagnostics
JavaScript
59
star
10

meteor-google-api

A simple API encapsulating some common patterns regarding Google's APIs
JavaScript
48
star
11

meteor-segment.io

Basic Segment.io loader snippet Meteor package
JavaScript
29
star
12

famous-blog-examples

The code for the examples on our blog post http://blog.percolatestudio.com/engineering/the-future-of-javascript-animation-with-famous/
JavaScript
29
star
13

momentum-iron-router

JavaScript
27
star
14

league

CSS
26
star
15

development-guide

A set of (best?) practices that we follow at Percolate Studio.
Shell
23
star
16

mdg-localmarket

Recipes example app
17
star
17

cordova-plugin-safe-reload

Cordova plugin to watch and recover after a broken Meteor Hot Code Push.
JavaScript
14
star
18

amble

A proof of concept Apple Watch and Meteor application.
JavaScript
14
star
19

meteor-url-shortener

URL Shortener for a Meteor app.
JavaScript
13
star
20

meteor-wireframing

Wireframing tools for Meteor
JavaScript
13
star
21

safe-reload

Percolate Studio's Safe Reload Package for Meteor / Cordova
JavaScript
11
star
22

percolate-icons

Percolate Studio Icon Set Package
Less
8
star
23

meteor-detective

Snoop into your Meteor app
JavaScript
8
star
24

meteor-velocityjs

Meteor wrapper package for velocity.js
JavaScript
7
star
25

meteor-pack

Random odds and ends we use across our Meteor projects.
JavaScript
7
star
26

cordova-plugin-file-upload

Cordova plugin for non-multipart file uploads.
JavaScript
6
star
27

meteor-mixpanel

Light Mixpanel Loader package for Meteor.
JavaScript
5
star
28

mdg-todos

CSS
3
star
29

safe

Make method calls safely
JavaScript
3
star
30

collection-models

Simple model code for Meteor
JavaScript
3
star
31

meteor-factory-client

JavaScript
2
star
32

contact-form

JavaScript
2
star
33

tooltips

Tooltips for Meteor
JavaScript
2
star
34

collection-models-schemas

JavaScript
2
star
35

amble-victoria

Ruby
1
star
36

mdg-dashboard

JavaScript
1
star
37

cordova-plugin-file-transfer

Non Multipart version of the Cordova FileTransfer plugin
JavaScript
1
star
38

MeteorShower

JavaScript
1
star
39

mdg-leaderboard

Leaderboard example app redesign
1
star
40

mdg-recipes-landing

Landing page for recipes app
CSS
1
star
41

bound-form

A Meteor package for forms bound to their output via a reactive dictionary.
JavaScript
1
star
42

theme-percolate-blog

Percolate Studio blog WP theme
CSS
1
star