-
-
Notifications
You must be signed in to change notification settings - Fork 526
/
database.js
185 lines (158 loc) · 4.7 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import process from 'process';
import { _baseOptions } from '../core/yargs';
import { logMigrator } from '../core/migrator';
import helpers from '../helpers';
import { cloneDeep, defaults, pick } from 'lodash';
import clc from 'cli-color';
const Sequelize = helpers.generic.getSequelize();
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',
}).argv;
exports.handler = async function (args) {
const command = args._[0];
// legacy, gulp used to do this
await helpers.config.init();
const sequelize = getDatabaseLessSequelize();
const config = helpers.config.readConfig();
const options = pick(args, [
'charset',
'collate',
'encoding',
'ctype',
'template',
]);
const queryInterface = sequelize.getQueryInterface();
const queryGenerator =
queryInterface.queryGenerator || queryInterface.QueryGenerator;
const query = getCreateDatabaseQuery(sequelize, config, options);
switch (command) {
case 'db:create':
await sequelize
.query(query, {
type: sequelize.QueryTypes.RAW,
})
.catch((e) => helpers.view.error(e));
helpers.view.log('Database', clc.blueBright(config.database), 'created.');
break;
case 'db:drop':
await sequelize
.query(
`DROP DATABASE IF EXISTS ${queryGenerator.quoteIdentifier(
config.database
)}`,
{
type: sequelize.QueryTypes.RAW,
}
)
.catch((e) => helpers.view.error(e));
helpers.view.log('Database', clc.blueBright(config.database), 'dropped.');
break;
}
process.exit(0);
};
function getCreateDatabaseQuery(sequelize, config, options) {
const queryInterface = sequelize.getQueryInterface();
const queryGenerator =
queryInterface.queryGenerator || queryInterface.QueryGenerator;
switch (config.dialect) {
case 'postgres':
case 'postgres-native':
return (
'CREATE DATABASE ' +
queryGenerator.quoteIdentifier(config.database) +
(options.encoding
? ' ENCODING = ' + queryGenerator.quoteIdentifier(options.encoding)
: '') +
(options.collate
? ' LC_COLLATE = ' + queryGenerator.quoteIdentifier(options.collate)
: '') +
(options.ctype
? ' LC_CTYPE = ' + queryGenerator.quoteIdentifier(options.ctype)
: '') +
(options.template
? ' TEMPLATE = ' + queryGenerator.quoteIdentifier(options.template)
: '')
);
case 'mysql':
return (
'CREATE DATABASE IF NOT EXISTS ' +
queryGenerator.quoteIdentifier(config.database) +
(options.charset
? ' DEFAULT CHARACTER SET ' +
queryGenerator.quoteIdentifier(options.charset)
: '') +
(options.collate
? ' DEFAULT COLLATE ' +
queryGenerator.quoteIdentifier(options.collate)
: '')
);
case 'mssql':
return (
"IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = N'" +
config.database +
"')" +
' BEGIN' +
' CREATE DATABASE ' +
queryGenerator.quoteIdentifier(config.database) +
(options.collate ? ' COLLATE ' + options.collate : '') +
' END;'
);
default:
helpers.view.error(
`Dialect ${config.dialect} does not support db:create / db:drop commands`
);
return (
'CREATE DATABASE ' + queryGenerator.quoteIdentifier(config.database)
);
}
}
function getDatabaseLessSequelize() {
let config = null;
try {
config = helpers.config.readConfig();
} catch (e) {
helpers.view.error(e);
}
config = cloneDeep(config);
config = defaults(config, { logging: logMigrator });
switch (config.dialect) {
case 'postgres':
case 'postgres-native':
config.database = 'postgres';
break;
case 'mysql':
delete config.database;
break;
case 'mssql':
config.database = 'master';
break;
default:
helpers.view.error(
`Dialect ${config.dialect} does not support db:create / db:drop commands`
);
}
try {
return new Sequelize(config);
} catch (e) {
helpers.view.error(e);
}
}