mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-11 17:27:31 +02:00
Fix critical production issue with missing SAV ticket data and add emergency recovery
Implement an automatic migration script and an emergency API route to resolve missing columns in the SAV production database, ensuring data integrity and system stability. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 31f9bd88-0285-4787-81b3-31ceeea5a08b Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/31f9bd88-0285-4787-81b3-31ceeea5a08b/nCX75tE
This commit is contained in:
1 parent
a4e02d2fd1
commit
27ae1a836f
5 files changed
+305
-28
No files matched your search
@@ -40,11 +40,16 @@ pool.on('error', (err) => {
|
||||
// Run migrations automatically when database is configured
|
||||
(async () => {
|
||||
try {
|
||||
console.log('🔄 Running automatic database migrations...');
|
||||
console.log('🔄 DB: Running automatic database migrations...');
|
||||
await runProductionMigrations();
|
||||
console.log('✅ Database migrations completed');
|
||||
console.log('✅ DB: Database migrations completed successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Database migrations failed:', error);
|
||||
console.error('❌ DB: Database migrations failed with error:', error);
|
||||
console.error('❌ DB: Migration failure details:', {
|
||||
name: error?.name,
|
||||
message: error?.message,
|
||||
code: error?.code
|
||||
});
|
||||
// Don't fail the application startup for migration errors
|
||||
}
|
||||
})();
|
||||
|
||||
@@ -3,14 +3,17 @@ import { neon } from '@neondatabase/serverless';
|
||||
export async function runProductionMigrations() {
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
if (!databaseUrl) {
|
||||
console.error('❌ DATABASE_URL not found for migrations');
|
||||
console.error('❌ MIGRATION: DATABASE_URL not found for migrations');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🔄 MIGRATION: Starting SAV production migrations...');
|
||||
console.log('🔄 MIGRATION: Database URL configured:', databaseUrl.substring(0, 30) + '...');
|
||||
|
||||
const sql = neon(databaseUrl);
|
||||
|
||||
try {
|
||||
console.log('🔄 Checking and running SAV production migrations...');
|
||||
console.log('🔄 MIGRATION: Checking if priority column exists...');
|
||||
|
||||
// Vérifier si la colonne priority existe
|
||||
const checkPriorityColumn = await sql`
|
||||
@@ -18,47 +21,47 @@ export async function runProductionMigrations() {
|
||||
WHERE table_name='sav_tickets' AND column_name='priority'
|
||||
`;
|
||||
|
||||
console.log('🔄 MIGRATION: Priority column check result:', checkPriorityColumn.length);
|
||||
|
||||
if (checkPriorityColumn.length === 0) {
|
||||
console.log('📝 Running SAV table migrations...');
|
||||
console.log('📝 MIGRATION: Priority column missing, adding it now...');
|
||||
|
||||
// Ajouter seulement la colonne priority manquante
|
||||
await sql`
|
||||
ALTER TABLE sav_tickets ADD COLUMN priority varchar(50) NOT NULL DEFAULT 'normale';
|
||||
`;
|
||||
console.log('✅ Colonne priority ajoutée à sav_tickets');
|
||||
console.log('✅ MIGRATION: Priority column added successfully!');
|
||||
|
||||
// Créer les index pour optimiser les performances
|
||||
await sql`
|
||||
CREATE INDEX IF NOT EXISTS idx_sav_tickets_priority ON sav_tickets(priority);
|
||||
`;
|
||||
console.log('✅ Index priority créé');
|
||||
console.log('✅ MIGRATION: Priority index created successfully!');
|
||||
|
||||
console.log('✅ SAV production migrations completed successfully!');
|
||||
|
||||
// Vérification finale
|
||||
const verificationResult = await sql`
|
||||
SELECT
|
||||
'sav_tickets' as table_name,
|
||||
COUNT(*) as record_count,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(CASE WHEN priority IS NOT NULL THEN 1 END) as records_with_priority
|
||||
FROM sav_tickets
|
||||
UNION ALL
|
||||
SELECT
|
||||
'sav_ticket_history' as table_name,
|
||||
COUNT(*) as record_count,
|
||||
NULL as records_with_priority
|
||||
FROM sav_ticket_history;
|
||||
FROM sav_tickets;
|
||||
`;
|
||||
|
||||
console.log('📊 SAV tables status:', verificationResult);
|
||||
console.log('✅ MIGRATION: SAV migration completed successfully!');
|
||||
console.log('📊 MIGRATION: Verification result:', verificationResult);
|
||||
|
||||
} else {
|
||||
console.log('✅ SAV migrations already applied, skipping');
|
||||
console.log('✅ MIGRATION: Priority column already exists, skipping migration');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error running SAV production migrations:', error);
|
||||
console.error('❌ MIGRATION ERROR: Failed to run SAV production migrations:', error);
|
||||
console.error('❌ MIGRATION ERROR: Error details:', {
|
||||
message: error?.message,
|
||||
code: error?.code,
|
||||
stack: error?.stack
|
||||
});
|
||||
// Ne pas faire échouer le démarrage du serveur pour les erreurs de migration
|
||||
// L'application peut continuer à fonctionner même si la migration échoue
|
||||
// Mais logger l'erreur pour diagnostic
|
||||
throw error; // Re-throw pour voir l'erreur dans les logs
|
||||
}
|
||||
}
|
||||
@@ -2683,6 +2683,35 @@ RÉSUMÉ DU SCAN
|
||||
}
|
||||
});
|
||||
|
||||
// Emergency migration route for SAV priority column
|
||||
app.post('/api/admin/emergency-migration', async (req, res) => {
|
||||
try {
|
||||
console.log('🚨 EMERGENCY: Forcing SAV migration execution...');
|
||||
|
||||
// Import migration function
|
||||
const { runProductionMigrations } = await import('./migrations.production.js');
|
||||
|
||||
// Force run the migration
|
||||
await runProductionMigrations();
|
||||
|
||||
console.log('✅ EMERGENCY: Migration executed successfully');
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Emergency migration executed successfully',
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ EMERGENCY: Migration failed:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Emergency migration failed',
|
||||
details: (error as Error).message,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Create server instance
|
||||
const httpServer = createServer(app);
|
||||
|
||||
|
||||
Reference in new issue
Block a user