const dotenv = require('dotenv');
const path = require('path');
const { execSync } = require('child_process');

dotenv.config({ path: path.resolve(process.cwd(), `.env`) });

const db = {
    user: process.env.DB_USERNAME,
    pass: process.env.DB_PASSWORD,
    host: process.env.DB_MIGRATION_HOST,
    port: process.env.DB_PORT,
    name: process.env.DB_DATABASE,
};
const dbUrl = `mysql://${db.user}:${db.pass}@tcp(${db.host}:${db.port})/${db.name}`;

const args = process.argv.slice(2).join(" ");
const migrations = path.join(process.cwd(), "migrations");

const cmd = [
    "docker run --rm",
    `-v "${migrations}:/migrations"`,
    "migrate/migrate",
    "-path=/migrations",
    `-database "mysql://${db.user}:${db.pass}@tcp(${db.host}:${db.port})/${db.name}"`, // Use double quotes
    args,
].join(" ");

console.log(cmd);
execSync(cmd, { stdio: "inherit" });
