Skip to content

Commit

Permalink
chore: migrations test (#3590)
Browse files Browse the repository at this point in the history
## About the changes
This is a raw but working test for up and down migrations. 

In the first commit (before Andreas' fix:
#3589) [it catches the migration
errors we
found](https://github.com/Unleash/unleash/actions/runs/4766567308/jobs/8473744864?pr=3590#step:5:605).
After merging from main the test go green, proving that it can catch
these issues earlier
  • Loading branch information
gastonfournier committed Apr 24, 2023
1 parent eb07599 commit 059b43f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/test/e2e/helpers/database-init.ts
Expand Up @@ -99,8 +99,7 @@ export default async function init(

await db.raw(`DROP SCHEMA IF EXISTS ${config.db.schema} CASCADE`);
await db.raw(`CREATE SCHEMA IF NOT EXISTS ${config.db.schema}`);
// @ts-expect-error
await migrateDb({ ...config, databaseSchema: config.db.schema });
await migrateDb(config);
await db.destroy();
const testDb = createDb(config);
const stores = await createStores(config, testDb);
Expand Down
40 changes: 40 additions & 0 deletions src/test/e2e/migrator.e2e.test.ts
@@ -0,0 +1,40 @@
import { getDbConfig } from './helpers/database-config';
import { createTestConfig } from '../config/test-config';
import { getInstance } from 'db-migrate';
import { Client } from 'pg';
import { IDBOption } from 'lib/types';

async function initSchema(db: IDBOption): Promise<void> {
const client = new Client(db);
await client.connect();
await client.query(`DROP SCHEMA IF EXISTS ${db.schema} CASCADE`);
await client.query(`CREATE SCHEMA IF NOT EXISTS ${db.schema}`);
await client.end();
}

test('Up & down migrations work', async () => {
const config = createTestConfig({
db: {
...getDbConfig(),
pool: { min: 1, max: 4 },
schema: 'up_n_down_migrations_test',
ssl: false,
},
});

await initSchema(config.db);

const e2e = {
...config.db,
connectionTimeoutMillis: 2000,
};

const dbm = getInstance(true, {
cwd: `${__dirname}/../../`, // relative to src/test/e2e
config: { e2e },
env: 'e2e',
});

await dbm.up();
await dbm.reset();
});

0 comments on commit 059b43f

Please sign in to comment.