@@ -9,17 +9,25 @@ import { afterAll } from 'vitest';
99export async function createTestDb ( ) {
1010 const systemClient = knex ( {
1111 client : 'mysql2' ,
12- connection : {
13- host : process . env . MYSQL_HOST ,
14- port : Number . parseInt ( process . env . MYSQL_PORT ! ) ,
15- user : process . env . MYSQL_USER ,
16- password : process . env . MYSQL_PASSWORD ,
17- database : 'mysql' ,
18- timezone : '+00:00' ,
19- } ,
12+ connection : process . env . MYSQL_SOCKET_PATH
13+ ? {
14+ socketPath : process . env . MYSQL_SOCKET_PATH ,
15+ user : process . env . MYSQL_USER ,
16+ password : process . env . MYSQL_PASSWORD ,
17+ database : 'mysql' ,
18+ timezone : '+00:00' ,
19+ }
20+ : {
21+ host : process . env . MYSQL_HOST ,
22+ port : Number . parseInt ( process . env . MYSQL_PORT ! ) ,
23+ user : process . env . MYSQL_USER ,
24+ password : process . env . MYSQL_PASSWORD ,
25+ database : 'mysql' ,
26+ timezone : '+00:00' ,
27+ } ,
2028 } ) ;
2129
22- const dbName = `test_${ randomBytes ( 16 ) . toString ( 'hex' ) } ` ;
30+ const dbName = `${ process . env . MYSQL_DATABASE ?. includes ( 'pr-' ) ? ` ${ process . env . MYSQL_DATABASE . replace ( / - / g , '_' ) } _` : '' } test_${ randomBytes ( 16 ) . toString ( 'hex' ) } ` ;
2331
2432 await systemClient . raw ( `CREATE DATABASE ${ dbName } ` ) ;
2533
@@ -29,23 +37,38 @@ export async function createTestDb() {
2937
3038 // Clone each table structure
3139 for ( const { TABLE_NAME } of tables [ 0 ] ) {
32- await systemClient . raw (
33- `CREATE TABLE ${ dbName } . ${ TABLE_NAME } LIKE ${ process . env . MYSQL_DATABASE } . ${ TABLE_NAME } ` ,
40+ const [ createTableResult ] = await systemClient . raw (
41+ `SHOW CREATE TABLE \` ${ process . env . MYSQL_DATABASE } \`.\` ${ TABLE_NAME } \` ` ,
3442 ) ;
43+ const createTableSql = createTableResult [ 0 ] [ 'Create Table' ]
44+ . replace ( 'CREATE TABLE ' , `CREATE TABLE \`${ dbName } \`.` )
45+ . split ( '\n' )
46+ . filter ( ( line : string ) => ! line . trim ( ) . startsWith ( 'CONSTRAINT' ) )
47+ . join ( '\n' )
48+ . replace ( / , \n \) / , '\n)' ) ; // clean up trailing comma
49+ await systemClient . raw ( createTableSql ) ;
3550 }
3651
3752 await systemClient . destroy ( ) ;
3853
3954 const dbClient = knex ( {
4055 client : 'mysql2' ,
41- connection : {
42- host : process . env . MYSQL_HOST ,
43- port : Number . parseInt ( process . env . MYSQL_PORT ! ) ,
44- user : process . env . MYSQL_USER ,
45- password : process . env . MYSQL_PASSWORD ,
46- database : dbName ,
47- timezone : '+00:00' ,
48- } ,
56+ connection : process . env . MYSQL_SOCKET_PATH
57+ ? {
58+ socketPath : process . env . MYSQL_SOCKET_PATH ,
59+ user : process . env . MYSQL_USER ,
60+ password : process . env . MYSQL_PASSWORD ,
61+ database : dbName ,
62+ timezone : '+00:00' ,
63+ }
64+ : {
65+ host : process . env . MYSQL_HOST ,
66+ port : Number . parseInt ( process . env . MYSQL_PORT ! ) ,
67+ user : process . env . MYSQL_USER ,
68+ password : process . env . MYSQL_PASSWORD ,
69+ database : dbName ,
70+ timezone : '+00:00' ,
71+ } ,
4972 } ) ;
5073
5174 afterAll ( async ( ) => {
0 commit comments