Skip to content

Commit e5a9b15

Browse files
committed
fix(db:create/drop): properly quote database name, fixed #545
1 parent 20d17ee commit e5a9b15

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/commands/database.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exports.handler = async function (args) {
1919

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

3535
break;
3636
case 'db:drop':
37-
await sequelize.query(`DROP DATABASE ${config.database}`, {
37+
await sequelize.query(`DROP DATABASE ${sequelize.queryInterface.quoteIdentifier(config.database)}`, {
3838
type: sequelize.QueryTypes.RAW
3939
}).catch(e => {
4040
helpers.view.error(`Error: ${e.message}`);

test/db/db-create.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ describe(Support.getTestDialectTeaser('db:create'), () => {
4040
}
4141
});
4242
});
43+
44+
it('correctly creates database with hyphen #545', function (done) {
45+
const databaseName = `my_test-db_${_.random(10000, 99999)}`;
46+
prepare(
47+
'db:create',
48+
() => {
49+
this.sequelize.query(`SELECT 1 as exists FROM pg_database WHERE datname = '${databaseName}';`, {
50+
type: this.sequelize.QueryTypes.SELECT
51+
}).then(result => {
52+
expect(result[0].exists).to.eql(1);
53+
done();
54+
});
55+
}, {
56+
config: {
57+
database: databaseName
58+
}
59+
});
60+
});
4361
}
4462

4563
if (Support.dialectIsMySQL()) {
@@ -60,5 +78,23 @@ describe(Support.getTestDialectTeaser('db:create'), () => {
6078
}
6179
});
6280
});
81+
82+
it('correctly creates database with hyphen #545', function (done) {
83+
const databaseName = `my_test-db_${_.random(10000, 99999)}`;
84+
prepare(
85+
'db:create',
86+
() => {
87+
this.sequelize.query(`SELECT IF('${databaseName}' IN(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA), 1, 0) AS found;`, {
88+
type: this.sequelize.QueryTypes.SELECT
89+
}).then(result => {
90+
expect(result[0].found).to.eql(1);
91+
done();
92+
});
93+
}, {
94+
config: {
95+
database: databaseName
96+
}
97+
});
98+
});
6399
}
64100
});

0 commit comments

Comments
 (0)