Skip to content

umzug.up() unstable and throwing ECONNRESET when used in Kubernetes Pod container #709

@fbele

Description

@fbele

I'm using Umzug migrations in an app that is deployed to the Kubernetes cluster. There is where I first run the migrations upon the start of the container, before I actually start the app. The migration (or it's connection to the database) is at this point is very unstable. It sometimes (rarely though) works properly right after the deployment, the other times I need to restart the Deployment (Pods) several times and eventually it does connect to the database and executes the migrations.

If I execute the migrations from the same app, but from local computer, the migrations always get executed flawlessly, so it cannot be up to the database that this issues arise.

Mostly I have notice that the migrations are working the worst from a container when I set my own database pool configuration, but also with that I could not pinpoint the problem.

Are there any particular considerations when using the Umzug for migrations in a container?

Umzug version: v3.8.2
Storage: SequelizeStorage
Database: Postgres
Sequelize version: v6.37.7
Node version: v22
Dev setup: Typescript, but it's transpiled to vanilla commonjs when deployed
Execution: "node ./src/db/migrate.js"

My code:

// ./src/db/db.ts
import { DataTypes, Transaction } from "sequelize";
import { Model, Sequelize } from "sequelize-typescript";
import { Umzug, SequelizeStorage } from "umzug";
import * as clsHooked from "cls-hooked";

import { config } from "./config";

// Model for instantiating Sequelize
import { Address, Condition, Document, Header, Partner, Position } from "../models";

export const cls = clsHooked.createNamespace("cls-namespace");

// Only do this if using PostgreSQL
if (config.dialect === "postgres") {
  // @ts-expect-error-next-line
  DataTypes.postgres.DECIMAL.parse = function (value: string) {
    return Number(value);
  };
}

Sequelize.useCLS(cls);

const { database, username, password, ...dbConfig } = config;
export const sql = new Sequelize(database, username, password, {
  ...dbConfig,
  models: [Address, Partner, Header, Position, Condition, Document],
});

export const migrator = new Umzug({
  migrations: {
    glob: ["./migrations/*.{ts,js}", { cwd: __dirname }],
  },
  context: sql.getQueryInterface(),
  storage: new SequelizeStorage({
    sequelize: sql,
    modelName: "x_migrator_meta",
  }),
  logger: console,
});
// ./src/db/config.ts
import { SequelizeOptions } from "sequelize-typescript";
// import * as env from "../env";

export const config = {
  username: process.env.DB_USER || "postgres",
  password: process.env.DB_PASSWORD || "supersecret",
  database: process.env.DB_NAME || "postgres",
  host: process.env.DB_HOST || "localhost",
  port: Number(process.env.DB_PORT) || 5432,
  dialect: "postgres" as SequelizeOptions["dialect"],
  dialectOptions: {
    ssl: Boolean(process.env.SSL_MODE === "true" || process.env.SSL_MODE === "require"),
    useNativeUUID: true,
  },
  // pool: {
  //   min: Number(env.get("DB_POOL_MIN_CONNECTIONS")),
  //   max: Number(env.get("DB_POOL_MAX_CONNECTIONS")),
  //   acquire: Number(env.get("DB_POOL_ACQUIRE_IN_MS")),
  //   idle: Number(env.get("DB_POOL_IDLE_IN_MS")),
  // },
  logging: process.env.DB_VERBOSE === "true",
  minifyAliases: true,
  migrationStorageTableSchema: "sales_orders",
  schema: "sales_orders",
};
// ./src/db/migrate.ts
import { migrator } from "./db";
migrator.runAsCLI().finally(() => process.exit(0)); // process.exit(0) because otherwise it exits with code 1 when error occurs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions