Skip to content

Commit

Permalink
fix: allow schema to be specified for migrations table (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr authored Nov 17, 2023
1 parent 31d0c77 commit aa9c0dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 14 additions & 4 deletions src/postgres/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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),
Expand All @@ -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');
}
Expand All @@ -88,6 +96,7 @@ export async function databaseHasData(
connectionArgs?: PgConnectionArgs,
opts?: {
ignoreMigrationTables?: boolean;
migrationsTable?: string;
}
): Promise<boolean> {
const sql = await connectPostgres({
Expand All @@ -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) {
Expand Down

0 comments on commit aa9c0dd

Please sign in to comment.