"fix-fresh-db-and-auto-init-users"

This commit is contained in:
deploy
2026-04-29 12:14:09 +02:00
parent c32cf3073e
commit d8c95b78ec
2 changed files with 25 additions and 3 deletions
+19 -1
View File
@@ -4,7 +4,7 @@ import cors from 'cors';
import helmet from 'helmet';
import path from 'path';
import fs from 'fs';
import { Pool } from 'pg';
import bcrypt from 'bcryptjs';
import { config } from './config';
import { db, testConnection } from './db';
@@ -55,6 +55,23 @@ async function runMigration(): Promise<void> {
console.log('✅ Migration terminée');
}
async function runInitUsers(): Promise<void> {
const users = [
{ name: 'Greg', email: process.env.GREG_EMAIL || 'greg@example.com', password: process.env.GREG_PASSWORD || 'changeme' },
{ name: 'Gaël', email: process.env.GAEL_EMAIL || 'gael@example.com', password: process.env.GAEL_PASSWORD || 'changeme' },
];
for (const user of users) {
const hash = await bcrypt.hash(user.password, 12);
await db.query(
`INSERT INTO users (name, email, password_hash)
VALUES ($1, $2, $3)
ON CONFLICT (email) DO NOTHING`,
[user.name, user.email, hash]
);
console.log(` ✅ Utilisateur prêt : ${user.name} <${user.email}>`);
}
}
async function waitForDb(maxAttempts = 30, delayMs = 2000): Promise<void> {
for (let i = 1; i <= maxAttempts; i++) {
try {
@@ -93,6 +110,7 @@ async function start() {
try {
await waitForDb();
await runMigration();
await runInitUsers();
console.log('✅ Base de données prête');
} catch (err: any) {
console.error('⚠️ Initialisation DB échouée (non bloquant):', err.message);