Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow schema to be specified for migrations table #17

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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. */

Check warning on line 8 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L8

Added line #L8 was not covered by tests
dangerousAllowDataLoss?: boolean;
/** Log all applied migrations */

Check warning on line 10 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L10

Added line #L10 was not covered by tests
logMigrations?: boolean;
/** Name of the table used for migrations. Defaults to `pgmigrations`. */
migrationsTable?: string;

Check warning on line 13 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L12-L13

Added lines #L12 - L13 were not covered by tests
}

/**
Expand Down Expand Up @@ -45,7 +48,8 @@
password: args.password,
database: args.database,
},
migrationsTable: 'pgmigrations',
migrationsTable: opts?.migrationsTable ?? 'pgmigrations',
schema: typeof args === 'string' ? 'public' : args.schema,

Check warning on line 52 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L51-L52

Added lines #L51 - L52 were not covered by tests
logger: {
info: msg => (opts?.logMigrations === true ? logger.info(msg) : {}),
warn: msg => logger.warn(msg),
Expand All @@ -64,13 +68,17 @@
dir: string,
connectionArgs?: PgConnectionArgs,
opts?: MigrationOptions & {
/** Validates if the database was cleared completely after all `down` migrations are done */

Check warning on line 71 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L71

Added line #L71 was not covered by tests
checkForEmptyData?: boolean;
}
) {
await runMigrations(dir, 'down', connectionArgs, opts);
if (
opts?.checkForEmptyData &&
(await databaseHasData(connectionArgs, { ignoreMigrationTables: true }))
(await databaseHasData(connectionArgs, {
ignoreMigrationTables: true,
migrationsTable: opts.migrationsTable,
}))

Check warning on line 81 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L78-L81

Added lines #L78 - L81 were not covered by tests
) {
throw new Error('Migration down process did not completely remove DB tables');
}
Expand All @@ -88,6 +96,7 @@
connectionArgs?: PgConnectionArgs,
opts?: {
ignoreMigrationTables?: boolean;
migrationsTable?: string;

Check warning on line 99 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L99

Added line #L99 was not covered by tests
}
): Promise<boolean> {
const sql = await connectPostgres({
Expand All @@ -96,12 +105,13 @@
});
try {
const ignoreMigrationTables = opts?.ignoreMigrationTables ?? false;
const tableName = opts?.migrationsTable ?? 'pgmigrations';

Check warning on line 108 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L108

Added line #L108 was not covered by tests
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``}

Check warning on line 114 in src/postgres/migrations.ts

View check run for this annotation

Codecov / codecov/patch

src/postgres/migrations.ts#L114

Added line #L114 was not covered by tests
`;
return result.count > 0 && result[0].count > 0;
} catch (error: any) {
Expand Down
Loading