github.com/blend/go-sdk@v1.20220411.3/db/dbutil/create_database_if_not_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 "github.com/blend/go-sdk/env" 15 "github.com/blend/go-sdk/ex" 16 ) 17 18 // CreateDatabaseIfNotExists creates a databse if it doesn't exist. 19 // 20 // It will check if a given `serviceEnv` is prodlike, and if the database doesn't exist, and the `serviceEnv` 21 // is prodlike, an `ErrDatabaseDoesntExist` will be returned. 22 // 23 // If a given `serviceEnv` is not prodlike, the database will be created with a management connection. 24 func CreateDatabaseIfNotExists(ctx context.Context, serviceEnv, database string, opts ...db.Option) error { 25 exists, err := DatabaseExists(ctx, database, opts...) 26 if err != nil { 27 return err 28 } 29 if !exists { 30 if env.IsProdlike(serviceEnv) { 31 return ex.New(ErrDatabaseDoesntExist, ex.OptMessagef("database: %s", database)) 32 } 33 if err = CreateDatabase(ctx, database, opts...); err != nil { 34 return err 35 } 36 } 37 return nil 38 }