Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
Merge branch 'features/fix-specs'
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmalov committed Oct 16, 2014
2 parents 55f9f24 + 783baad commit dfa2183
Show file tree
Hide file tree
Showing 17 changed files with 364 additions and 460 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules
*.log
logs
test/.tmp
coverage
.idea
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
language: node_js
node_js:
- "0.8"
- "0.10"
env:
- TEST_CMD=test
- TEST_CMD=lint
- NODE_ENV=test
before_script:
- npm install -g grunt-cli
- npm install sequelize
- npm i -g grunt-cli
- npm i sequelize
script:
- grunt travis --stack
- grunt validate
- npm run-script coverage
after_script:
- if [ "$TEST_CMD" = 'test' ]; then npm run coveralls; fi
- npm run-script coveralls
34 changes: 12 additions & 22 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

'use strict';

module.exports = function(grunt) {
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);

var files = {
lib: ['lib/**/*.js', 'tasks/**/*.js'],
lib: ['lib/**/*.js', 'tasks/**/*.js', '*.js'],
test: ['test/**/*.js'],
specs: ['test/**/.spec.js']
specs: ['test/**/*_spec.js']
};

grunt.initConfig({
Expand Down Expand Up @@ -45,30 +45,20 @@ module.exports = function(grunt) {
},

specs: files.specs
},

sequelize: {
options:{
dialect: 'sqlite',
storage: 'test/.tmp/db-test.sqlite',
logging: false,
migrationsPath: __dirname + '/test/migrations'
}
}

});

// Actually load this plugin's task(s).
grunt.loadTasks('tasks');
if (process.env.NODE_ENV === 'integration') {
grunt.config.set('sequelize', {
options: {
config: __dirname + '/db/config.json',
migrations: __dirname + '/db/migrations'
}
});
grunt.loadTasks('tasks');
}

grunt.registerTask('test', ['clean', 'mochaTest']);
grunt.registerTask('validate', ['jshint', 'jscs']);

if(process.env.TEST_CMD) {
grunt.registerTask('travis', process.env.TEST_CMD);
}

// By default, lint and run all tests.
grunt.registerTask('default', ['validate', 'test']);

};
53 changes: 40 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# grunt-sequelize

![Build](https://travis-ci.org/bencevans/grunt-sequelize.svg)
![Coverage](https://coveralls.io/repos/bencevans/grunt-sequelize/badge.png)
![Dependencies](https://david-dm.org/bencevans/grunt-sequelize.svg)
![Dev dependencies](https://david-dm.org/bencevans/grunt-sequelize/dev-status.svg)
![Peer dependencies](https://david-dm.org/bencevans/grunt-sequelize/peer-status.svg)

### Looking for a new maintainers, please contact @bencevans

> Sequelize migrations from Grunt
Expand Down Expand Up @@ -28,28 +34,36 @@ In your project's Gruntfile, add a section named `sequelize` to the data object
grunt.initConfig({
sequelize: {
options: {
migrationsPath: __dirname + '/migrations',
// The following is the sequelize config you're used to
dialect: 'postgres',
username: 'postgres',
database: '4_r34lly_s3cur3_p455w0rd',
host: '127.0.0.1'
migrations: 'db/migrations',
config: 'db/config.json'
}
},
}
})
```

### Options

#### options.migrationsPath
#### options.migrations
Type: `String`

A string value that is used to location your migration files.

#### options.*
Type: Many
#### options.config
Type: `String`

A string value that is used to locate your sequelize db config.

Sequelize options as you would usually use for the Sequelize client.
##### Example of config.json

```js
{
"development": {
"username": "user",
"password": "pwd",
"database": "my_cool_db"
}
}
```

### Running tasks

Expand All @@ -61,14 +75,27 @@ You can run the migrations up to the top migration by running:

$ grunt sequelize:migrate

or

$ grunt sequelize:migrate:up

#### Undo

In order to migrate down the stack, use:

$ grunt sequelize:undo
$ grunt sequelize:migrate:undo

#### Redo

Also you can redo all your migrations by running:

$ grunt sequelize:migrate:redo

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
In lieu of a formal styleguide, take care to maintain the existing coding style.
Add unit tests for any new or changed functionality. Validate and test your code by running [Grunt](http://gruntjs.com/)

$ grunt validate

## Release History
_(Nothing yet)_
6 changes: 3 additions & 3 deletions data/migration.js → lib/assets/migration.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

module.exports = {
up: function(migration, DataTypes, done) {
up: function (migration, DataTypes, done) {
// add altering commands here, calling 'done' when finished
done();
},
down: function(migration, DataTypes, done) {
down: function (migration, DataTypes, done) {
// add reverting commands here, calling 'done' when finished
done();
}
};
};
40 changes: 40 additions & 0 deletions lib/migrate_task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var Sequelize = require('sequelize');
var _ = require('lodash');

function MigrateTask(opts) {
this._migratorOpts = process.env.NODE_ENV === 'test' ?
{ path: opts.migrations } : /* istanbul ignore next */
{ path: opts.migrations, logging: console.log };

this.db = new Sequelize(opts.database, opts.username, opts.password, opts);
this.migrator = this.db.getMigrator(this._migratorOpts);
}

MigrateTask.prototype.up = function () {
return this.migrator.migrate();
};

MigrateTask.prototype.down = function () {
return this.migrator.findOrCreateSequelizeMetaDAO()
.then(function (Meta) {
return Meta.find({ order: 'id DESC' });
})
.then(function (meta) {
if (!meta) {
return;
}

return this.db.getMigrator(_.extend(meta.dataValues, this._migratorOpts), true)
.migrate({ method: 'down' });

}.bind(this));
};

MigrateTask.prototype.redo = function () {
return this.down()
.then(this.up.bind(this));
};

module.exports = MigrateTask;
39 changes: 39 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

var utils = exports;
var path = require('path');
var _ = require('lodash');

utils.lastMigrationId = function (migrator) {
return migrator.findOrCreateSequelizeMetaDAO()
.then(function (Meta) {
return Meta.find({ order: 'id DESC' });
})
.then(function (meta) {
return meta ? meta.to : null;
});
};

utils.pad = function (n) {
return n < 10 ? '0' + n : n;
};

utils.ts = function () {
var date = new Date();

return date.getFullYear().toString() +
utils.pad(date.getMonth() + 1).toString() +
utils.pad(date.getDate()).toString() +
utils.pad(date.getHours()).toString() +
utils.pad(date.getMinutes()).toString() +
utils.pad(date.getSeconds()).toString();
};

utils.optsProvider = function (grunt) {
return function (baseDbPath) {
return _.defaults(grunt.config.get('sequelize.options'), {
config: path.join(baseDbPath, 'config.json'),
migrations: path.join(baseDbPath, 'migrations')
});
};
};
27 changes: 17 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,43 @@
"url": "https://github.com/bencevans/grunt-sequelize/blob/master/LICENSE-MIT"
}
],
"main": "Gruntfile.js",
"main": "tasks/sequelize.js",
"engines": {
"node": ">= 0.8.0"
"node": ">= 0.10.0"
},
"scripts": {
"test": "grunt test",
"coverage": "rm -rf coverage && istanbul cover _mocha --report lcov",
"coveralls": "rm -rf coverage && istanbul cover _mocha --report lcovonly cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
"test": "_mocha ./test/**/*_spec.js",
"coverage": "rm -rf coverage && istanbul cover _mocha --report lcovonly -- ./test/**/*_spec.js",
"coveralls": "cat coverage/lcov.info | coveralls"
},
"devDependencies": {
"chai": "^1.9.2",
"coveralls": "~2.3.0",
"chai-as-promised": "^4.1.1",
"coveralls": "^2.11.2",
"grunt": "~0.4.1",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-jscs": "^0.7.1",
"grunt-mocha-test": "^0.12.1",
"istanbul": "^0.3.2",
"load-grunt-tasks": "^0.6.0",
"lodash": "^2.4.1",
"mkdirp": "^0.5.0",
"mocha": "^1.21.5",
"random-string": "^0.1.1",
"shared-examples-for": "^0.1.2",
"sqlite3": "^3.0.2",
"time-grunt": "^1.0.0"
"time-grunt": "^1.0.0",
"timekeeper": "0.0.4",
"tmp": "0.0.24"
},
"peerDependencies": {
"grunt": "~0.4.1",
"sequelize": "1.7.x"
"sequelize": "2.0.0-rc1"
},
"keywords": [
"gruntplugin"
]
],
"dependencies": {
"lodash": "^2.4.1"
}
}
Loading

0 comments on commit dfa2183

Please sign in to comment.