Skip to content

Fix(#656) Ensure scoped migration created in subdirectory #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/commands/create-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,12 @@ async function executeCreateMigration (internals, config) {
throw new Error("'migrationName' is required.");
}

try {
await createMigrationDir(migrationsDir);
} catch (err) {
log.error('Failed to create migration directory at ', migrationsDir, err);
throw new Error('Failed to create migration directory.');
}

const Migration = require('../template.js');

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

internals.argv.title = folder[folder.length - 2] || folder[0];
internals.argv.title = folder[folder.length - 1] || folder[0];
path = migrationsDir;

if (folder.length > 1) {
Expand All @@ -61,6 +54,14 @@ async function executeCreateMigration (internals, config) {
path += folder[i] + '/';
}
}
internals.argv['migrations-dir'] = path;

try {
await createMigrationDir(path);
} catch (err) {
log.error('Failed to create migration directory at ', migrationsDir, err);
throw new Error('Failed to create migration directory.');
}

let templateType = Migration.TemplateType.DEFAULT_JS;
if (
Expand Down
55 changes: 55 additions & 0 deletions test/integration/create_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,61 @@ lab.experiment('create', function () {
});
}
);
lab.experiment(
'with scoped migration',
function () {
let exitCode;
lab.experiment('without a migration directory', function () {
let exitCode;

lab.before(function (done) {
wipeMigrations(function (err) {
Code.expect(err).to.be.null();
const configOption = path.join('--sql-file');
const db = dbMigrate('create', 'test/first migration', configOption);
// db.stderr.on('data', data => console.log(data.toString()));
// db.stdout.on('data', data => console.log(data.toString()));

db.on('exit', function (code) {
exitCode = code;
done();
});
});
});

lab.test('does not cause an error', function (done) {
Code.expect(exitCode).to.equal(0);
done();
});

lab.test('will create a new migration directory', function (done) {
const stats = fs.statSync(path.join(__dirname, 'migrations/test'));
Code.expect(stats.isDirectory()).to.be.true();
done();
});

lab.test('will create a new migration', function (done) {
const files = fs.readdirSync(path.join(__dirname, 'migrations/test'));
Code.expect(files.length).to.equal(2);
const file = files[0];
Code.expect(file).to.match(/first-migration\.js$/);
done();
});
lab.test('will create a new migration/test/sqls directory', function (done) {
const stats = fs.statSync(path.join(__dirname, 'migrations/test/sqls'));
Code.expect(stats.isDirectory()).to.be.true();
done();
});
lab.test('will create a new migration sql up file', function (done) {
const files = fs.readdirSync(path.join(__dirname, 'migrations/test/sqls'));
Code.expect(files.length).to.equal(2);
const file = files[1];
Code.expect(file).to.match(/first-migration-up\.sql$/);
done();
});
});
}
);

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