Code review: stop Compose from failing open after a migration error

Code review: stop Compose from failing open after a migration error

●3 ●58
calendar_today ago • schedule2 min read
— Originally published at chriseugenerodriguez.com

Review the failure path first

The dangerous configuration is not one that crashes immediately. It is one that lets the app start after a migration job has merely been launched. At that point, the database can accept connections while the required tables or columns still do not exist.

Start by checking the negative contract: if the migration exits non-zero, the app must remain unstarted. A short depends_on list cannot express that rule. It gives Compose an ordering relationship, but not a requirement that the job completed with exit status 0.

Prerequisites

You need a Compose setup with a database service, a migration command that can run as its own finite container process, and an app service that requires the migrated schema. The migration command should be explicit rather than hidden inside the app startup script.

Make completion the dependency

Choose long-form depends_on on the app service and set the migration condition to service_completed_successfully. Keep the migration as a separate service that depends on the database starting. This represents the real sequence: database starts, migration runs once, then the app may start only after success.

The expected result has two observable branches. A zero exit from the migration satisfies the app dependency and permits app startup. A non-zero exit leaves the dependency unsatisfied, so the app stays blocked. The supplied fixture is useful for guarding this configuration shape: it can reject short syntax and a weaker started-only condition. It does not run Docker Compose or demonstrate an external database engine; that requires an integration check with Compose.

Limitation

Completion gating is not a migration strategy by itself. It does not verify the schema contents, recover a partially applied migration, prevent concurrent deploys, or ensure the app can connect after startup. Inspect the migration container logs and exit status when the app is blocked, and use additional release controls where those risks apply.

Working example

export const proofBoundary =
  'This module validates a Compose configuration shape only. It does not run Docker Compose and cannot prove container exit status or startup ordering; run `docker compose up` with Docker Compose to perform that integration check.';

/**
 * Builds the relevant part of a Compose configuration for a one-shot migration.
 * The migration command is supplied by the application rather than inferred.
 */
export function createMigrationComposeConfig({ migrationCommand }) {
  if (!Array.isArray(migrationCommand) || migrationCommand.length === 0) {
    throw new TypeError('migrationCommand must be a non-empty command array');
  }
  if (!migrationCommand.every((part) => typeof part === 'string' && part.length > 0)) {
    throw new TypeError('every migrationCommand item must be a non-empty string');
  }

  return {
    services: {
      db: {
        image: 'postgres:18',
      },
      migrate: {
        image: 'example/app:latest',
        command: [...migrationCommand],
        depends_on: {
          db: { condition: 'service_started' },
        },
      },
      app: {
        image: 'example/app:latest',
        depends_on: {
          migrate: { condition: 'service_completed_successfully' },
        },
      },
    },
  };
}

/**
 * Checks the application-owned configuration requirement documented by Compose:
 * app must use long-form depends_on and require successful migration completion.
 */
export function validateAppWaitsForMigration(config) {
  const dependency = config?.services?.app?.depends_on?.migrate;

  if (!dependency || Array.isArray(config?.services?.app?.depends_on)) {
    throw new TypeError('services.app.depends_on.migrate must use long syntax');
  }
  if (dependency.condition !== 'service_completed_successfully') {
    throw new RangeError(
      'services.app.depends_on.migrate.condition must be service_completed_successfully',
    );
  }

  return true;
}

Read the full article: https://chriseugenerodriguez.com/blog/wait-for-compose-migrations-to-finish-first

Want more #devops deep dives?

Join 6,134+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

Breaking the AI Data Bottleneck: How Hammerspace's AI Data Platform Eliminates Migration Nightmares

Tom Smithverified - Mar 16

AAIF Hits 10 Projects. Its CTO on What to Adopt Now and the Security Gaps Left Open

Tom Smithverified - Oct 23

Just completed another large-scale WordPress migration — and the client left this

saqib_devmorph - Apr 7

Cisco's Amy Chang: A Model's "Passport" Doesn't Tell You Where It Actually Came From

Tom Smithverified - Aug 27

Stop trusting verdicts your policy no longer supports

VEACESLAV - Dec 1
chevron_left
1.2k Points • 61 Badges
Santa Monica, CA • chriseugenerodriguez.com
45Posts
0Comments
11Connections
10 years fe

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!