-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdb.js
More file actions
74 lines (63 loc) · 2.31 KB
/
db.js
File metadata and controls
74 lines (63 loc) · 2.31 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
const { execSync } = require('child_process');
const DB_CONTAINER_NAME = 'ticket-bot-db';
const DB_USER = 'postgres';
const DB_PASSWORD = 'password';
const DB_NAME = 'ticketbot';
const DB_PORT = '5432';
const usage = () => {
console.log('Usage: node db.js {setup|start|stop|restart|clean|logs}');
console.log(' setup - Create and start the PostgreSQL container');
console.log(' start - Start the existing PostgreSQL container');
console.log(' stop - Stop the PostgreSQL container');
console.log(' restart - Restart the PostgreSQL container');
console.log(' clean - Stop and remove the container and its data');
console.log(' logs - Show database logs');
process.exit(1);
};
const run = (command) => {
try {
execSync(command, { stdio: 'inherit' });
} catch (error) {
// Error is already printed to stderr by inherit
}
};
const action = process.argv[2];
switch (action) {
case 'setup':
console.log('\x1b[34mSetting up PostgreSQL container...\x1b[0m');
run(`docker run --name ${DB_CONTAINER_NAME} \
-e POSTGRES_USER=${DB_USER} \
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
-e POSTGRES_DB=${DB_NAME} \
-p ${DB_PORT}:5432 \
-d postgres:latest`);
console.log('\x1b[32mDatabase is setting up!\x1b[0m');
console.log(`Connection string: \x1b[34mpostgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME}\x1b[0m`);
break;
case 'start':
console.log('\x1b[34mStarting PostgreSQL container...\x1b[0m');
run(`docker start ${DB_CONTAINER_NAME}`);
console.log('\x1b[32mDatabase started.\x1b[0m');
break;
case 'stop':
console.log('\x1b[34mStopping PostgreSQL container...\x1b[0m');
run(`docker stop ${DB_CONTAINER_NAME}`);
console.log('\x1b[32mDatabase stopped.\x1b[0m');
break;
case 'restart':
console.log('\x1b[34mRestarting PostgreSQL container...\x1b[0m');
run(`docker restart ${DB_CONTAINER_NAME}`);
console.log('\x1b[32mDatabase restarted.\x1b[0m');
break;
case 'clean':
console.log('\x1b[34mRemoving PostgreSQL container and data...\x1b[0m');
run(`docker stop ${DB_CONTAINER_NAME}`);
run(`docker rm ${DB_CONTAINER_NAME}`);
console.log('\x1b[32mDatabase container removed.\x1b[0m');
break;
case 'logs':
run(`docker logs -f ${DB_CONTAINER_NAME}`);
break;
default:
usage();
}