fix db init
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user