Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add intégration test for migration creation.

  • Loading branch information...
commit 45e6fa6693bda1ee1a6c874efd806cec5d2342cf 1 parent a04d86a
@kunklejr kunklejr authored
View
2  README.md
@@ -179,7 +179,7 @@ The above will run all migrations that haven't yet been run in the prod environm
## Defaults
-## Migrations
+## Migrations API
Below are examples of all the different migrations supported by db-migrate. Please note that not all migrations are supported by all databases. For example, SQLite does not support dropping columns.
View
88 test/create.js
@@ -1,88 +0,0 @@
-var vows = require('vows');
-var assert = require('assert');
-var util = require('util');
-var fs = require('fs');
-var lpad = require('../lib/util').lpad;
-var inflection = require('../lib/inflection');
-var Migration = require('../lib/migration');
-
-vows.describe('create').addBatch({
- 'create new migration': {
- topic: function() {
- return new Migration("first migration", new Date());
- },
-
- 'should provide the path to the created migration': function(migration) {
- assert.isNotNull(migration.path);
- },
-
- 'should exist': function(migration) {
- assert.isNotNull(migration);
- },
-
- 'should provide the date of the migration': function(migration) {
- assert.isNotNull(migration.date);
- assert.instanceOf(migration.date, Date);
- },
-
- 'should format the migration name': function(migration) {
- var date = migration.date;
- var dateStr = [
- date.getUTCFullYear(),
- lpad(date.getUTCMonth() + 1, '0', 2),
- lpad(date.getUTCDate(), '0', 2),
- lpad(date.getUTCHours(), '0', 2),
- lpad(date.getUTCMinutes(), '0', 2),
- lpad(date.getUTCSeconds(), '0', 2)
- ].join('');
- var migrationName = dateStr + '-' + inflection.dasherize(migration.title);
- assert.equal(migration.name, migrationName);
- },
-
- 'file': {
- topic: function(migration) {
- migration.write(function() {
- fs.stat(migration.path, function(err, stat) {
- this.callback(migration, err, stat);
- }.bind(this));
- }.bind(this));
- },
-
- 'exists': function(migration, err, stat) {
- assert.isNull(err);
- assert.isObject(stat);
- },
-
- 'is not empty': function(migration, err, stat) {
- assert.isNotZero(stat.size);
- },
-
- 'exports up and down functions': function(migration, err, stat) {
- var instance = require(migration.path);
- assert.isFunction(instance.up);
- assert.isFunction(instance.down);
- }
- },
- },
-
- 'create migration with same name as existing one': {
- topic: function() {
- return new Migration("first migration", new Date());
- },
-
- //'migration file': {
- //topic: function(migration) {
- //fs.stat(migration.path, this.callback);
- //},
-
- //'exists': function(err, stat) {
- //assert.isNull(err);
- //assert.isObject(stat);
- //},
-
- //'is not empty': function(err, stat) {
- //assert.isNotZero(stat.size);
- //}
- //}
- }
-}).export(module);
View
33 test/helper/fsext.js
@@ -0,0 +1,33 @@
+var fs = require('fs');
+var fspath = require('path');
+
+exports.rm_r = function rm_r(path, callback) {
+ fs.stat(path, function(err, stats) {
+ if (err) {
+ if (err.errno == 34) {
+ callback(null);
+ } else {
+ callback(err);
+ }
+ return;
+ }
+
+ if (stats.isFile()) {
+ fs.unlink(path, callback);
+ } else if (stats.isDirectory()) {
+ fs.readdir(path, function(err, files) {
+ if (files.length == 0) {
+ fs.rmdir(path, callback);
+ } else {
+ files.forEach(function(file, index) {
+ rm_r(fspath.join(path, file), function(err) {
+ if (index == files.length - 1) {
+ fs.rmdir(path, callback);
+ }
+ });
+ });
+ }
+ });
+ }
+ });
+};
View
54 test/integration/create.js
@@ -0,0 +1,54 @@
+var vows = require('vows');
+var assert = require('assert');
+var fs = require('fs');
+var path = require('path');
+var cp = require('child_process');
+var fsext = require('../helper/fsext');
+var dbmUtil = require('../../lib/util');
+
+
+function wipeMigrations(callback) {
+ var dir = path.join(__dirname, 'migrations');
+ fsext.rm_r(dir, callback);
+}
+
+function dbMigrate() {
+ var args = dbmUtil.toArray(arguments);
+ var dbm = path.join(__dirname, '..', '..', 'bin', 'db-migrate');
+ args.unshift(dbm);
+ return cp.spawn('node', args, { cwd: __dirname });
+}
+
+vows.describe('create').addBatch({
+ 'without a migration directory': {
+ topic: function() {
+ wipeMigrations(function(err) {
+ assert.isNull(err);
+ dbMigrate('create', 'first migration').on('exit', this.callback);
+ }.bind(this));
+ },
+
+ 'does not cause an error': function(code) {
+ assert.isNull(code);
+ },
+
+ 'will create a new migration directory': function(code) {
+ fs.stat(path.join(__dirname, 'migrations'), function(err, stats) {
+ assert.isNull(err);
+ assert.isTrue(stats.isDirectory());
+ });
+ },
+
+ 'will create a new migration': function(code) {
+ fs.readdir(path.join(__dirname, 'migrations'), function(err, files) {
+ assert.isNull(err);
+ assert.isEqual(files.length, 1);
+ assert.match(file, /first-migration\.js$/);
+
+ var migration = require(path.join(__dirname, 'migrations', file));
+ assert.isNotNull(migration.up);
+ assert.isNotNull(migration.down);
+ });
+ }
+ }
+}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.