Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run migration on dev and test env #24

Open
Tyflomate opened this issue Jun 20, 2024 · 2 comments · May be fixed by #26
Open

Run migration on dev and test env #24

Tyflomate opened this issue Jun 20, 2024 · 2 comments · May be fixed by #26

Comments

@Tyflomate
Copy link

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
Copy link
Owner

@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 acro5piano linked a pull request Jun 23, 2024 that will close this issue
3 tasks
@acro5piano
Copy link
Owner

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

#26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants