-
Notifications
You must be signed in to change notification settings - Fork 637
/
setup-test-db.js
executable file
·71 lines (58 loc) · 1.79 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const knex = require('knex');
// DATABASES environment variable can contain a comma separated list
// of databases to setup. Defaults to all databases.
const DATABASES = (process.env.DATABASES && process.env.DATABASES.split(',')) || [
'postgres',
'mysql',
];
async function setup() {
if (DATABASES.includes('postgres')) {
const postgres = await createKnex({
client: 'postgres',
connection: {
user: 'postgres',
host: 'localhost',
database: 'postgres',
},
});
await postgres.raw('DROP DATABASE IF EXISTS objection_test');
await postgres.raw('DROP USER IF EXISTS objection');
await postgres.raw('CREATE USER objection SUPERUSER');
await postgres.raw('CREATE DATABASE objection_test');
await postgres.destroy();
}
if (DATABASES.includes('mysql')) {
const mysql = await createKnex({
client: 'mysql',
connection: {
user: 'root',
host: 'localhost',
},
});
await mysql.raw('DROP DATABASE IF EXISTS objection_test');
await mysql.raw('DROP USER IF EXISTS objection');
await mysql.raw('CREATE USER objection');
await mysql.raw('GRANT ALL PRIVILEGES ON *.* TO objection');
await mysql.raw('CREATE DATABASE objection_test');
await mysql.destroy();
}
}
async function createKnex(config) {
const startTime = new Date();
while (true) {
try {
const knexInstance = knex(config);
await knexInstance.raw('SELECT 1');
return knexInstance;
} catch (err) {
const now = new Date();
if (now.getTime() - startTime.getTime() > 60000) {
process.exit(1);
} else {
console.log(`failed to connect to ${config.client}. Trying again soon`);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}
}
setup();