the-tip-top-backend/scripts/apply-fix-tickets-schema.js
2025-11-17 23:47:54 +01:00

79 lines
2.9 KiB
JavaScript

import { pool } from '../db.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function applyMigration() {
try {
console.log('🔧 Application de la migration: fix-tickets-schema\n');
// Lire le fichier de migration
const migrationPath = path.join(__dirname, '../database/migrations/fix-tickets-schema.sql');
const migration = fs.readFileSync(migrationPath, 'utf8');
console.log('📋 Vérification de l\'état actuel de la base de données...\n');
// Vérifier l'état actuel
const currentState = await pool.query(`
SELECT
COUNT(*) as total,
COUNT(CASE WHEN status IS NULL THEN 1 END) as non_joues,
COUNT(CASE WHEN status = 'PENDING' THEN 1 END) as pending,
COUNT(CASE WHEN status = 'CLAIMED' THEN 1 END) as claimed,
COUNT(CASE WHEN status = 'REJECTED' THEN 1 END) as rejected,
COUNT(CASE WHEN user_id IS NOT NULL THEN 1 END) as with_user
FROM tickets
`);
const stats = currentState.rows[0];
console.log('📊 État actuel:');
console.log(` • Total de tickets: ${stats.total}`);
console.log(` • Tickets non joués (status=NULL): ${stats.non_joues}`);
console.log(` • Tickets PENDING: ${stats.pending}`);
console.log(` • Tickets CLAIMED: ${stats.claimed}`);
console.log(` • Tickets REJECTED: ${stats.rejected}`);
console.log(` • Tickets avec utilisateur: ${stats.with_user}\n`);
console.log('🚀 Application de la migration...\n');
// Appliquer la migration
await pool.query(migration);
console.log('✅ Migration appliquée avec succès!\n');
// Vérifier le nouvel état
const newState = await pool.query(`
SELECT
COUNT(*) as total,
COUNT(CASE WHEN status IS NULL THEN 1 END) as non_joues,
COUNT(CASE WHEN status = 'PENDING' THEN 1 END) as pending,
COUNT(CASE WHEN status = 'CLAIMED' THEN 1 END) as claimed,
COUNT(CASE WHEN status = 'REJECTED' THEN 1 END) as rejected,
COUNT(CASE WHEN user_id IS NOT NULL THEN 1 END) as with_user
FROM tickets
`);
const newStats = newState.rows[0];
console.log('📊 Nouvel état:');
console.log(` • Total de tickets: ${newStats.total}`);
console.log(` • Tickets non joués (status=NULL): ${newStats.non_joues}`);
console.log(` • Tickets PENDING: ${newStats.pending}`);
console.log(` • Tickets CLAIMED: ${newStats.claimed}`);
console.log(` • Tickets REJECTED: ${newStats.rejected}`);
console.log(` • Tickets avec utilisateur: ${newStats.with_user}\n`);
console.log('✨ Migration terminée!\n');
process.exit(0);
} catch (error) {
console.error('❌ Erreur lors de l\'application de la migration:', error.message);
console.error(error.stack);
process.exit(1);
}
}
applyMigration();