@@ -216,3 +216,126 @@ func Test_MysqlBackend_WorkerName(t *testing.T) {
216216 }
217217 })
218218}
219+
220+ func Test_MysqlBackendWithDB (t * testing.T ) {
221+ if testing .Short () {
222+ t .Skip ()
223+ }
224+
225+ t .Run ("UsesProvidedConnection" , func (t * testing.T ) {
226+ // Create database for test
227+ adminDB , err := sql .Open ("mysql" , fmt .Sprintf ("%s:%s@/?parseTime=true&interpolateParams=true" , testUser , testPassword ))
228+ if err != nil {
229+ t .Fatal (err )
230+ }
231+
232+ dbName := "test_withdb_" + strings .ReplaceAll (uuid .NewString (), "-" , "" )
233+ if _ , err := adminDB .Exec ("CREATE DATABASE " + dbName ); err != nil {
234+ t .Fatal (err )
235+ }
236+ defer func () {
237+ adminDB .Exec ("DROP DATABASE IF EXISTS " + dbName )
238+ adminDB .Close ()
239+ }()
240+
241+ // Create our own connection to the test database
242+ dsn := fmt .Sprintf ("%s:%s@tcp(localhost:3306)/%s?parseTime=true&interpolateParams=true" , testUser , testPassword , dbName )
243+ db , err := sql .Open ("mysql" , dsn )
244+ if err != nil {
245+ t .Fatal (err )
246+ }
247+ defer db .Close ()
248+
249+ // Create backend with existing connection and migration DSN
250+ migrationDSN := dsn + "&multiStatements=true"
251+ backend := NewMysqlBackendWithDB (db ,
252+ WithApplyMigrations (true ),
253+ WithMigrationDSN (migrationDSN ),
254+ )
255+
256+ // Verify the backend uses our connection
257+ if backend .db != db {
258+ t .Error ("Backend should use provided db connection" )
259+ }
260+ if backend .ownsConnection {
261+ t .Error ("Backend should not own the connection" )
262+ }
263+
264+ // Close backend - should NOT close our connection
265+ if err := backend .Close (); err != nil {
266+ t .Fatal (err )
267+ }
268+
269+ // Verify our connection is still usable
270+ if err := db .Ping (); err != nil {
271+ t .Errorf ("Connection should still be open after backend.Close(): %v" , err )
272+ }
273+ })
274+
275+ t .Run ("MigrationsDisabledByDefault" , func (t * testing.T ) {
276+ // Create database for test
277+ adminDB , err := sql .Open ("mysql" , fmt .Sprintf ("%s:%s@/?parseTime=true&interpolateParams=true" , testUser , testPassword ))
278+ if err != nil {
279+ t .Fatal (err )
280+ }
281+
282+ dbName := "test_withdb2_" + strings .ReplaceAll (uuid .NewString (), "-" , "" )
283+ if _ , err := adminDB .Exec ("CREATE DATABASE " + dbName ); err != nil {
284+ t .Fatal (err )
285+ }
286+ defer func () {
287+ adminDB .Exec ("DROP DATABASE IF EXISTS " + dbName )
288+ adminDB .Close ()
289+ }()
290+
291+ dsn := fmt .Sprintf ("%s:%s@tcp(localhost:3306)/%s?parseTime=true&interpolateParams=true" , testUser , testPassword , dbName )
292+ db , err := sql .Open ("mysql" , dsn )
293+ if err != nil {
294+ t .Fatal (err )
295+ }
296+ defer db .Close ()
297+
298+ // Create backend without enabling migrations
299+ backend := NewMysqlBackendWithDB (db )
300+ defer backend .Close ()
301+
302+ // Tables should not exist since migrations weren't applied
303+ _ , err = db .Exec ("SELECT 1 FROM instances LIMIT 1" )
304+ if err == nil {
305+ t .Error ("Expected error because table should not exist" )
306+ }
307+ })
308+
309+ t .Run ("MigrationFailsWithoutDSN" , func (t * testing.T ) {
310+ // Create database for test
311+ adminDB , err := sql .Open ("mysql" , fmt .Sprintf ("%s:%s@/?parseTime=true&interpolateParams=true" , testUser , testPassword ))
312+ if err != nil {
313+ t .Fatal (err )
314+ }
315+
316+ dbName := "test_withdb3_" + strings .ReplaceAll (uuid .NewString (), "-" , "" )
317+ if _ , err := adminDB .Exec ("CREATE DATABASE " + dbName ); err != nil {
318+ t .Fatal (err )
319+ }
320+ defer func () {
321+ adminDB .Exec ("DROP DATABASE IF EXISTS " + dbName )
322+ adminDB .Close ()
323+ }()
324+
325+ dsn := fmt .Sprintf ("%s:%s@tcp(localhost:3306)/%s?parseTime=true&interpolateParams=true" , testUser , testPassword , dbName )
326+ db , err := sql .Open ("mysql" , dsn )
327+ if err != nil {
328+ t .Fatal (err )
329+ }
330+ defer db .Close ()
331+
332+ // Create backend without migration DSN - should panic when trying to migrate
333+ defer func () {
334+ if r := recover (); r == nil {
335+ t .Error ("Expected panic when ApplyMigrations=true without MigrationDSN" )
336+ }
337+ }()
338+
339+ NewMysqlBackendWithDB (db , WithApplyMigrations (true ))
340+ })
341+ }
0 commit comments