Skip to content
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

feat(db:create): support options on db:create #699

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ services:
ports:
- "54320:5432"
container_name: postgres
command: >
bash -c "sed -i -e 's/# \(zh_TW\|en_US\).UTF-8 UTF-8/\1.UTF-8 UTF-8/' /etc/locale.gen
&& locale-gen
&& docker-entrypoint.sh postgres"

# MySQL
mysql:
Expand Down
49 changes: 41 additions & 8 deletions src/commands/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,37 @@ import { _baseOptions } from '../core/yargs';
import { logMigrator } from '../core/migrator';

import helpers from '../helpers';
import { cloneDeep, defaults } from 'lodash';
import { cloneDeep, defaults, pick } from 'lodash';
import clc from 'cli-color';

const Sequelize = helpers.generic.getSequelize();

exports.builder = yargs => _baseOptions(yargs).help().argv;
exports.builder =
yargs =>
_baseOptions(yargs)
.option('charset', {
describe: 'Pass charset option to dialect, MYSQL only',
type: 'string'
})
.option('collate', {
describe: 'Pass collate option to dialect',
type: 'string'
})
.option('encoding', {
describe: 'Pass encoding option to dialect, PostgreSQL only',
type: 'string'
})
.option('ctype', {
describe: 'Pass ctype option to dialect, PostgreSQL only',
type: 'string'
})
.option('template', {
describe: 'Pass template option to dialect, PostgreSQL only',
type: 'string'
})
.help()
.argv;

exports.handler = async function (args) {
const command = args._[0];

Expand All @@ -19,9 +44,17 @@ exports.handler = async function (args) {

switch (command) {
case 'db:create':
await sequelize.query(`CREATE DATABASE ${sequelize.getQueryInterface().quoteIdentifier(config.database)}`, {
type: sequelize.QueryTypes.RAW
}).catch(e => helpers.view.error(e));
const options = pick(args, [
'charset',
'collate',
'encoding',
'ctype',
'template'
]);

await sequelize.getQueryInterface()
.createDatabase(config.database, options)
.catch(e => helpers.view.error(e));

helpers.view.log(
'Database',
Expand All @@ -31,9 +64,9 @@ exports.handler = async function (args) {

break;
case 'db:drop':
await sequelize.query(`DROP DATABASE ${sequelize.getQueryInterface().quoteIdentifier(config.database)}`, {
type: sequelize.QueryTypes.RAW
}).catch(e => helpers.view.error(e));
await sequelize.getQueryInterface()
.dropDatabase(config.database)
.catch(e => helpers.view.error(e));

helpers.view.log(
'Database',
Expand Down
96 changes: 96 additions & 0 deletions test/db/db-create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,58 @@ describe(Support.getTestDialectTeaser('db:create'), () => {
}
});
});

it('correctly creates database with encoding, collate and template', function (done) {
const databaseName = `my_test-db_${_.random(10000, 99999)}`;
prepare(
'db:create --encoding UTF8 --collate zh_TW.UTF-8 --template template0',
() => {
this.sequelize.query(`SELECT
1 as exists,
pg_encoding_to_char(encoding) as encoding,
datcollate as collate,
datctype as ctype
FROM pg_database WHERE datname = '${databaseName}';`, {
type: this.sequelize.QueryTypes.SELECT
}).then(result => {
expect(result[0].exists).to.eql(1);
expect(result[0].encoding).to.eql('UTF8');
expect(result[0].collate).to.eql('zh_TW.UTF-8');
expect(result[0].ctype).to.eql('en_US.utf8');
done();
});
}, {
config: {
database: databaseName
}
});
});

it('correctly creates database with encoding, collate, ctype and template', function (done) {
const databaseName = `my_test-db_${_.random(10000, 99999)}`;
prepare(
'db:create --encoding UTF8 --collate zh_TW.UTF-8 --ctype zh_TW.UTF-8 --template template0',
() => {
this.sequelize.query(`SELECT
1 as exists,
pg_encoding_to_char(encoding) as encoding,
datcollate as collate,
datctype as ctype
FROM pg_database WHERE datname = '${databaseName}';`, {
type: this.sequelize.QueryTypes.SELECT
}).then(result => {
expect(result[0].exists).to.eql(1);
expect(result[0].encoding).to.eql('UTF8');
expect(result[0].collate).to.eql('zh_TW.UTF-8');
expect(result[0].ctype).to.eql('zh_TW.UTF-8');
done();
});
}, {
config: {
database: databaseName
}
});
});
}

if (Support.dialectIsMySQL()) {
Expand Down Expand Up @@ -96,5 +148,49 @@ describe(Support.getTestDialectTeaser('db:create'), () => {
}
});
});

it('correctly creates database with charset', function (done) {
const databaseName = `my_test-db_${_.random(10000, 99999)}`;
prepare(
'db:create --charset utf8mb4',
() => {
this.sequelize.query(`SELECT
DEFAULT_CHARACTER_SET_NAME as charset,
DEFAULT_COLLATION_NAME as collation
FROM information_schema.SCHEMATA WHERE schema_name = '${databaseName}';`, {
type: this.sequelize.QueryTypes.SELECT
}).then(result => {
expect(result[0].charset).to.eql('utf8mb4');
expect(result[0].collation).to.eql('utf8mb4_general_ci');
done();
});
}, {
config: {
database: databaseName
}
});
});

it('correctly creates database with charset and collation', function (done) {
const databaseName = `my_test-db_${_.random(10000, 99999)}`;
prepare(
'db:create --charset utf8mb4 --collate utf8mb4_unicode_ci',
() => {
this.sequelize.query(`SELECT
DEFAULT_CHARACTER_SET_NAME as charset,
DEFAULT_COLLATION_NAME as collation
FROM information_schema.SCHEMATA WHERE schema_name = '${databaseName}';`, {
type: this.sequelize.QueryTypes.SELECT
}).then(result => {
expect(result[0].charset).to.eql('utf8mb4');
expect(result[0].collation).to.eql('utf8mb4_unicode_ci');
done();
});
}, {
config: {
database: databaseName
}
});
});
}
});