UpdateDatabaseGroup updates a database group.
(ctx context.Context, projectID, resourceID string, patch *UpdateDatabaseGroupMessage)
| 134 | |
| 135 | // UpdateDatabaseGroup updates a database group. |
| 136 | func (s *Store) UpdateDatabaseGroup(ctx context.Context, projectID, resourceID string, patch *UpdateDatabaseGroupMessage) (*DatabaseGroupMessage, error) { |
| 137 | set := qb.Q() |
| 138 | if v := patch.Title; v != nil { |
| 139 | set.Comma("name = ?", *v) |
| 140 | } |
| 141 | if v := patch.Expression; v != nil { |
| 142 | exprBytes, err := protojson.Marshal(patch.Expression) |
| 143 | if err != nil { |
| 144 | return nil, errors.Wrapf(err, "failed to marshal expression") |
| 145 | } |
| 146 | set.Comma("expression = ?", exprBytes) |
| 147 | } |
| 148 | if set.Len() == 0 { |
| 149 | return nil, errors.New("no fields to update") |
| 150 | } |
| 151 | |
| 152 | q := qb.Q().Space("UPDATE db_group SET ? WHERE project = ? AND resource_id = ? RETURNING project, resource_id, name, expression", set, projectID, resourceID) |
| 153 | |
| 154 | query, args, err := q.ToSQL() |
| 155 | if err != nil { |
| 156 | return nil, errors.Wrapf(err, "failed to build sql") |
| 157 | } |
| 158 | |
| 159 | var updatedDatabaseGroup DatabaseGroupMessage |
| 160 | var exprBytes []byte |
| 161 | if err := s.GetDB().QueryRowContext( |
| 162 | ctx, |
| 163 | query, |
| 164 | args..., |
| 165 | ).Scan( |
| 166 | &updatedDatabaseGroup.ProjectID, |
| 167 | &updatedDatabaseGroup.ResourceID, |
| 168 | &updatedDatabaseGroup.Title, |
| 169 | &exprBytes, |
| 170 | ); err != nil { |
| 171 | return nil, errors.Wrapf(err, "failed to scan") |
| 172 | } |
| 173 | var expression expr.Expr |
| 174 | if err := common.ProtojsonUnmarshaler.Unmarshal(exprBytes, &expression); err != nil { |
| 175 | return nil, errors.Wrapf(err, "failed to unmarshal expression") |
| 176 | } |
| 177 | updatedDatabaseGroup.Expression = &expression |
| 178 | return &updatedDatabaseGroup, nil |
| 179 | } |
| 180 | |
| 181 | // CreateDatabaseGroup creates a database group. |
| 182 | func (s *Store) CreateDatabaseGroup(ctx context.Context, create *DatabaseGroupMessage) (*DatabaseGroupMessage, error) { |