fix db init

This commit is contained in:
2026-01-29 00:36:20 -05:00
parent 4a6e2c307c
commit b2886cbcb0
3 changed files with 36 additions and 13 deletions

View File

@@ -9,18 +9,42 @@ const __dirname = path.dirname(__filename);
let pool;
export const initDB = async () => {
const dbConfig = {
const dbName = process.env.DB_NAME || 'whats_the_point';
// First connect without specifying database to create it if needed
const baseConfig = {
host: process.env.DB_HOST || 'db',
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'root',
database: process.env.DB_NAME || 'whats_the_point',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
multipleStatements: true
};
// Create pool
// Create temporary pool to create database
const tempPool = mysql.createPool(baseConfig);
try {
const connection = await tempPool.getConnection();
// Create database if it doesn't exist
await connection.query(`CREATE DATABASE IF NOT EXISTS \`${dbName}\``);
console.log(`✅ Database '${dbName}' ready`);
connection.release();
} catch (error) {
console.error('❌ Failed to create database:', error);
throw error;
} finally {
await tempPool.end();
}
// Now connect to the specific database
const dbConfig = {
...baseConfig,
database: dbName
};
pool = mysql.createPool(dbConfig);
// Test connection and run init script