go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/testutil/drop_test_database.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package testutil
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  
    14  	"go.charczuk.com/sdk/db"
    15  	"go.charczuk.com/sdk/db/dbutil"
    16  )
    17  
    18  // DropTestDatabase drops a database.
    19  func DropTestDatabase(ctx context.Context, conn *db.Connection, opts ...db.Option) (err error) {
    20  	var mgmt *db.Connection
    21  	defer func() {
    22  		err = dbutil.PoolCloseFinalizer(mgmt, err)
    23  	}()
    24  
    25  	mgmt, err = dbutil.OpenManagementConnection(opts...)
    26  	if err != nil {
    27  		return
    28  	}
    29  
    30  	_, err = mgmt.ExecContext(ctx, fmt.Sprintf("DROP DATABASE %s", conn.Config.Database))
    31  	return
    32  }