github.com/blend/go-sdk@v1.20220411.3/db/dbutil/create_database.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  	"fmt"
    13  
    14  	"github.com/blend/go-sdk/db"
    15  )
    16  
    17  // CreateDatabase creates a database with a given name.
    18  //
    19  // Note: the `name` parameter is passed to the statement directly (not via. a parameter).
    20  // You should use extreme care to not pass user submitted inputs to this function.
    21  func CreateDatabase(ctx context.Context, name string, opts ...db.Option) (err error) {
    22  	var conn *db.Connection
    23  	defer func() {
    24  		err = db.PoolCloseFinalizer(conn, err)
    25  	}()
    26  
    27  	conn, err = OpenManagementConnection(opts...)
    28  	if err != nil {
    29  		return
    30  	}
    31  
    32  	if err = ValidateDatabaseName(name); err != nil {
    33  		return
    34  	}
    35  
    36  	statement := fmt.Sprintf("CREATE DATABASE %s", name)
    37  	_, err = conn.ExecContext(ctx, statement)
    38  	return
    39  }