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

const mode = process.env.NODE_ENV || "development";
dotenv.config({ path: path.resolve(process.cwd(), `.env.${mode}`) });

const db = {
    user: process.env.DB_USERNAME,
    pass: process.env.DB_PASSWORD,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    name: process.env.DB_DATABASE,
};
const dbUrl = `mysql://${db.user}:${db.pass}@tcp(host.docker.internal:${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 \"${dbUrl}\"`,
    args,
].join(" ");

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