forked from Vincit/objection.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-test-db.js
executable file
·56 lines (47 loc) · 1.37 KB
/
setup-test-db.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
const knex = require('knex');
// DATABASES environment variable can contain a comma separated list
// of databases to setup.
const DATABASES = (process.env.DATABASES && process.env.DATABASES.split(',')) || [];
const knexes = [];
const commands = [];
if (DATABASES.length === 0 || DATABASES.includes('postgres')) {
const postgres = knex({
client: 'postgres',
connection: {
user: 'postgres',
host: 'localhost',
database: 'postgres',
},
});
knexes.push(postgres);
commands.push(
postgres.raw('DROP DATABASE IF EXISTS objection_test'),
postgres.raw('DROP USER IF EXISTS objection'),
postgres.raw('CREATE USER objection SUPERUSER'),
postgres.raw('CREATE DATABASE objection_test')
);
}
if (DATABASES.length === 0 || DATABASES.includes('mysql')) {
const mysql = knex({
client: 'mysql',
connection: {
user: 'root',
host: 'localhost',
},
});
knexes.push(mysql);
commands.push(
mysql.raw('DROP DATABASE IF EXISTS objection_test'),
mysql.raw('DROP USER IF EXISTS objection'),
mysql.raw('CREATE USER objection'),
mysql.raw('GRANT ALL PRIVILEGES ON *.* TO objection'),
mysql.raw('CREATE DATABASE objection_test')
);
}
commands
.reduce((promise, query) => {
return promise.then(() => query);
}, Promise.resolve())
.then(() => {
return Promise.all(knexes.map((it) => it.destroy()));
});