/
data.go
125 lines (106 loc) · 3.05 KB
/
data.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Copyright 2023 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package data
import (
"context"
"fmt"
"io"
"time"
"database/sql"
"entgo.io/ent/dialect"
entsql "entgo.io/ent/dialect/sql"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/conf"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent/organization"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/uuid"
"github.com/google/wire"
// Load PGX driver
_ "github.com/jackc/pgx/v5/stdlib"
)
// ProviderSet is data providers.
var ProviderSet = wire.NewSet(
NewData,
NewWorkflowRepo,
NewUserRepo,
NewRobotAccountRepo,
NewWorkflowRunRepo,
NewOrganizationRepo,
NewWorkflowContractRepo,
NewCASBackendRepo,
NewOrgMetricsRepo,
NewIntegrationRepo,
NewIntegrationAttachmentRepo,
NewCASMappingRepo,
NewMembershipRepo,
NewOrgInvitation,
NewReferrerRepo,
NewAPITokenRepo,
NewAttestationStateRepo,
)
// Data .
type Data struct {
db *ent.Client
}
// Load DB schema
// NOTE: this is different than running migrations
// this method is used to load the schema into the DB for TESTING!
func (data *Data) SchemaLoad() error {
return data.db.Schema.Create(context.Background())
}
// NewData .
func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) {
if logger == nil {
logger = log.NewStdLogger(io.Discard)
}
log := log.NewHelper(logger)
db, err := initSQLDatabase(c.Database, log)
if err != nil {
log.Errorf("error initialing the DB : %v", err)
return nil, nil, fmt.Errorf("failed to initialized db: %w", err)
}
cleanup := func() {
log.Info("closing the data resources")
if err := db.Close(); err != nil {
log.Error(err)
}
}
return &Data{db: db}, cleanup, nil
}
func initSQLDatabase(c *conf.Data_Database, log *log.Helper) (*ent.Client, error) {
log.Debugf("connecting to db: driver=%s", c.Driver)
db, err := sql.Open(
c.Driver,
c.Source,
)
if err != nil {
return nil, fmt.Errorf("error opening the connection, driver=%s: %w", c.Driver, err)
}
// Create an ent.Driver from `db`.
drv := entsql.OpenDB(dialect.Postgres, db)
client := ent.NewClient(ent.Driver(drv))
// NOTE: We do not run migrations automatically anymore
// Instead we leverage atlas cli to run migrations
return client, nil
}
func toTimePtr(t time.Time) *time.Time {
if t.IsZero() {
return nil
}
return &t
}
func orgScopedQuery(client *ent.Client, orgID uuid.UUID) *ent.OrganizationQuery {
return client.Organization.Query().Where(organization.ID(orgID))
}