flyway-has-changed: Stop Starting a JVM Every Time a Container Restarts

We have released flyway-has-changed, a small open source command that tells a container whether its database still needs Flyway to run, in milliseconds and without starting Flyway's JVM.

We have released flyway-has-changed, a small command line tool that answers one question before a container starts Flyway: does this database actually need migrating? If it does not, Flyway is skipped entirely. The tool is open source and available on npm, and it now runs in front of Flyway on our own PrimeCRM sites.

The problem it solves

Flyway is excellent at applying migrations, and we have no intention of replacing it. However, Flyway is a Java program, so every flyway migrate starts a JVM. When each container runs Flyway in its entrypoint, restarting many containers at once, after a deployment or a server reload, starts just as many JVMs at the same moment. On a modest server that can exhaust the available memory, even though almost every one of those restarts has nothing to migrate.

We encountered this ourselves while applying server updates to mitigate known CVEs. The full server restart that followed put every site container through its start-up migration check at the same time. Each PrimeCRM site container checks its database when it starts, and several also maintain a second migration history for site-specific tables. Every restart therefore paid the cost of a JVM, only to learn that nothing had changed.

Why we kept it lean

When we first looked at the problem, our instinct was to replace Flyway altogether, or to use AI to build a Node.js version of it. Both options came with a hidden cost. A migration engine is a large body of code, and we would have taken on the burden of maintaining all of it, along with the security responsibility of keeping pace with the upstream Java project. We have all seen how that tends to end: a replacement that starts out close to the original gradually falls behind it, until there is a significant gap in features, fixes and security patches that nobody has time to close.

So we settled on a different principle: keep it lean. What we actually needed was not a new migration engine. We needed to know whether we had to run Java at all. That is a much smaller question, and it can be answered by a much smaller tool, one that leaves the hard work of applying migrations to Flyway.

What it does

flyway-has-changed is a lightweight Node.js tool. It reads the Flyway history table from your database, reads your migration files, compares the two, and exits with a code that your entrypoint can act on:

  • 0 — the database matches the migration files exactly. Skip Flyway.
  • 10 — something differs, such as a pending migration, an edited file, a failed migration or an out-of-order file. Run Flyway.
  • 20 — the tool could not decide, for example because the database was unreachable. Run Flyway.

Because exit codes 10 and 20 both mean “run Flyway”, the tool never makes a deployment less safe than running Flyway every time. It never writes to the database and never applies a migration. Flyway remains the engine; flyway-has-changed only decides whether that engine needs to start.

Using it

Install it as a regular dependency, rather than a development dependency, so that production images include it:

npm install flyway-has-changed

Then run it before Flyway in your container entrypoint:

if ./node_modules/.bin/flyway-has-changed; then
  echo "Database up to date, skipping Flyway"
else
  flyway migrate
fi

If the package is missing, or anything unexpected happens, the else branch runs and behaviour is exactly as it was before.

Nothing new to configure

The tool reads the settings that Flyway already reads: the FLYWAY_URL, FLYWAY_USER, FLYWAY_PASSWORD and FLYWAY_LOCATIONS environment variables, or your existing flyway.conf. It also expands ${DB_HOST}-style placeholders in the configuration file from the environment, as Flyway does, so production configurations written that way work without changes. If a variable is not set, the placeholder is left as written and the tool reports that it could not decide, so Flyway runs as normal.

A site with a second migration history is simply checked once per history, using a different configuration file each time.

How fast is it?

In our own environment, the check on a site container took about 60 milliseconds, with no JVM involved. A whole set of containers can now restart without each one starting Flyway. When a migration is genuinely pending, the check reports it and Flyway runs as usual.

Supported databases

PostgreSQL is supported today and is selected automatically from the JDBC URL in your Flyway settings. Support for other databases can be added as small drivers. The tool requires Node.js 18 or later and is released under the MIT licence.

Try it

flyway-has-changed is available on npm as flyway-has-changed. If you run Flyway in containers and your restarts are heavier than they need to be, we would welcome your feedback, including which database you would like to see supported next.

If you are considering a custom software project and want a partner who works as part of your team, please get in touch at [email protected].

Adrian Sweeney, CEO & Founder, Libertas Software Research Ltd

↑ Back to Knowledge Hub
Should We Build It? The Question We Ask Before Writing Any Code ›