Skip to Content
Hands-On Software Engineering with Golang
book

Hands-On Software Engineering with Golang

by Achilleas Anagnostopoulos
January 2020
Intermediate to advanced
640 pages
16h 56m
English
Packt Publishing
Content preview from Hands-On Software Engineering with Golang

Upserting edges

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) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On Software Architecture with Golang

Hands-On Software Architecture with Golang

Jyotiswarup Raiturkar

Publisher Resources

ISBN: 9781838554491Supplemental Content