-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate-db-schema.js
More file actions
109 lines (90 loc) · 4.49 KB
/
Copy pathupdate-db-schema.js
File metadata and controls
109 lines (90 loc) · 4.49 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
const { neon } = require('@neondatabase/serverless');
async function updateDatabaseSchema() {
console.log('🔄 Updating Database Schema...\n');
try {
const sql = neon(process.env.DATABASE_URL || 'postgresql://neondb_owner:npg_xqJl5kA7jDBO@ep-empty-silence-a8li7oul-pooler.eastus2.azure.neon.tech/neondb?sslmode=require&channel_binding=require');
console.log('📋 Current reports table structure:');
const currentColumns = await sql`
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'reports'
ORDER BY ordinal_position
`;
currentColumns.forEach(col => {
console.log(` - ${col.column_name}: ${col.data_type} (${col.is_nullable === 'YES' ? 'nullable' : 'not null'})`);
});
// Check what changes are needed
const hasDescription = currentColumns.some(col => col.column_name === 'description');
const hasVerificationStatus = currentColumns.some(col => col.column_name === 'verification_status');
const hasAiAnalysis = currentColumns.some(col => col.column_name === 'ai_analysis');
const hasAmount = currentColumns.some(col => col.column_name === 'amount');
const hasVerificationResult = currentColumns.some(col => col.column_name === 'verification_result');
console.log('\n🔍 Schema analysis:');
console.log(` - Has 'description' column: ${hasDescription}`);
console.log(` - Has 'verification_status' column: ${hasVerificationStatus}`);
console.log(` - Has 'ai_analysis' column: ${hasAiAnalysis}`);
console.log(` - Has 'amount' column: ${hasAmount}`);
console.log(` - Has 'verification_result' column: ${hasVerificationResult}`);
// Update schema
console.log('\n🔧 Updating schema...');
// Add description column if it doesn't exist
if (!hasDescription) {
console.log('➕ Adding description column...');
await sql`ALTER TABLE reports ADD COLUMN description TEXT`;
console.log('✅ Description column added');
}
// Add verification_status column if it doesn't exist
if (!hasVerificationStatus) {
console.log('➕ Adding verification_status column...');
await sql`ALTER TABLE reports ADD COLUMN verification_status VARCHAR(50) DEFAULT 'pending'`;
console.log('✅ Verification_status column added');
}
// Add ai_analysis column if it doesn't exist
if (!hasAiAnalysis) {
console.log('➕ Adding ai_analysis column...');
await sql`ALTER TABLE reports ADD COLUMN ai_analysis JSONB`;
console.log('✅ Ai_analysis column added');
}
// Remove old columns if they exist
if (hasAmount) {
console.log('➖ Removing old amount column...');
await sql`ALTER TABLE reports DROP COLUMN amount`;
console.log('✅ Amount column removed');
}
if (hasVerificationResult) {
console.log('➖ Removing old verification_result column...');
await sql`ALTER TABLE reports DROP COLUMN verification_result`;
console.log('✅ Verification_result column removed');
}
console.log('\n📋 Updated reports table structure:');
const updatedColumns = await sql`
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'reports'
ORDER BY ordinal_position
`;
updatedColumns.forEach(col => {
console.log(` - ${col.column_name}: ${col.data_type} (${col.is_nullable === 'YES' ? 'nullable' : 'not null'})`);
});
// Test the updated schema
console.log('\n🧪 Testing updated schema...');
const testReport = await sql`
INSERT INTO reports (user_id, location, waste_type, description, verification_status, status)
VALUES (1, 'Test Location', 'plastic', 'Test description', 'pending', 'pending')
RETURNING id
`;
console.log('✅ Test report inserted successfully, ID:', testReport[0].id);
// Clean up test data
await sql`DELETE FROM reports WHERE id = ${testReport[0].id}`;
console.log('🧹 Test data cleaned up');
console.log('\n✨ Database schema updated successfully!');
console.log('🎯 You can now submit reports with AI verification.');
} catch (error) {
console.error('❌ Schema update failed:', error.message);
console.log('\n🔧 Troubleshooting:');
console.log('1. Check if you have write permissions to the database');
console.log('2. Verify your Neon database is active');
console.log('3. Check the connection string is correct');
}
}
updateDatabaseSchema();