Tips to run database migration within a Github Action?

I’m setting up a Github Action to automatically deploy a fly app, and I’d like to run the associated Prisma database migration as part of that. When running locally, I open fly proxy 5432 in one terminal, and DATABASE_URL=...localhost... prisma migrate deploy in another one, but of course Github Actions don’t have multiple terminals, and running a pipeline with fly proxy 5432 & prisma migrate is likely to try to connect before fly proxy has actually finished the wireguard connection. Guessing a sleep duration is my next fallback…

What might be ideal would be to have fly proxy take a subcommand as an argument, spawn it once the wireguard connection is up, and then close the connection once the subcommand terminates.

Is something like that on the roadmap, or have other people found a better way to do this?

1 Like

This is a problem for me too, I’d love to get a background flag like fly proxy --background 5432, that would wait to check the connection and error out if need be, and if connection is successful maybe output the pid as a common utility.

I’m came across this thread whilst looking to do the same – I’m still new to fly so had not thought to do fly proxy 5432 line.

I’ve now written a small deno script which I call from the github action to achieve this

import { readLines } from "https://deno.land/std@0.171.0/io/read_lines.ts";

async function startProcess() {
  const proxyProcess = Deno.run({
    cmd: ["fly", "proxy", "5432", "-a", "db-app-name"],
    stdout: "piped",
  });

  for await (const line of readLines(proxyProcess.stdout)) {
    console.log(line);
    if (line.startsWith("Proxying local port 5432")) {
      return () => {
        proxyProcess.kill("SIGTERM");
        proxyProcess.close();
      };
    } else {
      throw new Error(`Failed to start fly proxy.`);
    }
  }
  throw new Error(`Failed to start fly proxy.`);
}

const endProcess = await startProcess();

const prismaMigrate = Deno.run({
  cmd: ["npm", "exec", "prisma", "migrate", "deploy"],
});
await prismaMigrate.status();
prismaMigrate.close();
endProcess!();

(could be simpler/cleaner – but :man_shrugging:)

and in the github action

      - uses: denoland/setup-deno@v1
      - run: deno run --allow-run script-path.ts

Thanks for the suggestion re fly proxy

(would still be good to have a flag like --background to avoid the need for the deno script – but works well in the meantime)

2 Likes