(ctx context.Context, projectID, environment string, vars map[string]string, userID string)
| 3193 | } |
| 3194 | |
| 3195 | func (c *connection) UpsertProjectVariable(ctx context.Context, projectID, environment string, vars map[string]string, userID string) ([]*database.ProjectVariable, error) { |
| 3196 | var userIDPtr *string |
| 3197 | if userID != "" { |
| 3198 | userIDPtr = &userID |
| 3199 | } |
| 3200 | |
| 3201 | query := `INSERT INTO project_variables (project_id, environment, name, value, value_encryption_key_id, updated_by_user_id, updated_on) |
| 3202 | VALUES %s |
| 3203 | ON CONFLICT (project_id, environment, lower(name)) DO UPDATE SET |
| 3204 | value = EXCLUDED.value, |
| 3205 | value_encryption_key_id = EXCLUDED.value_encryption_key_id, |
| 3206 | updated_by_user_id = EXCLUDED.updated_by_user_id, |
| 3207 | updated_on = now() RETURNING *` |
| 3208 | |
| 3209 | var placeholders strings.Builder |
| 3210 | args := []any{projectID, environment, userIDPtr} |
| 3211 | i := 3 |
| 3212 | for key, value := range vars { |
| 3213 | // Encrypt the variables |
| 3214 | encryptedValue, valueEncryptionKeyID, err := c.encrypt([]byte(value)) |
| 3215 | if err != nil { |
| 3216 | return nil, err |
| 3217 | } |
| 3218 | |
| 3219 | if valueEncryptionKeyID != "" { |
| 3220 | value = base64.StdEncoding.EncodeToString(encryptedValue) |
| 3221 | } |
| 3222 | args = append(args, key, value, valueEncryptionKeyID) |
| 3223 | if placeholders.Len() > 0 { |
| 3224 | placeholders.WriteString(", ") |
| 3225 | } |
| 3226 | fmt.Fprintf(&placeholders, "($1, $2, $%d, $%d, $%d, $3, now())", i+1, i+2, i+3) // project_id, environment, name, value, value_encryption_key_id, updated_by_user_id, updated_on |
| 3227 | i += 3 |
| 3228 | } |
| 3229 | |
| 3230 | var res []*database.ProjectVariable |
| 3231 | err := c.getDB(ctx).SelectContext(ctx, &res, fmt.Sprintf(query, placeholders.String()), args...) |
| 3232 | if err != nil { |
| 3233 | return nil, parseErr("project variables", err) |
| 3234 | } |
| 3235 | |
| 3236 | // Decrypt the variables |
| 3237 | err = c.decryptProjectVariables(res) |
| 3238 | if err != nil { |
| 3239 | return nil, err |
| 3240 | } |
| 3241 | |
| 3242 | return res, nil |
| 3243 | } |
| 3244 | |
| 3245 | func (c *connection) DeleteProjectVariables(ctx context.Context, projectID, environment string, names []string) error { |
| 3246 | if len(names) == 0 { |
nothing calls this directly
no test coverage detected