-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-bigquery.js
More file actions
84 lines (71 loc) · 3 KB
/
test-bigquery.js
File metadata and controls
84 lines (71 loc) · 3 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
// Quick test script to verify BigQuery connection
require('dotenv').config();
const { BigQuery } = require('@google-cloud/bigquery');
async function testConnection() {
console.log('Testing BigQuery connection...\n');
// Check environment variables
console.log('Environment variables:');
console.log('- BIGQUERY_PROJECT_ID:', process.env.BIGQUERY_PROJECT_ID || 'MISSING');
console.log('- BIGQUERY_DATASET:', process.env.BIGQUERY_DATASET || 'MISSING');
console.log('- BIGQUERY_CREDENTIALS:', process.env.BIGQUERY_CREDENTIALS ? 'SET' : 'MISSING');
console.log('');
if (!process.env.BIGQUERY_PROJECT_ID || !process.env.BIGQUERY_CREDENTIALS) {
console.error('❌ Missing required environment variables');
process.exit(1);
}
try {
// Parse credentials
const credentials = JSON.parse(process.env.BIGQUERY_CREDENTIALS);
console.log('✓ Credentials parsed successfully');
console.log(' Project ID from credentials:', credentials.project_id);
console.log(' Client email:', credentials.client_email);
console.log('');
// Initialize BigQuery
const bigquery = new BigQuery({
projectId: process.env.BIGQUERY_PROJECT_ID,
credentials: credentials,
});
console.log('✓ BigQuery client initialized');
console.log('');
// Test query - list datasets
console.log('Testing query: Listing datasets...');
const [datasets] = await bigquery.getDatasets();
console.log('✓ Query successful!');
console.log(' Available datasets:');
datasets.forEach(dataset => console.log(` - ${dataset.id}`));
console.log('');
// Check if our dataset exists
const datasetId = process.env.BIGQUERY_DATASET;
const datasetExists = datasets.some(ds => ds.id === datasetId);
if (datasetExists) {
console.log(`✓ Dataset "${datasetId}" found`);
// List tables in the dataset
const [tables] = await bigquery.dataset(datasetId).getTables();
console.log(' Tables in dataset:');
tables.forEach(table => console.log(` - ${table.id}`));
console.log('');
// Check if agents table exists
const agentsTableExists = tables.some(t => t.id === 'agents');
if (agentsTableExists) {
console.log('✓ "agents" table exists');
// Try to count rows
const query = `SELECT COUNT(*) as count FROM \`${process.env.BIGQUERY_PROJECT_ID}.${datasetId}.agents\``;
const [rows] = await bigquery.query({ query });
console.log(` Current agent count: ${rows[0].count}`);
} else {
console.log('❌ "agents" table NOT found - you need to create it using the schema SQL');
}
} else {
console.log(`❌ Dataset "${datasetId}" NOT found`);
console.log(' You need to create the dataset in BigQuery first');
}
console.log('\n✅ BigQuery connection test completed successfully!');
} catch (error) {
console.error('\n❌ Error:', error.message);
if (error.errors) {
console.error('Details:', error.errors);
}
process.exit(1);
}
}
testConnection();