acro5piano / kysely-migration-cli

A thin library to create a migration script using Kysely
https://www.npmjs.com/package/kysely-migration-cli
MIT License
66 stars 3 forks source link

Run migration on dev and test env #24

Open Tyflomate opened 3 months ago

Tyflomate commented 3 months ago

I would like to migrate both dev and test database at the same time but I can't figure out a way to make it work. Here is my take:

import * as path from 'path';
import { promises as fs } from 'fs';
import { Pool } from 'pg';
import { Kysely, Migrator, PostgresDialect, FileMigrationProvider } from 'kysely';
import { run } from 'kysely-migration-cli';

const migrationFolder = path.join(__dirname, './migrations');

if (!!process.env.DATABASE_URL) {
  const db = new Kysely<any>({
    dialect: new PostgresDialect({
      pool: new Pool({
        connectionString: process.env.DATABASE_URL,
      }),
    }),
  });

  const migrator = new Migrator({
    db,
    provider: new FileMigrationProvider({
      fs,
      path,
      migrationFolder,
    }),
  });

  console.log('Running development migrations...');
  run(db, migrator, migrationFolder);
  db.destroy();
} else {
  console.log('No dev database url. Skipping...');
}

if (!!process.env.TEST_DATABASE_URL) {
  const test_db = new Kysely<any>({
    dialect: new PostgresDialect({
      pool: new Pool({
        connectionString: process.env.TEST_DATABASE_URL,
      }),
    }),
  });
  const test_migrator = new Migrator({
    db: test_db,
    provider: new FileMigrationProvider({
      fs,
      path,
      migrationFolder,
    }),
  });

  console.log('Running test migrations...');
  run(test_db, test_migrator, migrationFolder);
  test_db.destroy();
} else {
  console.log('No test database. Skipping...');
}

It does run twice but always on the first database, not the second. Is there a way to do this ? Thanks !

acro5piano commented 3 months ago

@Tyflomate Thanks for reporting.

It seems that commander.js re-use the default instance. We need to refactor the code to create commander instance every time!

Related:

acro5piano commented 3 months ago

To resolve this issue, we need to change our architecture entirely - meaning v1.

https://github.com/acro5piano/kysely-migration-cli/pull/26