Skip to content

Commit

Permalink
fix(db:create/drop): properly quote database name, fixed #545
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantdhiman committed Sep 23, 2017
1 parent 20d17ee commit e5a9b15
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/commands/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports.handler = async function (args) {

switch (command) {
case 'db:create':
await sequelize.query(`CREATE DATABASE ${config.database}`, {
await sequelize.query(`CREATE DATABASE ${sequelize.queryInterface.quoteIdentifier(config.database)}`, {
type: sequelize.QueryTypes.RAW
}).catch(e => {
helpers.view.error(`Error: ${e.message}`);
Expand All @@ -34,7 +34,7 @@ exports.handler = async function (args) {

break;
case 'db:drop':
await sequelize.query(`DROP DATABASE ${config.database}`, {
await sequelize.query(`DROP DATABASE ${sequelize.queryInterface.quoteIdentifier(config.database)}`, {
type: sequelize.QueryTypes.RAW
}).catch(e => {
helpers.view.error(`Error: ${e.message}`);
Expand Down
36 changes: 36 additions & 0 deletions test/db/db-create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ describe(Support.getTestDialectTeaser('db:create'), () => {
}
});
});

it('correctly creates database with hyphen #545', function (done) {
const databaseName = `my_test-db_${_.random(10000, 99999)}`;
prepare(
'db:create',
() => {
this.sequelize.query(`SELECT 1 as exists FROM pg_database WHERE datname = '${databaseName}';`, {
type: this.sequelize.QueryTypes.SELECT
}).then(result => {
expect(result[0].exists).to.eql(1);
done();
});
}, {
config: {
database: databaseName
}
});
});
}

if (Support.dialectIsMySQL()) {
Expand All @@ -60,5 +78,23 @@ describe(Support.getTestDialectTeaser('db:create'), () => {
}
});
});

it('correctly creates database with hyphen #545', function (done) {
const databaseName = `my_test-db_${_.random(10000, 99999)}`;
prepare(
'db:create',
() => {
this.sequelize.query(`SELECT IF('${databaseName}' IN(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA), 1, 0) AS found;`, {
type: this.sequelize.QueryTypes.SELECT
}).then(result => {
expect(result[0].found).to.eql(1);
done();
});
}, {
config: {
database: databaseName
}
});
});
}
});

0 comments on commit e5a9b15

Please sign in to comment.