Skip to main content

Migration

TypeORM DB migration

When the PROD DB needs migration

This is little unconventional but this is what i have been doing

The entire idea run a docker container with POSTGRES db which is similar to current PROD DB then run typeorm migration generation script to create new migration and then connect to the db thorough Kubectl proxy command then run typeorm migrate command with prod db config in typeorm configs

Step by Step Process

Run Docker

Run all migration to make that PG Db similar to Prod db then create migration script


npm run typeorm -- -d ./apps/omni-api/typeorm.config.ts migration:generate ./apps/omni-api/src/migrations/AddClientPrefandProxyConfigs

Port forward prod db kubectl to local


kubectl port-forward svc/postgres-rw -n omni 5432:5432

then run migration after updating tsconfig to now forwarded prod db


import { DataSource } from 'typeorm';

const dataSource = new DataSource({
// NOTE: This configuration is only used when generating migrations. Our target DB
// is Postgres, so this needs to point to a local postgres database to generate
// the appropriate SQL migrations.
type: 'postgres',
host: 'localhost', // Port-forwarded host
port: 5432,
database: 'omni', // Your production database name
username: 'omni', // Production username
password: '4PWeuycgqfhs4oWUUm4fZoSCdtqNHvcJAAKdXESs3vjNsDBtiF59kUl9mX1Y5wr5',
// entities: ['./apps/omni-api/src/**/*.entity.ts'],
synchronize: false,
migrations: ['./apps/omni-api/src/migrations/*.ts']
});

export default dataSource;


npx typeorm-ts-node-commonjs migration:run -d apps/omni-api/typeorm.config.ts

When you run Migration you need comment out entities


kubectl patch cluster postgres -n omni --type merge --patch '{\"spec\":{\"instances\":3}}'

Test Run Migration


npx typeorm-ts-node-commonjs migration:run -d apps/omni-api/typeorm.config.ts --transaction