From aa9c0dd91acfbbf57ba671df0c0ecaacd0a8ea5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20C=C3=A1rdenas?= Date: Fri, 17 Nov 2023 12:35:42 -0600 Subject: [PATCH] fix: allow schema to be specified for migrations table (#17) --- package.json | 2 +- src/postgres/migrations.ts | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index bd73baf..d4e2f09 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "typings": "./dist/index.d.ts", "scripts": { "build": "rimraf ./dist && tsc --project tsconfig.build.json && copyfiles -u 1 ./src/server-version/*.mjs ./dist", - "test": "jest", + "test": "jest --runInBand", "lint:eslint": "eslint . --ext .js,.jsx,.ts,.tsx -f unix", "lint:prettier": "prettier --check src/**/*.ts", "testenv:run": "docker-compose -f docker/docker-compose.dev.postgres.yml up", diff --git a/src/postgres/migrations.ts b/src/postgres/migrations.ts index abea5a9..56eb599 100644 --- a/src/postgres/migrations.ts +++ b/src/postgres/migrations.ts @@ -5,9 +5,12 @@ import { PgConnectionArgs, connectPostgres, standardizedConnectionArgs } from '. import { isDevEnv, isTestEnv } from '../helpers/values'; export interface MigrationOptions { - // Bypass the NODE_ENV check when performing a "down" migration which irreversibly drops data. + /** Bypass the NODE_ENV check when performing a "down" migration which irreversibly drops data. */ dangerousAllowDataLoss?: boolean; + /** Log all applied migrations */ logMigrations?: boolean; + /** Name of the table used for migrations. Defaults to `pgmigrations`. */ + migrationsTable?: string; } /** @@ -45,7 +48,8 @@ export async function runMigrations( password: args.password, database: args.database, }, - migrationsTable: 'pgmigrations', + migrationsTable: opts?.migrationsTable ?? 'pgmigrations', + schema: typeof args === 'string' ? 'public' : args.schema, logger: { info: msg => (opts?.logMigrations === true ? logger.info(msg) : {}), warn: msg => logger.warn(msg), @@ -64,13 +68,17 @@ export async function cycleMigrations( dir: string, connectionArgs?: PgConnectionArgs, opts?: MigrationOptions & { + /** Validates if the database was cleared completely after all `down` migrations are done */ checkForEmptyData?: boolean; } ) { await runMigrations(dir, 'down', connectionArgs, opts); if ( opts?.checkForEmptyData && - (await databaseHasData(connectionArgs, { ignoreMigrationTables: true })) + (await databaseHasData(connectionArgs, { + ignoreMigrationTables: true, + migrationsTable: opts.migrationsTable, + })) ) { throw new Error('Migration down process did not completely remove DB tables'); } @@ -88,6 +96,7 @@ export async function databaseHasData( connectionArgs?: PgConnectionArgs, opts?: { ignoreMigrationTables?: boolean; + migrationsTable?: string; } ): Promise { const sql = await connectPostgres({ @@ -96,12 +105,13 @@ export async function databaseHasData( }); try { const ignoreMigrationTables = opts?.ignoreMigrationTables ?? false; + const tableName = opts?.migrationsTable ?? 'pgmigrations'; const result = await sql<{ count: number }[]>` SELECT COUNT(*) FROM pg_class c JOIN pg_namespace s ON s.oid = c.relnamespace WHERE s.nspname = ${sql.options.connection.search_path} - ${ignoreMigrationTables ? sql`AND c.relname NOT LIKE 'pgmigrations%'` : sql``} + ${ignoreMigrationTables ? sql`AND c.relname NOT LIKE '${tableName}%'` : sql``} `; return result.count > 0 && result[0].count > 0; } catch (error: any) {