-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.js
135 lines (107 loc) · 3.67 KB
/
config.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
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
'use strict';
const inquirer = require('inquirer');
const fs = require('fs');
const pg = require('pg');
async function input(){
try{
let cliInput = await inquirer.prompt([
{
name: 'username',
message: 'What is your PostGres username?'
},
{
name: 'password',
message: 'What is your PostGres password?',
type: 'password'
},
{
name: 'port',
message: 'What is your desired PostGres port? If empty, will default to 5432.' ,
default: '5432'
},
{
name: 'database',
message: 'What is your desired PostGres db? If empty, will default to splunk. Please note, if you use postgres in your project your env must not leave DATABASE_URL available',
default: 'splunk'
},
{
name: 'table',
message: 'Slipup currently only supports one table name: errevents',
default: 'errevents'
}
]);
let config = {
username: cliInput.username,
password: cliInput.password,
port: cliInput.port,
database: cliInput.database,
table: 'errevents'
}
let data = `DATABASE_URL=postgresql://${cliInput.username}:${cliInput.password}@localhost:${cliInput.port}/${cliInput.database}?schema=public`
fs.writeFile('.env', data, (err) => {
if (err) throw err;
});
let jsdata = JSON.stringify(config);
fs.writeFileSync('config.json', jsdata);
console.log('prisma env written')
}catch(e){
console.error(e);
}
};
async function setupPg(){
try{
const config = require('./config.json')
const username = config.username;
const password = config.password;
const port = config.port;
const database = config.database.toLowerCase();
const table = config.table.toLowerCase();
//INITIALIZE DB
//CREATE code copied from https://notathoughtexperiment.me/blog/how-to-do-create-database-dbname-if-not-exists-in-postgres-in-golang/
//POSTGRES doesn't support the CREATE DATABASE IF NOT EXISTS syntax, so we have to:
//1. connect to default postgres db
//2. select all the dbs that exist using the pg_catalog object
//3. query with user provided db name, this query will return a t/f boolean
//4. if false, create the database and proceed, if true, db already exists so just proceed
//5. once db is created/confirmed, reconnect our pg client from default postgres db to correct splunk db
let client = new pg.Client(`postgres://${username}:${password}@localhost:${port}/postgres?schema=public`);
client.connect();
let checkDB = `SELECT EXISTS(SELECT datname FROM pg_catalog.pg_database WHERE datname = '${database}');`
let result = await client.query(checkDB);
let dbresult = result.rows[0];
let checkVal =Object.values(dbresult)[0];
if (checkVal == false) {
let SQLDB = `CREATE DATABASE ${database};`;
await client.query(SQLDB);
console.log(`database ${database} created succesfully`)
}else{
console.log(`database ${database} already exists`)
}
client = new pg.Client(`postgres://${username}:${password}@localhost:${port}/${database}?schema=public`);
client.connect();
//INITIALIZE TABLE
const SQL = `DROP TABLE IF EXISTS errevents;
CREATE TABLE errevents (
id SERIAL PRIMARY KEY,
date DATE,
time TIMESTAMPTZ,
userid VARCHAR(1000),
errortype VARCHAR(1000),
errormessage VARCHAR(1000),
userparam VARCHAR(1000),
usernote VARCHAR(1000),
stack TEXT
);`
await client.query(SQL);
await client.end();
console.log(`table ${table} created succesfully`)
}catch(e){
console.error(e);
}
}
async function execute(){
await input();
await setupPg();
process.exit()
};
execute();