Adam
03/22/2022, 7:09 PMv2.27.0
to the latest v3.11.0
in typescript/node, and I’m having issues with Prisma
Keeping the previous connection string when starting a new Integration Test.
Is there something else I should do to force-reset the connection string that prisma is using?
I know the example docs show to call delete
on everything, but that seemed excessive and painstaking. So I simply generate a new schema (postgres
) for each test suite, keeps it nice and clean. I override the Jest
NodeEnvironment
to create a new random connection string. This was working fine in v2.27. Now the below code will fail tests because prisma
will try to be querying a schema from a previous test and it doesn’t exist. I am also using --runInBand
with jest to run tests sequentially
class PrismaTestEnvironment extends NodeEnvironment {
connectionString?: string;
schema?: string;
app?: ExpressType;
constructor(config: Config.ProjectConfig) {
//@ts-ignore
super(config);
// Generate a unique schema identifier for this test context
this.schema = stringGenerator();
//pseudo-code, set random schema on DB_URL ENV
this.global.process.env.DB_URL = `{myConnectionString}/${this.schema}`
process.env.DB_URL = `{myConnectionString}/${this.schema}`
}
public async setup(): Promise<void> {
process.env.DB_URL = this.connectionString;
this.global.process.env.DB_URL = this.connectionString;
forceResetPrisma();
// Run the migrations to ensure our schema has the required structure
try {
const out = await promisifiedExec(
`${prismaBinary} db push --force-reset`
);
<http://this.app|this.app> = await initExpress();
<http://this.global.APP|this.global.APP> = <http://this.app|this.app>;
} catch (e) {
console.error(e);
throw e;
}
return super.setup();
}
public async teardown(): Promise<void> {
const prismaClient = getPrisma();
await prismaClient.$executeRawUnsafe(
`drop schema if exists "${this.schema}" cascade`
);
await prismaClient.$disconnect();
await super.teardown();
}
}
export default PrismaTestEnvironment;
//Prisma Generator File
import { PrismaClient, Prisma } from '@prisma/client';
let db: PrismaClient;
const DefaultLogLevels: Prisma.LogLevel[] = ['info', 'warn', 'error'];
export const getPrisma = (log: Array<Prisma.LogLevel> = DefaultLogLevels) => {
if (!db) {
forceResetPrisma(log);
}
return db;
};
export const hasPrisma = () => !!db;
/**
* Force reset the DB instance using the current @param DB_URL environment var
* @param log
* @returns
*/
export const forceResetPrisma = (
log: Array<Prisma.LogLevel> = DefaultLogLevels
) => {
console.log(`[PRISMA] Generating Client with log levels ${log.join(',')} `);
try {
db = new PrismaClient({
log,
});
} catch (e) {
console.error(e);
throw e;
}
return db;
};
janpio
I'm having issues with Prisma Keeping the previous connection string
mean?