Re-seeding database between tests #712
-
I am performing browser tests of my application and between each test scenario I need to reset the database. I have been doing this by calling an API mutation exposed by the backend which calls This works for a few tests and then stops working with the backend hanging on each call to the API. When I look at the MySQL server it is connected to I can see multiple connections sitting in the Is this a bug or am I doing something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
After reading Demystifying ‘Waiting for table metadata lock’ in mysql 5.7 For information here is my GraphQL mutation resolver which I use to trigger a database reset between tests: @Resolver()
export class TestsResolver {
private _logger: LoggingService;
private get logger(): LoggingService {
if (this._logger === undefined) {
this._logger = new LoggingService('TestsResolver');
}
return this._logger;
}
@Mutation((_returns) => TestsOperationResponse)
public async seed(@Ctx() ctx: IContext): Promise<ISimpleMutationResponse> {
this.logger.info('Seeding database');
try {
const { dbSession } = ctx.req.context.dataSources;
const mySQLSession = dbSession as MySQLSession;
const dataSource = await mySQLSession.getDataSource();
this.logger.debug('Recreating database');
await dataSource.synchronize(true);
this.logger.debug('Seeding database');
await runSeeders(dataSource);
this.logger.info('Database reset');
return new TestsOperationResponse(
true,
'Database reset successfully'
);
} catch (error) {
return new TestsOperationResponse(false, error.message);
}
}
} |
Beta Was this translation helpful? Give feedback.
After reading Demystifying ‘Waiting for table metadata lock’ in mysql 5.7
I was able to trace the issue to an open transaction with one of my queries triggered by one of my tests. When I fixed my code to commit this transaction I was able to get my tests passing perfectly.
For information here is my GraphQL mutation resolver which I use to trigger a database reset between tests: