| 1 | package registry |
| 2 | |
| 3 | // DeleteIfUnchanged deletes listed.Host's route only while the stored row |
| 4 | // still has the identity the caller listed it with: its kind, account and |
| 5 | // app (the owner), whether a record provisioned it, its import gate and |
| 6 | // seed column, and its creation time. It reports whether a row was |
| 7 | // deleted. |
| 8 | // |
| 9 | // It is for a pass that decides from a route list read earlier and |
| 10 | // deletes by host. Between the list and the delete, another writer can |
| 11 | // put a different row at the host: an import's rollback restoring the |
| 12 | // original route, owned by someone else, is the case it was written for. |
| 13 | // A plain Delete would remove that row, and the operator grants it |
| 14 | // carries. Here the check and the delete are one statement, so a row that |
| 15 | // changed identity is never removed on the strength of the old one. |
| 16 | func (r *Registry) DeleteIfUnchanged(listed Route) (bool, error) { |
| 17 | if r.remote != nil { |
| 18 | return false, errRemoteMethod |
| 19 | } |
| 20 | res, err := r.sql.Exec(`DELETE FROM routes WHERE host = ? AND kind = ? AND account = ? AND app = ? |
| 21 | AND provisioned = ? AND import_gate = ? AND seed_source = ? AND created_at = ?`, |
| 22 | listed.Host, listed.Kind, listed.Account, listed.App, b2iDB(listed.Provisioned), |
| 23 | listed.ImportGate, listed.SeedSource, listed.CreatedAt) |
| 24 | if err != nil { |
| 25 | return false, err |
| 26 | } |
| 27 | n, err := res.RowsAffected() |
| 28 | return n > 0, err |
| 29 | } |
| 30 | |