-
Notifications
You must be signed in to change notification settings - Fork 142
/
seed.go
192 lines (158 loc) · 4.06 KB
/
seed.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package cli
import (
"context"
"errors"
"fmt"
"log"
"os"
"github.com/jackc/pgx/v5"
"github.com/spf13/cobra"
"github.com/hatchet-dev/hatchet/pkg/config/loader"
"github.com/hatchet-dev/hatchet/pkg/repository"
"github.com/hatchet-dev/hatchet/pkg/repository/prisma/db"
"github.com/hatchet-dev/hatchet/pkg/repository/prisma/sqlchelpers"
)
// seedCmd seeds the database with initial data
var seedCmd = &cobra.Command{
Use: "seed",
Short: "seed create initial data in the database.",
Run: func(cmd *cobra.Command, args []string) {
var err error
configLoader := loader.NewConfigLoader(configDirectory)
err = runSeed(configLoader)
if err != nil {
log.Printf("Fatal: could not run seed command: %v", err)
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(seedCmd)
}
func runSeed(cf *loader.ConfigLoader) error {
// load the config
dc, err := cf.LoadDatabaseConfig()
if err != nil {
panic(err)
}
defer dc.Disconnect() // nolint: errcheck
shouldSeedUser := dc.Seed.AdminEmail != "" && dc.Seed.AdminPassword != ""
var userId string
if shouldSeedUser {
// seed an example user
hashedPw, err := repository.HashPassword(dc.Seed.AdminPassword)
if err != nil {
return err
}
user, err := dc.APIRepository.User().GetUserByEmail(dc.Seed.AdminEmail)
if err != nil {
if errors.Is(err, db.ErrNotFound) {
user, err = dc.APIRepository.User().CreateUser(&repository.CreateUserOpts{
Email: dc.Seed.AdminEmail,
Name: repository.StringPtr(dc.Seed.AdminName),
EmailVerified: repository.BoolPtr(true),
Password: hashedPw,
})
if err != nil {
return err
}
} else {
return err
}
}
userId = user.ID
}
tenant, err := dc.APIRepository.Tenant().GetTenantBySlug("default")
if err != nil {
if errors.Is(err, db.ErrNotFound) {
// seed an example tenant
// initialize a tenant
sqlcTenant, err := dc.APIRepository.Tenant().CreateTenant(&repository.CreateTenantOpts{
ID: &dc.Seed.DefaultTenantID,
Name: dc.Seed.DefaultTenantName,
Slug: dc.Seed.DefaultTenantSlug,
})
if err != nil {
return err
}
tenant, err = dc.APIRepository.Tenant().GetTenantByID(sqlchelpers.UUIDToStr(sqlcTenant.ID))
if err != nil {
return err
}
fmt.Println("created tenant", tenant.ID)
// add the user to the tenant
_, err = dc.APIRepository.Tenant().CreateTenantMember(tenant.ID, &repository.CreateTenantMemberOpts{
Role: "OWNER",
UserId: userId,
})
if err != nil {
return err
}
} else {
return err
}
}
if dc.Seed.IsDevelopment {
err = seedDev(dc.EngineRepository, tenant.ID)
if err != nil {
return err
}
}
return nil
}
func seedDev(repo repository.EngineRepository, tenantId string) error {
_, err := repo.Workflow().GetWorkflowByName(context.Background(), tenantId, "test-workflow")
if err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
return err
}
wf, err := repo.Workflow().CreateNewWorkflow(context.Background(), tenantId, &repository.CreateWorkflowVersionOpts{
Name: "test-workflow",
Description: repository.StringPtr("This is a test workflow."),
Version: repository.StringPtr("v0.1.0"),
EventTriggers: []string{
"user:create",
},
Tags: []repository.CreateWorkflowTagOpts{
{
Name: "Preview",
},
},
Concurrency: &repository.CreateWorkflowConcurrencyOpts{
Action: "test:concurrency",
},
Jobs: []repository.CreateWorkflowJobOpts{
{
Name: "job-name",
Kind: "DEFAULT",
Steps: []repository.CreateWorkflowStepOpts{
{
ReadableId: "echo1",
Action: "echo:echo",
},
{
ReadableId: "echo2",
Action: "echo:echo",
Parents: []string{
"echo1",
},
},
{
ReadableId: "echo3",
Action: "echo:echo",
Parents: []string{
"echo2",
},
},
},
},
},
})
if err != nil {
return err
}
workflowVersionId := sqlchelpers.UUIDToStr(wf.WorkflowVersion.ID)
fmt.Println("created workflow version", workflowVersionId)
}
return nil
}