11import * as fs from 'fs' ;
22import path from 'path' ;
3- import { EntityManager } from 'mikro-orm' ;
3+ import { EntityManager , Transaction } from 'mikro-orm' ;
44import { getDataToSeed } from './seed-data' ;
5+ import { InternalServerErrorException } from '@nestjs/common' ;
56
67export class DatabaseManager {
78 private dropSchemaPath = path . join ( __dirname , '../../data/drop-schema.sql' ) ;
@@ -18,47 +19,34 @@ export class DatabaseManager {
1819 this . createSchemaQuery = fs . readFileSync ( this . createSchemaPath , 'utf-8' ) ;
1920 }
2021
21- private async execute ( em : EntityManager , sql : string ) {
22+ private async execute ( em : EntityManager , sql : string , ctx ?: Transaction ) {
2223 const lines = sql . split ( '\n' ) . filter ( ( i ) => i . trim ( ) ) ;
2324
2425 for ( const line of lines ) {
25- await em . getConnection ( ) . execute ( line ) ;
26+ await em . getConnection ( ) . execute ( line , [ ] , 'run' , ctx ) ;
2627 }
2728 }
2829
29- dropSchema ( em : EntityManager ) : Promise < void > {
30- return this . execute ( em , this . dropSchemaQuery ) ;
30+ dropSchema ( em : EntityManager , ctx ?: Transaction ) : Promise < void > {
31+ return this . execute ( em , this . dropSchemaQuery , ctx ) ;
3132 }
3233
3334 seed ( em : EntityManager ) : Promise < void > {
3435 const meetups = getDataToSeed ( ) ;
3536 return em . persistAndFlush ( meetups ) ;
3637 }
3738
38- createSchema ( em : EntityManager ) : Promise < void > {
39- return this . execute ( em , this . createSchemaQuery ) ;
39+ createSchema ( em : EntityManager , ctx ?: Transaction ) : Promise < void > {
40+ return this . execute ( em , this . createSchemaQuery , ctx ) ;
4041 }
4142
42- async refresh ( em : EntityManager , attempt ?: number ) : Promise < void > {
43- // TODO: add transaction here
44- // for some reason, thi.em.transactional fails with sqlite
45- // Not the best solution, but it seems to work
46- try {
47- await this . dropSchema ( em ) ;
48- await this . createSchema ( em ) ;
49- await this . seed ( em ) ;
50- } catch {
51- if ( attempt < 10 ) {
52- return new Promise ( ( resolve , reject ) => {
53- setTimeout ( ( ) => {
54- this . refresh ( em , attempt + 1 )
55- . then ( resolve )
56- . catch ( reject ) ;
57- } , 100 ) ;
58- } ) ;
59- } else {
60- throw new Error ( 'Error on database refresh' ) ;
61- }
62- }
43+ async refresh ( em : EntityManager ) {
44+ return em . transactional ( async ( _em ) => {
45+ const ctx : Transaction = _em . getTransactionContext ( ) ;
46+ await this . dropSchema ( _em , ctx ) ;
47+ await this . createSchema ( _em , ctx ) ;
48+ await this . seed ( _em ) ;
49+ return true ;
50+ } ) ;
6351 }
6452}
0 commit comments