@@ -11,6 +11,7 @@ import { io } from 'socket.io-client';
11
11
import { Member } from 'src/member/entity/member.entity' ;
12
12
import { MemberService } from 'src/member/service/member.service' ;
13
13
import * as portfinder from 'portfinder' ;
14
+ import { v4 as uuidv4 } from 'uuid' ;
14
15
15
16
export let app : INestApplication ;
16
17
export let githubApiService : GithubApiService ;
@@ -178,7 +179,33 @@ export const listenAppAndSetPortEnv = async (app) => {
178
179
process . env . PORT = port . toString ( ) ;
179
180
} ;
180
181
182
+ const createDatabase = async ( dbName : string ) => {
183
+ const normalDataSource = getDataSource ( ) ;
184
+ await normalDataSource . initialize ( ) ;
185
+ await normalDataSource . query ( `CREATE DATABASE \`${ dbName } \`;` ) ;
186
+ await normalDataSource . destroy ( ) ;
187
+ } ;
188
+
189
+ const dropDatabase = async ( dbName : string ) => {
190
+ const normalDataSource = getDataSource ( ) ;
191
+ await normalDataSource . initialize ( ) ;
192
+ await normalDataSource . query ( `DROP DATABASE IF EXISTS \`${ dbName } \`;` ) ;
193
+ await normalDataSource . destroy ( ) ;
194
+ } ;
195
+
196
+ const getDataSource = ( ) => {
197
+ return new DataSource ( {
198
+ type : 'mysql' ,
199
+ host : process . env . DATABASE_HOST ,
200
+ port : + process . env . DATABASE_PORT ,
201
+ username : process . env . DATABASE_USER ,
202
+ password : process . env . DATABASE_PASSWORD ,
203
+ } ) ;
204
+ } ;
205
+
181
206
beforeAll ( async ( ) => {
207
+ process . env . DATABASE_NAME = uuidv4 ( ) ;
208
+ await createDatabase ( process . env . DATABASE_NAME ) ;
182
209
await appInit ( ) ;
183
210
} ) ;
184
211
@@ -195,4 +222,5 @@ beforeEach(async () => {
195
222
196
223
afterAll ( async ( ) => {
197
224
await app . close ( ) ;
225
+ await dropDatabase ( process . env . DATABASE_NAME ) ;
198
226
} ) ;
0 commit comments