Skip to content

Commit 57e736c

Browse files
authored
Merge pull request #655 from AnsonT/fix-create-migration-scope-path
Fix(#656) Ensure scoped migration created in subdirectory
2 parents bd165a5 + 2c9e2c4 commit 57e736c

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

lib/commands/create-migration.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,12 @@ async function executeCreateMigration (internals, config) {
3939
throw new Error("'migrationName' is required.");
4040
}
4141

42-
try {
43-
await createMigrationDir(migrationsDir);
44-
} catch (err) {
45-
log.error('Failed to create migration directory at ', migrationsDir, err);
46-
throw new Error('Failed to create migration directory.');
47-
}
48-
4942
const Migration = require('../template.js');
5043

5144
internals.argv.title = internals.argv._.shift();
5245
folder = internals.argv.title.split('/');
5346

54-
internals.argv.title = folder[folder.length - 2] || folder[0];
47+
internals.argv.title = folder[folder.length - 1] || folder[0];
5548
path = migrationsDir;
5649

5750
if (folder.length > 1) {
@@ -61,6 +54,14 @@ async function executeCreateMigration (internals, config) {
6154
path += folder[i] + '/';
6255
}
6356
}
57+
internals.argv['migrations-dir'] = path;
58+
59+
try {
60+
await createMigrationDir(path);
61+
} catch (err) {
62+
log.error('Failed to create migration directory at ', migrationsDir, err);
63+
throw new Error('Failed to create migration directory.');
64+
}
6465

6566
let templateType = Migration.TemplateType.DEFAULT_JS;
6667
if (

test/integration/create_test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,61 @@ lab.experiment('create', function () {
216216
});
217217
}
218218
);
219+
lab.experiment(
220+
'with scoped migration',
221+
function () {
222+
let exitCode;
223+
lab.experiment('without a migration directory', function () {
224+
let exitCode;
225+
226+
lab.before(function (done) {
227+
wipeMigrations(function (err) {
228+
Code.expect(err).to.be.null();
229+
const configOption = path.join('--sql-file');
230+
const db = dbMigrate('create', 'test/first migration', configOption);
231+
// db.stderr.on('data', data => console.log(data.toString()));
232+
// db.stdout.on('data', data => console.log(data.toString()));
233+
234+
db.on('exit', function (code) {
235+
exitCode = code;
236+
done();
237+
});
238+
});
239+
});
240+
241+
lab.test('does not cause an error', function (done) {
242+
Code.expect(exitCode).to.equal(0);
243+
done();
244+
});
245+
246+
lab.test('will create a new migration directory', function (done) {
247+
const stats = fs.statSync(path.join(__dirname, 'migrations/test'));
248+
Code.expect(stats.isDirectory()).to.be.true();
249+
done();
250+
});
251+
252+
lab.test('will create a new migration', function (done) {
253+
const files = fs.readdirSync(path.join(__dirname, 'migrations/test'));
254+
Code.expect(files.length).to.equal(2);
255+
const file = files[0];
256+
Code.expect(file).to.match(/first-migration\.js$/);
257+
done();
258+
});
259+
lab.test('will create a new migration/test/sqls directory', function (done) {
260+
const stats = fs.statSync(path.join(__dirname, 'migrations/test/sqls'));
261+
Code.expect(stats.isDirectory()).to.be.true();
262+
done();
263+
});
264+
lab.test('will create a new migration sql up file', function (done) {
265+
const files = fs.readdirSync(path.join(__dirname, 'migrations/test/sqls'));
266+
Code.expect(files.length).to.equal(2);
267+
const file = files[1];
268+
Code.expect(file).to.match(/first-migration-up\.sql$/);
269+
done();
270+
});
271+
});
272+
}
273+
);
219274

220275
lab.experiment(
221276
'with coffee-file option set to true as a command parameter',

0 commit comments

Comments
 (0)