import mysql from 'mysql2/promise';
import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';

dotenv.config();

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function run() {
    try {
        const connection = await mysql.createConnection({
            host: process.env.DB_HOST || 'localhost',
            user: process.env.DB_USER || 'root',
            password: process.env.DB_PASSWORD || '',
            multipleStatements: true
        });

        const sql = fs.readFileSync(path.join(__dirname, 'database.sql'), 'utf8');
        await connection.query(sql);
        console.log('Database schema updated successfully.');
        await connection.end();
    } catch (error) {
        console.error('Error updating database:', error);
        process.exit(1);
    }
}

run();
