MCPcopy Create free account
hub / github.com/bytebase/bytebase / createTasksTx

Method createTasksTx

backend/store/task.go:156–268  ·  view source on GitHub ↗

createTasksTx creates tasks in a transaction.

(ctx context.Context, txn *sql.Tx, creates ...*TaskMessage)

Source from the content-addressed store, hash-verified

154
155// createTasksTx creates tasks in a transaction.
156func (*Store) createTasksTx(ctx context.Context, txn *sql.Tx, creates ...*TaskMessage) ([]*TaskMessage, error) {
157 if len(creates) == 0 {
158 return nil, nil
159 }
160
161 projectID := creates[0].ProjectID
162 baseID, err := nextProjectID(ctx, txn, "task", projectID)
163 if err != nil {
164 return nil, err
165 }
166
167 var (
168 ids []int64
169 projects []string
170 planIDs []int64
171 instances []string
172 databases []*string
173 environments []string
174 types []string
175 payloads [][]byte
176 )
177 for i, create := range creates {
178 if create.Payload == nil {
179 create.Payload = &storepb.Task{}
180 }
181 payload, err := protojson.Marshal(create.Payload)
182 if err != nil {
183 return nil, errors.Wrapf(err, "failed to marshal payload")
184 }
185 ids = append(ids, baseID+int64(i))
186 projects = append(projects, create.ProjectID)
187 planIDs = append(planIDs, create.PlanID)
188 instances = append(instances, create.InstanceID)
189 databases = append(databases, create.DatabaseName)
190 types = append(types, create.Type.String())
191 environments = append(environments, create.Environment)
192 payloads = append(payloads, payload)
193 }
194
195 q := qb.Q().Space(`
196 INSERT INTO task (
197 id,
198 project,
199 plan_id,
200 instance,
201 db_name,
202 environment,
203 type,
204 payload
205 ) SELECT
206 unnest(CAST(? AS BIGINT[])),
207 unnest(CAST(? AS TEXT[])),
208 unnest(CAST(? AS BIGINT[])),
209 unnest(CAST(? AS TEXT[])),
210 unnest(CAST(? AS TEXT[])),
211 unnest(CAST(? AS TEXT[])),
212 unnest(CAST(? AS TEXT[])),
213 unnest(CAST(? AS JSONB[]))

Callers 1

CreateTasksMethod · 0.95

Calls 11

QFunction · 0.92
nextProjectIDFunction · 0.85
SpaceMethod · 0.80
ToSQLMethod · 0.80
QueryContextMethod · 0.80
ScanMethod · 0.80
ErrorfMethod · 0.80
UnmarshalMethod · 0.80
StringMethod · 0.65
CloseMethod · 0.65
NextMethod · 0.45

Tested by

no test coverage detected