January 2020
Intermediate to advanced
640 pages
16h 56m
English
To upsert an edge, we will be using the following query:
INSERT INTO edges (src, dst, updated_at) VALUES ($1, $2, NOW()) ON CONFLICT (src,dst) DO UPDATE SET updated_at=NOW() RETURNING id, updated_at
As you can see, the query includes a conflict resolution step for the case where we try to insert an edge with the same (src, dst) tuple. If that happens, we simply change the updated_at column value to the current timestamp.
Unsurprisingly, the code to upsert an edge to the CockroachDB store looks quite similar to the link upsert code:
func (c *CockroachDBGraph) UpsertEdge(edge *graph.Edge) error { row := c.db.QueryRow(upsertEdgeQuery, edge.Src, edge.Dst) if err := row.Scan(&edge.ID, &edge.UpdatedAt); err != nil { if isForeignKeyViolationError(err) ...