-
-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathdatabase.js
More file actions
258 lines (226 loc) · 6.66 KB
/
database.js
File metadata and controls
258 lines (226 loc) · 6.66 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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',
})
.option('force', {
describe:
'Pass force option to dialect with db:drop, PostgreSQL > v13 only',
type: 'boolean',
}).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',
'force',
]);
const createQuery = getCreateDatabaseQuery(sequelize, config, options);
const dropQuery = await getDropDatabaseQuery(sequelize, config, options);
switch (command) {
case 'db:create':
await sequelize
.query(createQuery, {
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(dropQuery, {
type: sequelize.QueryTypes.RAW,
})
.catch((e) => helpers.view.error(e));
helpers.view.log('Database', clc.blueBright(config.database), 'dropped.');
break;
}
process.exit(0);
};
/**
*
* @param sequelize
* @param config
* @param options
* @returns {string}
*/
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)
);
}
}
/**
*
* @param sequelize
* @returns {Promise<String|null>}
*/
async function getPostgresVersion(sequelize) {
try {
const [results] = await sequelize.query(
`SELECT current_setting('server_version_num')::int AS version`,
{
type: sequelize.QueryTypes.RAW,
}
);
return results[0]?.version ?? null;
} catch (e) {
return null;
}
}
/**
*
* @param sequelize
* @param config
* @param options
* @returns {Promise<string>}
*/
async function getDropDatabaseQuery(sequelize, config, options) {
// Adds the force option for WITH(FORCE) to drop a database that has connected users, fallback to default drop if version lower
// for postgres v13 and above see manual https://www.postgresql.org/docs/current/sql-dropdatabase.html
const POSTGRES_FORCE_DROP_MIN_VERSION = 130000;
const queryInterface = sequelize.getQueryInterface();
const queryGenerator =
queryInterface.queryGenerator || queryInterface.QueryGenerator;
switch (config.dialect) {
case 'postgres':
if (options.force) {
const version = await getPostgresVersion(sequelize);
helpers.view.log(
clc.redBright(
`WARNING :: Force dropping database, version check ${
version < POSTGRES_FORCE_DROP_MIN_VERSION
? 'NOT OK!. will ignore --force flag'
: 'OK!. will force drop database regardless of connected users'
} `
)
);
return `DROP DATABASE IF EXISTS ${queryGenerator.quoteIdentifier(
config.database
)} ${
Number(version) >= POSTGRES_FORCE_DROP_MIN_VERSION
? 'WITH (FORCE)'
: ''
} ;`;
} else
return `DROP DATABASE IF EXISTS ${queryGenerator.quoteIdentifier(
config.database
)}`;
default:
return `DROP DATABASE IF EXISTS ${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);
}
}