refact(cmd): Encapsulate some functionality on Command and Server
This commit extracts 3 separate pieces of functionality into their own function/method: - Extracts the database opening with the intention to have a function that performs all that functionality but returns the created database object rather than setting it to the Server structure. - Extracts database validation logic, to provide a common code path for upcoming functionality. - Expose schema manager on the Command struct as well as providing common functions to acquire shared lock/close.
This commit is contained in:
1 parent
621f8a9ff2
commit
8ab9ffbcac
6 files changed
+117
-51
No files matched your search
@@ -93,7 +93,7 @@ func (b *Server) CreateDevDatabase(ctx context.Context, opt ...Option) error {
|
||||
b.Info["dev database container"] = strings.TrimPrefix(container, "/")
|
||||
}
|
||||
|
||||
if err := b.ConnectToDatabase(ctx, dialect); err != nil {
|
||||
if err := b.OpenAndSetServerDatabase(ctx, dialect); err != nil {
|
||||
if c != nil {
|
||||
err = multierror.Append(err, c())
|
||||
}
|
||||
|
||||
@@ -678,11 +678,26 @@ func (b *Server) RunShutdownFuncs() error {
|
||||
return mErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
func (b *Server) ConnectToDatabase(ctx context.Context, dialect string) error {
|
||||
// OpenAndSetServerDatabase calls OpenDatabase and sets its result *db.DB to the Server's
|
||||
// `Database` field.
|
||||
func (b *Server) OpenAndSetServerDatabase(ctx context.Context, dialect string) error {
|
||||
dbase, err := b.OpenDatabase(ctx, dialect, b.DatabaseUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.Database = dbase
|
||||
return nil
|
||||
}
|
||||
|
||||
// OpenDatabase creates a database connection with the given URL and returns it to the caller.
|
||||
// It supports various configuration options - The values must be set on the Server object
|
||||
// beforehand.
|
||||
func (b *Server) OpenDatabase(ctx context.Context, dialect, url string) (*db.DB, error) {
|
||||
dbType, err := db.StringToDbType(dialect)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err)
|
||||
return nil, fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err)
|
||||
}
|
||||
|
||||
opts := []db.Option{
|
||||
db.WithMaxOpenConnections(b.DatabaseMaxOpenConnections),
|
||||
db.WithMaxIdleConnections(b.DatabaseMaxIdleConnections),
|
||||
@@ -691,12 +706,13 @@ func (b *Server) ConnectToDatabase(ctx context.Context, dialect string) error {
|
||||
if os.Getenv("BOUNDARY_DISABLE_GORM_FORMATTER") == "" {
|
||||
opts = append(opts, db.WithGormFormatter(b.Logger))
|
||||
}
|
||||
dbase, err := db.Open(ctx, dbType, b.DatabaseUrl, opts...)
|
||||
|
||||
dbase, err := db.Open(ctx, dbType, url, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err)
|
||||
return nil, fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err)
|
||||
}
|
||||
b.Database = dbase
|
||||
return nil
|
||||
|
||||
return dbase, nil
|
||||
}
|
||||
|
||||
func (b *Server) CreateGlobalKmsKeys(ctx context.Context) error {
|
||||
|
||||
Reference in new issue
Block a user