Repository navigation
Expand file tree
/
Copy pathsetup-test-db.js
More file actions
executable file
·100 lines (81 loc) · 2.55 KB
/
Copy pathsetup-test-db.js
File metadata and controls
executable file
·100 lines (81 loc) · 2.55 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
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',
'mariadb10',
'mariadb11',
];
// The MariaDB versions, on the ports of docker-compose.yml.
const MARIADB_PORTS = { mariadb10: 3307, mariadb11: 3308 };
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: 'mysql2',
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();
}
for (const [name, port] of Object.entries(MARIADB_PORTS)) {
if (!DATABASES.includes(name)) {
continue;
}
const mariadb = await createKnex({
client: 'mariadb',
connection: {
user: 'root',
host: '127.0.0.1',
port,
},
});
await mariadb.raw('DROP DATABASE IF EXISTS objection_test');
await mariadb.raw("DROP USER IF EXISTS objection@'%'");
await mariadb.raw("CREATE USER objection@'%'");
await mariadb.raw("GRANT ALL PRIVILEGES ON *.* TO objection@'%'");
await mariadb.raw('CREATE DATABASE objection_test');
await mariadb.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();