Files
cim_summary/backend/fix-testing-indexes.js
Jon 185c780486
Some checks failed
CI/CD Pipeline / Backend - Lint & Test (push) Has been cancelled
CI/CD Pipeline / Frontend - Lint & Test (push) Has been cancelled
CI/CD Pipeline / Security Scan (push) Has been cancelled
CI/CD Pipeline / Build Backend (push) Has been cancelled
CI/CD Pipeline / Build Frontend (push) Has been cancelled
CI/CD Pipeline / Integration Tests (push) Has been cancelled
CI/CD Pipeline / Deploy to Staging (push) Has been cancelled
CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Performance Tests (push) Has been cancelled
CI/CD Pipeline / Dependency Updates (push) Has been cancelled
🚀 Update to Claude 3.7 latest and fix LLM processing issues
- Updated Anthropic API to latest version (2024-01-01)
- Set Claude 3.7 Sonnet Latest as primary model
- Removed deprecated Opus 3.5 references
- Fixed LLM response validation and JSON parsing
- Improved error handling and logging
- Updated model configurations and pricing
- Enhanced document processing reliability
- Fixed TypeScript type issues
- Updated environment configuration
2025-08-17 17:31:56 -04:00

172 lines
6.8 KiB
JavaScript

#!/usr/bin/env node
/**
* 🔧 Fix Testing Environment Indexes
*
* This script checks the actual table structure and creates proper indexes.
*/
const { createClient } = require('@supabase/supabase-js');
require('dotenv').config();
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseServiceKey = process.env.SUPABASE_SERVICE_KEY;
if (!supabaseUrl || !supabaseServiceKey) {
console.log('❌ Missing Supabase credentials');
process.exit(1);
}
const supabase = createClient(supabaseUrl, supabaseServiceKey);
async function checkTableStructure() {
console.log('🔍 Checking table structure...\n');
try {
// Check documents table structure
console.log('📋 Documents table structure:');
const { data: docColumns, error: docError } = await supabase.rpc('exec_sql', {
sql: `
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'documents'
ORDER BY ordinal_position;
`
});
if (docError) {
console.log('❌ Error checking documents table:', docError.message);
} else {
console.log('Columns in documents table:');
docColumns.forEach(col => {
console.log(` - ${col.column_name} (${col.data_type}, nullable: ${col.is_nullable})`);
});
}
// Check users table structure
console.log('\n📋 Users table structure:');
const { data: userColumns, error: userError } = await supabase.rpc('exec_sql', {
sql: `
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'users'
ORDER BY ordinal_position;
`
});
if (userError) {
console.log('❌ Error checking users table:', userError.message);
} else {
console.log('Columns in users table:');
userColumns.forEach(col => {
console.log(` - ${col.column_name} (${col.data_type}, nullable: ${col.is_nullable})`);
});
}
// Check processing_jobs table structure
console.log('\n📋 Processing_jobs table structure:');
const { data: jobColumns, error: jobError } = await supabase.rpc('exec_sql', {
sql: `
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'processing_jobs'
ORDER BY ordinal_position;
`
});
if (jobError) {
console.log('❌ Error checking processing_jobs table:', jobError.message);
} else {
console.log('Columns in processing_jobs table:');
jobColumns.forEach(col => {
console.log(` - ${col.column_name} (${col.data_type}, nullable: ${col.is_nullable})`);
});
}
} catch (error) {
console.log('❌ Error checking table structure:', error.message);
}
}
async function createProperIndexes() {
console.log('\n🔄 Creating proper indexes...\n');
try {
// Create indexes based on actual column names
const indexSql = `
-- Documents table indexes
CREATE INDEX IF NOT EXISTS idx_documents_user_id ON documents(user_id);
CREATE INDEX IF NOT EXISTS idx_documents_status ON documents(status);
CREATE INDEX IF NOT EXISTS idx_documents_created_at ON documents(created_at);
CREATE INDEX IF NOT EXISTS idx_documents_original_file_name ON documents(original_file_name);
-- Processing jobs table indexes
CREATE INDEX IF NOT EXISTS idx_processing_jobs_document_id ON processing_jobs(document_id);
CREATE INDEX IF NOT EXISTS idx_processing_jobs_status ON processing_jobs(status);
CREATE INDEX IF NOT EXISTS idx_processing_jobs_user_id ON processing_jobs(user_id);
CREATE INDEX IF NOT EXISTS idx_processing_jobs_created_at ON processing_jobs(created_at);
-- Users table indexes
CREATE INDEX IF NOT EXISTS idx_users_firebase_uid ON users(firebase_uid);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
`;
console.log('📝 Creating indexes...');
const { error: indexError } = await supabase.rpc('exec_sql', { sql: indexSql });
if (indexError) {
console.log('❌ Index creation error:', indexError.message);
// Try creating indexes one by one to identify the problematic one
console.log('\n🔍 Trying to create indexes individually...');
const individualIndexes = [
'CREATE INDEX IF NOT EXISTS idx_documents_user_id ON documents(user_id);',
'CREATE INDEX IF NOT EXISTS idx_documents_status ON documents(status);',
'CREATE INDEX IF NOT EXISTS idx_documents_created_at ON documents(created_at);',
'CREATE INDEX IF NOT EXISTS idx_processing_jobs_document_id ON processing_jobs(document_id);',
'CREATE INDEX IF NOT EXISTS idx_processing_jobs_status ON processing_jobs(status);',
'CREATE INDEX IF NOT EXISTS idx_processing_jobs_user_id ON processing_jobs(user_id);',
'CREATE INDEX IF NOT EXISTS idx_users_firebase_uid ON users(firebase_uid);',
'CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);'
];
for (let i = 0; i < individualIndexes.length; i++) {
const indexSql = individualIndexes[i];
console.log(` Creating index ${i + 1}/${individualIndexes.length}...`);
const { error } = await supabase.rpc('exec_sql', { sql: indexSql });
if (error) {
console.log(` ❌ Index ${i + 1} failed: ${error.message}`);
} else {
console.log(` ✅ Index ${i + 1} created successfully`);
}
}
} else {
console.log('✅ All indexes created successfully');
}
} catch (error) {
console.log('❌ Error creating indexes:', error.message);
}
}
async function main() {
console.log('🔧 Fixing Testing Environment Indexes');
console.log('=====================================\n');
// Step 1: Check table structure
await checkTableStructure();
// Step 2: Create proper indexes
await createProperIndexes();
console.log('\n🎉 Index fixing completed!');
}
main().catch(error => {
console.error('❌ Script failed:', error);
process.exit(1);
});