GithubHelp home page GithubHelp logo

grunt-mongo-migrations's Introduction

grunt-mongo-migrations

A helper grunt task to manage Mongoose MongoDB database migrations.

Dependency status Build Status

Description

grunt-mongo-migrations works by keeping track of migration files that it executes in the migrations_versions MongoDB collection. You create and write your migrations which you then execute on your server. Migrations that were already executed will not execute again.

Installation

$ npm install grunt-mongo-migrations

Tests

$ npm test

Usage

Gruntfile

module.exports = (grunt) ->
  grunt.loadNpmTasks 'grunt-mongo-migrations'

  grunt.initConfig
    migrations:
      path: "#{__dirname}/migrations"
      template: grunt.file.read "#{__dirname}/migrations/_template.coffee" # optional
      mongo: 'mongodb://localhost:12345'
      ext: "coffee"

Options

path (required)

Type: String
Path to directory containing migrations.

template

Type: String
Template for generating new migration files.

mongo

Type: String | Object | Function
If a string is specified, it is the connection string for MongoDB. If an object is specified, it may have two properties: uri (required) which is the connection string, and opts which will be passed through to mongoose.createConnection. Ex:

grunt.initConfig
  migrations:
    path: "#{__dirname}/migrations"
    mongo:
      uri: 'mongodb://localhost:12345'
      opts:
        ssl: true

If a function is specified, it must return either a string or object as described above.

ext

Type: String
Default: 'coffee'
Values: 'coffee' | 'js'
Migrations file extension.

_template.coffee

By default the task will generate a migration in CoffeeScript using the same template as in this example. You can of course provide your own template in JavaScript.

module.exports =
  requiresDowntime: FIXME # true or false

  up: (callback) ->
    // your migration goes here
    callback()

  down: (callback) ->
    // throw new Error('Irreversible migration.');
    callback()

  test: (callback) ->
    // your test goes here
    callback()

Down Time Management

requiresDowntime is an option that intentionaly set to invalid value so that developer has to fix it. The flag is meant for your own deployment pipeline to be aware that there's a migration that requires server down time.

Tasks

  • migrate:generate
  • migrate:pending
  • migrate:all
  • migrate:one
  • migrate:test

migrate:generate

Will create a migration using template in the file name with a [timestamp]_[name].[ext]

$ grunt migrate:generate --name=rename_created_on_to_created_at

Running "migrate:generate" task
>> Created `./migrations/20131108211056037_rename_created_on_to_created_at.coffee`

Done, without errors.

migrate:pending

Prints out a list of pending migrations

$ grunt migrate:pending

Running "migrate:pending" task
>> `20131108193444023_move_users` is pending (requires downtime)
>> `20131108211056037_rename_created_on_to_created_at` is pending

migrate:all

Runs all pending migrations.

$ grunt migrate:all

Running "migrate:all" task
>> Running migration `20131108193444023_test1`
>> Running migration `20131108211056037_rename_created_on_to_created_at`
>> Finished migrations

Done, without errors.

Running this again will do nothing because all migrations have been ran.

$ grunt migrate:all

Running "migrate:all" task
>> Finished migrations

Done, without errors.

migrate:down

Runs down the last migration.

$ grunt migrate:all

Running "migrate:down" task
>> Reversing migration `20131108211056037_rename_created_on_to_created_at`
>> Migrated down

migrate:one

Runs specific migration by name. If it was already executed before, will generate an error.

$ grunt migrate:one --name=rename_created_on_to_created_at

Running "migrate:one" task
>> Running migration `20131108211056037_rename_created_on_to_created_at`
>> Finished migrations

Done, without errors.

migrate:test

Runs specific migration by name via test runner. You should take care of using correct test databases. The template includes a proposed way of copying development database into test on every run.

$ grunt migrate:test --name=rename_created_on_to_created_at

Running "migrate:test" task
>> Testing migration `20131108211056037_rename_created_on_to_created_at`
>> Finished migrations

Done, without errors.

License

The MIT License (MIT)

Copyright (c) 2013 Good Eggs Inc.

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.

grunt-mongo-migrations's People

Contributors

alexgorbatchev avatar bobzoller avatar burtonjc avatar danypype avatar goodeggs-terraformer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-mongo-migrations's Issues

Project organization

Don't you think it would be clearer/cleaner to have the coffee source in src/ and the compiled JavaScript in lib/? It seems like a standard convention to have the runtime code in lib/.

I've even seen some folks publish to npm without the src/ and test/ dirs and only lib/. I'm not proposing that - simply that src/ and lib/ would be more common/standard organizational approaches.

Integration with MEANjs

Hi

I'm looking to incorporate a mongodb migrations framework into my meanjs app. I have read snez's notes on integrating mongo-migrate https://gist.github.com/snez/ce1248661aefc15cb8d0#file-gistfile1-md. The problem I have with this approach is that it looks like you have to duplicate the mongo connection details.

Looking around for other frameworks I like the look of grunt-mongo-migrations which integrates with grunt nicely. What I haven't worked out yet is how to integrate this with meanjs' config. I want to use the NODE_ENV env variable to determine the connection string. So I have the following in my gruntFile.js:

// Project Configuration
grunt.initConfig({
...
migrations: {
      path: 'migrations',
      template: grunt.file.read('migrations/_template.js'),
      mongo: '<%= db  %>',
      ext: 'js'
}
});

...

// A Task for loading the configuration object
grunt.task.registerTask('loadConfig', 'Task that loads the config into a grunt option.', function() {
var init = require('./config/init')();
var config = require('./config/config');

grunt.config.set('applicationJavaScriptFiles', config.assets.js);
grunt.config.set('applicationCSSFiles', config.assets.css);
grunt.config.set('db', config.db);
});

...

grunt.loadNpmTasks('grunt-mongo-migrations');

...

// Execute MongoDB migrations
grunt.registerTask('mongodb-migrate', ['loadConfig', 'migrate:all']);

// List MongoDB migrations
grunt.registerTask('mongodb-list-migrations', ['loadConfig', 'migrate:pending']);

Generating the migrations using grunt-mongo-migrations'
grunt migrate:generate --name=rename_created_on_to_created_at task works. So I get an empty migration file in /migrations. However if I try and list the migrations using my grunt task I get:

Tims-MacBook-Pro:run-fit-uk tim$ grunt mongodb-list-migrations
Running "loadConfig" task

 Application loaded using the "development" environment configuration

Running "migrate:pending" task
Fatal error: Cannot find module 'migrations/20150311075542073_do_something'

and if I try to execute the migration also get

Tims-MacBook-Pro:run-fit-uk tim$ grunt mongodb-migrate
Running "loadConfig" task

 Application loaded using the "development" environment configuration


Running "migrate:all" task
Fatal error: Cannot find module 'migrations/20150311075542073_do_something'

The location that is in the output of the task has the correct path.

I'm stumped, any suggestions?

Thanks
Tim

Cross posted from meanjs forum: https://groups.google.com/forum/#!topic/meanjs/FcqSMtcUHxc

Examples

Could you add an example for Grunt Configuration for JS as well as the Coffeescript example?

Better support for testing migrations

Currently, running a test on a migration gives you something like this:
screen shot 2015-01-27 at 4 23 28 pm
It would be great if Mocha was configured for you. It would also be nice to be able to run the tests for all pending migrations.

Are there any working examples of migration tests out there we could look at?

DB credentials in gruntfile?

How would one use this library without putting db creds into the gruntfile? I don't want to do that because the gruntfile gets checked into source control.

Other libraries accomplish this by having config files that are loaded. These config files can then be added to .gitignore.

Publish 0.7.0

Looks like 0.7.0 has not been published to npm yet. Mind publishing? Thank you!

0.6.1 not published to NPM

Hey,
Mind publishing 0.6.1 to npm? It looks like there have been good fixes (file extension related) that I would love to take advantage of.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.