fix db init
This commit is contained in:
@@ -1,6 +0,0 @@
|
|||||||
# Backend
|
|
||||||
PORT=4000
|
|
||||||
JWT_SECRET=your_jwt_secret
|
|
||||||
DATABASE_URL=postgres://postgres:postgres@db:5432/whats_the_point
|
|
||||||
# Frontend
|
|
||||||
VITE_API_URL=http://localhost:4000
|
|
||||||
@@ -9,18 +9,42 @@ const __dirname = path.dirname(__filename);
|
|||||||
let pool;
|
let pool;
|
||||||
|
|
||||||
export const initDB = async () => {
|
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',
|
host: process.env.DB_HOST || 'db',
|
||||||
user: process.env.DB_USER || 'root',
|
user: process.env.DB_USER || 'root',
|
||||||
password: process.env.DB_PASSWORD || 'root',
|
password: process.env.DB_PASSWORD || 'root',
|
||||||
database: process.env.DB_NAME || 'whats_the_point',
|
|
||||||
waitForConnections: true,
|
waitForConnections: true,
|
||||||
connectionLimit: 10,
|
connectionLimit: 10,
|
||||||
queueLimit: 0,
|
queueLimit: 0,
|
||||||
multipleStatements: true
|
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);
|
pool = mysql.createPool(dbConfig);
|
||||||
|
|
||||||
// Test connection and run init script
|
// Test connection and run init script
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
version: '3.8'
|
version: '3.8'
|
||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: linuxserver/mariadb
|
image: mariadb:10.11
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
MYSQL_ROOT_PASSWORD: rootpassword
|
MYSQL_ROOT_PASSWORD: rootpassword
|
||||||
MYSQL_DATABASE: whats_the_point
|
MYSQL_DATABASE: whats_the_point
|
||||||
TZ: America/Toronto
|
|
||||||
volumes:
|
volumes:
|
||||||
- db_data:/config
|
- db_data:/var/lib/mysql
|
||||||
ports:
|
ports:
|
||||||
- "3306:3306"
|
- "3306:3306"
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
web:
|
web:
|
||||||
build: .
|
build: .
|
||||||
@@ -24,7 +28,8 @@ services:
|
|||||||
DB_NAME: whats_the_point
|
DB_NAME: whats_the_point
|
||||||
TMDB_API_KEY: your_tmdb_api_key_here
|
TMDB_API_KEY: your_tmdb_api_key_here
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
db:
|
||||||
|
condition: service_healthy
|
||||||
ports:
|
ports:
|
||||||
- "4000:4000"
|
- "4000:4000"
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
Reference in New Issue
Block a user