-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-database-connectivity.js
More file actions
executable file
·408 lines (346 loc) · 15.4 KB
/
test-database-connectivity.js
File metadata and controls
executable file
·408 lines (346 loc) · 15.4 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env node
/**
* Database and Redis Connectivity Test
* Tests PostgreSQL and Redis connections for the Subway Lettuce Tracker
*/
const { Client } = require('pg');
const redis = require('redis');
const fs = require('fs');
// Load environment variables
function loadEnvFile() {
try {
const envContent = fs.readFileSync('.env', 'utf8');
const envVars = {};
envContent.split('\n').forEach(line => {
line = line.trim();
if (line && !line.startsWith('#')) {
const [key, ...valueParts] = line.split('=');
if (key && valueParts.length > 0) {
envVars[key.trim()] = valueParts.join('=').trim();
}
}
});
Object.keys(envVars).forEach(key => {
if (!process.env[key]) {
process.env[key] = envVars[key];
}
});
} catch (error) {
console.log('No .env file found or error reading it');
}
}
loadEnvFile();
const colors = {
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
reset: '\x1b[0m',
bold: '\x1b[1m'
};
function log(message, color = colors.reset) {
console.log(`${color}${message}${colors.reset}`);
}
class DatabaseConnectivityTest {
constructor() {
this.issues = [];
this.solutions = [];
}
addIssue(issue, solution) {
this.issues.push(issue);
this.solutions.push(solution);
}
async testPostgreSQL() {
log(`\n${colors.bold}=== Testing PostgreSQL Connection ===${colors.reset}`);
const databaseUrl = process.env.DATABASE_URL;
const postgresPassword = process.env.POSTGRES_PASSWORD;
if (!databaseUrl) {
log(`${colors.red}✗ DATABASE_URL not found in environment${colors.reset}`);
this.addIssue(
'DATABASE_URL environment variable missing',
'Add DATABASE_URL to your .env file'
);
return false;
}
log(`${colors.cyan}Database URL: ${databaseUrl.replace(/:[^:@]*@/, ':***@')}${colors.reset}`);
// Parse the database URL to check components
try {
const url = new URL(databaseUrl);
log(`${colors.blue} Host: ${url.hostname}${colors.reset}`);
log(`${colors.blue} Port: ${url.port}${colors.reset}`);
log(`${colors.blue} Database: ${url.pathname.substring(1)}${colors.reset}`);
log(`${colors.blue} Username: ${url.username}${colors.reset}`);
// Check if using external host (not localhost/postgres)
if (url.hostname !== 'localhost' && url.hostname !== 'postgres') {
log(`${colors.yellow}⚠ Using external PostgreSQL host: ${url.hostname}${colors.reset}`);
log(`${colors.yellow} Make sure this host is accessible from your container${colors.reset}`);
}
} catch (error) {
log(`${colors.red}✗ Invalid DATABASE_URL format: ${error.message}${colors.reset}`);
this.addIssue(
'Invalid DATABASE_URL format',
'Check your DATABASE_URL format: postgresql://user:password@host:port/database'
);
return false;
}
// Test actual connection
const client = new Client({
connectionString: databaseUrl,
connectionTimeoutMillis: 5000,
});
try {
log(`${colors.yellow}Attempting to connect to PostgreSQL...${colors.reset}`);
await client.connect();
// Test basic query
const result = await client.query('SELECT version()');
log(`${colors.green}✓ PostgreSQL connection successful${colors.reset}`);
log(`${colors.blue} Version: ${result.rows[0].version.split(' ').slice(0, 2).join(' ')}${colors.reset}`);
// Test if PostGIS extension is available
try {
const postgisResult = await client.query('SELECT PostGIS_Version()');
log(`${colors.green}✓ PostGIS extension available${colors.reset}`);
log(`${colors.blue} PostGIS Version: ${postgisResult.rows[0].postgis_version}${colors.reset}`);
} catch (postgisError) {
log(`${colors.yellow}⚠ PostGIS extension not found${colors.reset}`);
this.addIssue(
'PostGIS extension not available',
'Install PostGIS extension: CREATE EXTENSION IF NOT EXISTS postgis;'
);
}
// Test if our tables exist
try {
const tablesResult = await client.query(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name IN ('locations', 'ratings')
`);
const tables = tablesResult.rows.map(row => row.table_name);
if (tables.length > 0) {
log(`${colors.green}✓ Application tables found: ${tables.join(', ')}${colors.reset}`);
} else {
log(`${colors.yellow}⚠ Application tables not found${colors.reset}`);
log(`${colors.blue} This is normal for first-time setup${colors.reset}`);
}
} catch (tableError) {
log(`${colors.yellow}⚠ Could not check application tables${colors.reset}`);
}
await client.end();
return true;
} catch (error) {
log(`${colors.red}✗ PostgreSQL connection failed: ${error.message}${colors.reset}`);
if (error.code === 'ECONNREFUSED') {
this.addIssue(
'PostgreSQL connection refused',
'Check if PostgreSQL is running and accessible at the specified host/port'
);
} else if (error.code === 'ENOTFOUND') {
this.addIssue(
'PostgreSQL host not found',
'Check if the hostname in DATABASE_URL is correct and resolvable'
);
} else if (error.message.includes('password authentication failed')) {
this.addIssue(
'PostgreSQL authentication failed',
'Check username and password in DATABASE_URL'
);
} else {
this.addIssue(
`PostgreSQL connection error: ${error.message}`,
'Check your DATABASE_URL configuration and network connectivity'
);
}
try {
await client.end();
} catch (endError) {
// Ignore cleanup errors
}
return false;
}
}
async testRedis() {
log(`\n${colors.bold}=== Testing Redis Connection ===${colors.reset}`);
const redisUrl = process.env.REDIS_URL;
const redisPassword = process.env.REDIS_PASSWORD;
if (!redisUrl) {
log(`${colors.red}✗ REDIS_URL not found in environment${colors.reset}`);
this.addIssue(
'REDIS_URL environment variable missing',
'Add REDIS_URL to your .env file'
);
return false;
}
log(`${colors.cyan}Redis URL: ${redisUrl.replace(/:[^:@]*@/, ':***@')}${colors.reset}`);
// Parse Redis URL
try {
const url = new URL(redisUrl);
log(`${colors.blue} Host: ${url.hostname}${colors.reset}`);
log(`${colors.blue} Port: ${url.port || '6379'}${colors.reset}`);
if (url.password) {
log(`${colors.blue} Password: ***${colors.reset}`);
} else if (redisPassword) {
log(`${colors.blue} Password: *** (from REDIS_PASSWORD)${colors.reset}`);
} else {
log(`${colors.yellow} No password configured${colors.reset}`);
}
} catch (error) {
log(`${colors.red}✗ Invalid REDIS_URL format: ${error.message}${colors.reset}`);
this.addIssue(
'Invalid REDIS_URL format',
'Check your REDIS_URL format: redis://[:password@]host:port'
);
return false;
}
// Test actual connection
let client;
try {
log(`${colors.yellow}Attempting to connect to Redis...${colors.reset}`);
const redisConfig = {
url: redisUrl,
socket: {
connectTimeout: 5000,
commandTimeout: 5000,
}
};
// Add password if specified separately
if (redisPassword && !redisUrl.includes('@')) {
redisConfig.password = redisPassword;
}
client = redis.createClient(redisConfig);
client.on('error', (err) => {
log(`${colors.red}Redis client error: ${err.message}${colors.reset}`);
});
await client.connect();
// Test basic operations
await client.set('test_key', 'test_value');
const value = await client.get('test_key');
await client.del('test_key');
if (value === 'test_value') {
log(`${colors.green}✓ Redis connection successful${colors.reset}`);
// Get Redis info
const info = await client.info('server');
const version = info.match(/redis_version:([^\r\n]+)/);
if (version) {
log(`${colors.blue} Version: ${version[1]}${colors.reset}`);
}
await client.quit();
return true;
} else {
throw new Error('Redis test operation failed');
}
} catch (error) {
log(`${colors.red}✗ Redis connection failed: ${error.message}${colors.reset}`);
if (error.code === 'ECONNREFUSED') {
this.addIssue(
'Redis connection refused',
'Check if Redis is running and accessible at the specified host/port'
);
} else if (error.code === 'ENOTFOUND') {
this.addIssue(
'Redis host not found',
'Check if the hostname in REDIS_URL is correct and resolvable'
);
} else if (error.message.includes('WRONGPASS') || error.message.includes('NOAUTH')) {
this.addIssue(
'Redis authentication failed',
'Check password in REDIS_URL or REDIS_PASSWORD'
);
} else {
this.addIssue(
`Redis connection error: ${error.message}`,
'Check your REDIS_URL configuration and network connectivity'
);
}
if (client) {
try {
await client.quit();
} catch (quitError) {
// Ignore cleanup errors
}
}
return false;
}
}
async checkEnvironmentConfiguration() {
log(`\n${colors.bold}=== Environment Configuration Analysis ===${colors.reset}`);
const requiredVars = [
'DATABASE_URL',
'REDIS_URL',
'VITE_GOOGLE_MAPS_API_KEY'
];
const optionalVars = [
'POSTGRES_PASSWORD',
'REDIS_PASSWORD',
'VITE_API_URL',
'NODE_ENV'
];
log(`${colors.cyan}Required Variables:${colors.reset}`);
requiredVars.forEach(varName => {
const value = process.env[varName];
if (value) {
if (varName.includes('PASSWORD') || varName.includes('API_KEY')) {
log(` ${colors.green}✓ ${varName}: ${value.substring(0, 10)}...${colors.reset}`);
} else {
log(` ${colors.green}✓ ${varName}: ${value}${colors.reset}`);
}
} else {
log(` ${colors.red}✗ ${varName}: NOT SET${colors.reset}`);
}
});
log(`\n${colors.cyan}Optional Variables:${colors.reset}`);
optionalVars.forEach(varName => {
const value = process.env[varName];
if (value) {
if (varName.includes('PASSWORD')) {
log(` ${colors.blue}• ${varName}: ***${colors.reset}`);
} else {
log(` ${colors.blue}• ${varName}: ${value}${colors.reset}`);
}
} else {
log(` ${colors.yellow}• ${varName}: not set${colors.reset}`);
}
});
}
generateReport() {
log(`\n${colors.bold}=== Connectivity Test Summary ===${colors.reset}`);
if (this.issues.length === 0) {
log(`${colors.green}✓ All connectivity tests passed!${colors.reset}`);
log(`${colors.green}✓ PostgreSQL and Redis are properly configured${colors.reset}`);
log(`${colors.blue}Your database connections should work correctly.${colors.reset}`);
} else {
log(`${colors.red}Found ${this.issues.length} connectivity issue(s):${colors.reset}`);
this.issues.forEach((issue, index) => {
log(`\n${colors.red}Issue ${index + 1}: ${issue}${colors.reset}`);
log(`${colors.green}Solution: ${this.solutions[index]}${colors.reset}`);
});
}
log(`\n${colors.bold}=== Next Steps ===${colors.reset}`);
if (this.issues.length > 0) {
log(`${colors.yellow}1. Fix the connectivity issues above${colors.reset}`);
log(`${colors.yellow}2. Re-run this test: node test-database-connectivity.js${colors.reset}`);
log(`${colors.yellow}3. Once connections work, test your location functionality${colors.reset}`);
} else {
log(`${colors.green}1. Your database connections are working${colors.reset}`);
log(`${colors.green}2. Test your AIO container: ./test-aio-local.sh${colors.reset}`);
log(`${colors.green}3. If location issues persist, check Google Maps API configuration${colors.reset}`);
}
}
async runTests() {
log(`${colors.bold}${colors.blue}Database Connectivity Test for Subway Lettuce Tracker${colors.reset}`);
log(`${colors.blue}====================================================${colors.reset}`);
await this.checkEnvironmentConfiguration();
const postgresOk = await this.testPostgreSQL();
const redisOk = await this.testRedis();
this.generateReport();
return postgresOk && redisOk;
}
}
// Run the tests
const tester = new DatabaseConnectivityTest();
tester.runTests().then(success => {
process.exit(success ? 0 : 1);
}).catch(error => {
console.error(`${colors.red}Test runner error: ${error.message}${colors.reset}`);
process.exit(1);
});