github.com/blend/go-sdk@v1.20220411.3/db/dbutil/database_exists.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package dbutil
     9  
    10  import (
    11  	"context"
    12  
    13  	"github.com/blend/go-sdk/db"
    14  )
    15  
    16  // DatabaseExists returns if a database exists or not.
    17  func DatabaseExists(ctx context.Context, name string, opts ...db.Option) (exists bool, err error) {
    18  	var conn *db.Connection
    19  	defer func() {
    20  		err = db.PoolCloseFinalizer(conn, err)
    21  	}()
    22  
    23  	conn, err = OpenManagementConnection(opts...)
    24  	if err != nil {
    25  		return
    26  	}
    27  
    28  	exists, err = conn.QueryContext(ctx, "SELECT 1 FROM pg_database WHERE datname = $1", name).Any()
    29  	return
    30  }