@@ -9,16 +9,16 @@ const config = {
9
9
password : ( process . env . CI ? process . env . MYSQL_PASSWORD : '' ) || '' ,
10
10
database : process . env . MYSQL_DATABASE || 'test' ,
11
11
compress : process . env . MYSQL_USE_COMPRESSION ,
12
- port : process . env . MYSQL_PORT || 3306
12
+ port : process . env . MYSQL_PORT || 3306 ,
13
13
} ;
14
14
15
15
if ( process . env . MYSQL_USE_TLS === '1' ) {
16
16
config . ssl = {
17
17
rejectUnauthorized : false ,
18
18
ca : fs . readFileSync (
19
19
path . join ( __dirname , '../test/fixtures/ssl/certs/ca.pem' ) ,
20
- 'utf-8'
21
- )
20
+ 'utf-8' ,
21
+ ) ,
22
22
} ;
23
23
}
24
24
@@ -27,37 +27,58 @@ const configURI = `mysql://${config.user}:${config.password}@${config.host}:${co
27
27
exports . SqlString = require ( 'sqlstring' ) ;
28
28
exports . config = config ;
29
29
30
- exports . waitDatabaseReady = function ( callback ) {
30
+ exports . waitDatabaseReady = function ( callback ) {
31
31
const start = Date . now ( ) ;
32
- const tryConnect = function ( ) {
33
- const conn = exports . createConnection ( { database : 'mysql' , password : process . env . MYSQL_PASSWORD } ) ;
32
+ const timeout = 300000 ; // 5 minutes in milliseconds
33
+
34
+ const tryConnect = function ( ) {
35
+ if ( Date . now ( ) - start > timeout ) {
36
+ console . log ( 'Connection attempt timed out after 5 minutes.' ) ;
37
+ process . exit ( 1 ) ;
38
+ }
39
+
40
+ const conn = exports . createConnection ( {
41
+ database : 'mysql' ,
42
+ password : process . env . MYSQL_PASSWORD ,
43
+ } ) ;
44
+
34
45
conn . once ( 'error' , err => {
35
- if ( err . code !== 'PROTOCOL_CONNECTION_LOST' && err . code !== 'ETIMEDOUT' && err . code !== 'ECONNREFUSED' ) {
46
+ if (
47
+ err . code !== 'PROTOCOL_CONNECTION_LOST' &&
48
+ err . code !== 'ETIMEDOUT' &&
49
+ err . code !== 'ECONNREFUSED'
50
+ ) {
36
51
console . log ( 'Unexpected error waiting for connection' , err ) ;
37
52
process . exit ( - 1 ) ;
38
53
}
54
+
39
55
try {
40
56
conn . close ( ) ;
41
57
} catch ( err ) {
42
58
console . log ( err ) ;
43
59
}
60
+
44
61
console . log ( 'not ready' ) ;
45
62
setTimeout ( tryConnect , 1000 ) ;
46
63
} ) ;
64
+
47
65
conn . once ( 'connect' , ( ) => {
48
66
console . log ( `ready after ${ Date . now ( ) - start } ms!` ) ;
49
67
conn . close ( ) ;
50
68
callback ( ) ;
51
69
} ) ;
52
70
} ;
71
+
53
72
tryConnect ( ) ;
54
73
} ;
55
74
56
- exports . createConnection = function ( args ) {
57
-
75
+ exports . createConnection = function ( args ) {
58
76
const driver = require ( '../index.js' ) ;
59
77
if ( ! args ?. port && process . env . MYSQL_CONNECTION_URL ) {
60
- return driver . createConnection ( { ...args , uri : process . env . MYSQL_CONNECTION_URL } )
78
+ return driver . createConnection ( {
79
+ ...args ,
80
+ uri : process . env . MYSQL_CONNECTION_URL ,
81
+ } ) ;
61
82
}
62
83
63
84
if ( ! args ) {
@@ -91,7 +112,7 @@ exports.createConnection = function(args) {
91
112
return conn ;
92
113
} ;
93
114
94
- exports . getConfig = function ( input ) {
115
+ exports . getConfig = function ( input ) {
95
116
const args = input || { } ;
96
117
const params = {
97
118
host : args . host || config . host ,
@@ -118,10 +139,13 @@ exports.getConfig = function(input) {
118
139
return params ;
119
140
} ;
120
141
121
- exports . createPool = function ( args ) {
142
+ exports . createPool = function ( args ) {
122
143
let driver = require ( '../index.js' ) ;
123
144
if ( ! args ?. port && process . env . MYSQL_CONNECTION_URL ) {
124
- return driver . createPool ( { ...args , uri : process . env . MYSQL_CONNECTION_URL } )
145
+ return driver . createPool ( {
146
+ ...args ,
147
+ uri : process . env . MYSQL_CONNECTION_URL ,
148
+ } ) ;
125
149
}
126
150
127
151
if ( ! args ) {
@@ -135,33 +159,36 @@ exports.createPool = function(args) {
135
159
return driver . createPool ( exports . getConfig ( args ) ) ;
136
160
} ;
137
161
138
- exports . createPoolCluster = function ( args = { } ) {
162
+ exports . createPoolCluster = function ( args = { } ) {
139
163
const driver = require ( '../index.js' ) ;
140
164
if ( ! args ?. port && process . env . MYSQL_CONNECTION_URL ) {
141
- return driver . createPoolCluster ( { ...args , uri : process . env . MYSQL_CONNECTION_URL } )
165
+ return driver . createPoolCluster ( {
166
+ ...args ,
167
+ uri : process . env . MYSQL_CONNECTION_URL ,
168
+ } ) ;
142
169
}
143
- return driver . createPoolCluster ( args )
144
- }
170
+ return driver . createPoolCluster ( args ) ;
171
+ } ;
145
172
146
- exports . createConnectionWithURI = function ( ) {
173
+ exports . createConnectionWithURI = function ( ) {
147
174
const driver = require ( '../index.js' ) ;
148
175
149
176
return driver . createConnection ( { uri : configURI } ) ;
150
177
} ;
151
178
152
- exports . createTemplate = function ( ) {
179
+ exports . createTemplate = function ( ) {
153
180
const jade = require ( 'jade' ) ;
154
181
const template = require ( 'fs' ) . readFileSync (
155
182
`${ __dirname } /template.jade` ,
156
- 'ascii'
183
+ 'ascii' ,
157
184
) ;
158
185
return jade . compile ( template ) ;
159
186
} ;
160
187
161
188
const ClientFlags = require ( '../lib/constants/client.js' ) ;
162
189
163
190
const portfinder = require ( 'portfinder' ) ;
164
- exports . createServer = function ( onListening , handler ) {
191
+ exports . createServer = function ( onListening , handler ) {
165
192
const server = require ( '../index.js' ) . createServer ( ) ;
166
193
server . on ( 'connection' , conn => {
167
194
conn . on ( 'error' , ( ) => {
@@ -178,7 +205,7 @@ exports.createServer = function(onListening, handler) {
178
205
connectionId : 1234 ,
179
206
statusFlags : 2 ,
180
207
characterSet : 8 ,
181
- capabilityFlags : flags
208
+ capabilityFlags : flags ,
182
209
} ) ;
183
210
if ( handler ) {
184
211
handler ( conn ) ;
@@ -190,6 +217,6 @@ exports.createServer = function(onListening, handler) {
190
217
return server ;
191
218
} ;
192
219
193
- exports . useTestDb = function ( ) {
220
+ exports . useTestDb = function ( ) {
194
221
// no-op in my setup, need it for compatibility with node-mysql tests
195
222
} ;
0 commit comments