mirror of
https://github.com/Unleash/unleash.git
synced 2025-10-04 11:17:02 +02:00
We're migrating to ESM, which will allow us to import the latest versions of our dependencies. Co-Authored-By: Christopher Kolstad <chriswk@getunleash.io>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import dbMigrateShared from 'db-migrate-shared';
|
|
const { log } = dbMigrateShared;
|
|
import dbMigrate from 'db-migrate';
|
|
const { getInstance } = dbMigrate;
|
|
import type { IUnleashConfig } from './lib/types/option.js';
|
|
import { secondsToMilliseconds } from 'date-fns';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
log.setLogLevel('error');
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
async function noDatabaseUrl<T>(fn: () => Promise<T>): Promise<T> {
|
|
// unset DATABASE_URL so it doesn't take presedence over the provided db config
|
|
const dbUrlEnv = process.env.DATABASE_URL;
|
|
delete process.env.DATABASE_URL;
|
|
const result = fn();
|
|
process.env.DATABASE_URL = dbUrlEnv;
|
|
return result;
|
|
}
|
|
export async function migrateDb(
|
|
{ db }: Pick<IUnleashConfig, 'db'>,
|
|
stopAt?: string,
|
|
): Promise<void> {
|
|
return noDatabaseUrl(async () => {
|
|
const custom = {
|
|
...db,
|
|
connectionTimeoutMillis: secondsToMilliseconds(10),
|
|
};
|
|
|
|
// disable Intellij/WebStorm from setting verbose CLI argument to db-migrator
|
|
process.argv = process.argv.filter((it) => !it.includes('--verbose'));
|
|
const dbm = getInstance(true, {
|
|
cwd: __dirname,
|
|
config: { custom },
|
|
env: 'custom',
|
|
});
|
|
|
|
return dbm.up(stopAt);
|
|
});
|
|
}
|
|
|
|
// This exists to ease testing
|
|
export async function resetDb({ db }: IUnleashConfig): Promise<void> {
|
|
return noDatabaseUrl(async () => {
|
|
const custom = {
|
|
...db,
|
|
connectionTimeoutMillis: secondsToMilliseconds(10),
|
|
};
|
|
|
|
const dbm = getInstance(true, {
|
|
cwd: __dirname,
|
|
config: { custom },
|
|
env: 'custom',
|
|
});
|
|
|
|
return dbm.reset();
|
|
});
|
|
}
|