"fix-fresh-db-and-auto-init-users"
This commit is contained in:
+19
-1
@@ -4,7 +4,7 @@ import cors from 'cors';
|
|||||||
import helmet from 'helmet';
|
import helmet from 'helmet';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { Pool } from 'pg';
|
import bcrypt from 'bcryptjs';
|
||||||
import { config } from './config';
|
import { config } from './config';
|
||||||
import { db, testConnection } from './db';
|
import { db, testConnection } from './db';
|
||||||
|
|
||||||
@@ -55,6 +55,23 @@ async function runMigration(): Promise<void> {
|
|||||||
console.log('✅ Migration terminée');
|
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> {
|
async function waitForDb(maxAttempts = 30, delayMs = 2000): Promise<void> {
|
||||||
for (let i = 1; i <= maxAttempts; i++) {
|
for (let i = 1; i <= maxAttempts; i++) {
|
||||||
try {
|
try {
|
||||||
@@ -93,6 +110,7 @@ async function start() {
|
|||||||
try {
|
try {
|
||||||
await waitForDb();
|
await waitForDb();
|
||||||
await runMigration();
|
await runMigration();
|
||||||
|
await runInitUsers();
|
||||||
console.log('✅ Base de données prête');
|
console.log('✅ Base de données prête');
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('⚠️ Initialisation DB échouée (non bloquant):', err.message);
|
console.error('⚠️ Initialisation DB échouée (non bloquant):', err.message);
|
||||||
|
|||||||
+6
-2
@@ -8,7 +8,7 @@ services:
|
|||||||
POSTGRES_USER: notesfrais
|
POSTGRES_USER: notesfrais
|
||||||
POSTGRES_PASSWORD: 9a3dabd70bb1e09f09962a95bdaffbeacdc56eeee029334b
|
POSTGRES_PASSWORD: 9a3dabd70bb1e09f09962a95bdaffbeacdc56eeee029334b
|
||||||
volumes:
|
volumes:
|
||||||
- pgdata2:/var/lib/postgresql/data
|
- pgdata3:/var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U notesfrais -d notesfrais"]
|
test: ["CMD-SHELL", "pg_isready -U notesfrais -d notesfrais"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
@@ -31,6 +31,10 @@ services:
|
|||||||
APP_SECRET: bbec693632ddd25adeefaddfa64a3e8e1245a97f530cb492f1d3f496ae3a1936
|
APP_SECRET: bbec693632ddd25adeefaddfa64a3e8e1245a97f530cb492f1d3f496ae3a1936
|
||||||
UPLOADS_DIR: /app/uploads
|
UPLOADS_DIR: /app/uploads
|
||||||
FRONTEND_URL: https://frais.domench.fr
|
FRONTEND_URL: https://frais.domench.fr
|
||||||
|
GAEL_EMAIL: waltergael@1dotech.com
|
||||||
|
GAEL_PASSWORD: Changeme123!
|
||||||
|
GREG_EMAIL: greg@domench.fr
|
||||||
|
GREG_PASSWORD: Changeme123!
|
||||||
volumes:
|
volumes:
|
||||||
- uploads:/app/uploads
|
- uploads:/app/uploads
|
||||||
depends_on:
|
depends_on:
|
||||||
@@ -74,5 +78,5 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
pgdata2:
|
pgdata3:
|
||||||
uploads:
|
uploads:
|
||||||
|
|||||||
Reference in New Issue
Block a user