-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdeployment.go
83 lines (66 loc) · 2.1 KB
/
deployment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package deployment
import (
"context"
"fmt"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type (
// config represents the settings required to create the engine that implements the DeploymentInterface interface.
config struct {
// specifies to skip creating tables and indexes for the Deployment engine
SkipCreation bool
}
// engine represents the deployment functionality that implements the DeploymentInterface interface.
engine struct {
// engine configuration settings used in deployment functions
config *config
ctx context.Context
// gorm.io/gorm database client used in deployment functions
//
// https://pkg.go.dev/gorm.io/gorm#DB
client *gorm.DB
// sirupsen/logrus logger used in deployment functions
//
// https://pkg.go.dev/github.com/sirupsen/logrus#Entry
logger *logrus.Entry
}
)
// New creates and returns a Vela service for integrating with deployments in the database.
//
//nolint:revive // ignore returning unexported engine
func New(opts ...EngineOpt) (*engine, error) {
// create new Deployment engine
e := new(engine)
// create new fields
e.client = new(gorm.DB)
e.config = new(config)
e.logger = new(logrus.Entry)
// apply all provided configuration options
for _, opt := range opts {
err := opt(e)
if err != nil {
return nil, err
}
}
// check if we should skip creating deployment database objects
if e.config.SkipCreation {
e.logger.Warning("skipping creation of deployment table and indexes in the database")
return e, nil
}
// create the deployments table
err := e.CreateDeploymentTable(e.ctx, e.client.Config.Dialector.Name())
if err != nil {
return nil, fmt.Errorf("unable to create %s table: %w", constants.TableDeployment, err)
}
// create the indexes for the deployments table
err = e.CreateDeploymentIndexes(e.ctx)
if err != nil {
return nil, fmt.Errorf("unable to create indexes for %s table: %w", constants.TableDeployment, err)
}
return e, nil
}