go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/dbutil/create_database_if_not_exists.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 13 "go.charczuk.com/sdk/db" 14 ) 15 16 // CreateDatabaseIfNotExists creates a databse if it doesn't exist. 17 // 18 // It will check if a given `serviceEnv` is prodlike, and if the database doesn't exist, and the `serviceEnv` 19 // is prodlike, an `ErrDatabaseDoesntExist` will be returned. 20 // 21 // If a given `serviceEnv` is not prodlike, the database will be created with a management connection. 22 func CreateDatabaseIfNotExists(ctx context.Context, database string, opts ...db.Option) error { 23 exists, err := DatabaseExists(ctx, database, opts...) 24 if err != nil { 25 return err 26 } 27 if !exists { 28 if err = CreateDatabase(ctx, database, opts...); err != nil { 29 return err 30 } 31 } 32 return nil 33 }