go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/dbutil/create_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 dbutil 9 10 import ( 11 "context" 12 "fmt" 13 14 "go.charczuk.com/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 = 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 statement := fmt.Sprintf("CREATE DATABASE %s", name) 36 _, err = conn.ExecContext(ctx, statement) 37 return 38 }