From 059b43ffaa8a5e01c8b26c37b10a581dd65e03bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Mon, 24 Apr 2023 09:59:12 +0200 Subject: [PATCH] chore: migrations test (#3590) ## About the changes This is a raw but working test for up and down migrations. In the first commit (before Andreas' fix: https://github.com/Unleash/unleash/pull/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 --- src/test/e2e/helpers/database-init.ts | 3 +- src/test/e2e/migrator.e2e.test.ts | 40 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/test/e2e/migrator.e2e.test.ts diff --git a/src/test/e2e/helpers/database-init.ts b/src/test/e2e/helpers/database-init.ts index da06c6220f..b4b124dfa3 100644 --- a/src/test/e2e/helpers/database-init.ts +++ b/src/test/e2e/helpers/database-init.ts @@ -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); diff --git a/src/test/e2e/migrator.e2e.test.ts b/src/test/e2e/migrator.e2e.test.ts new file mode 100644 index 0000000000..21cf1ed2ba --- /dev/null +++ b/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 { + 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(); +});