diff --git a/internal/db/queries.sql b/internal/db/queries.sql index 3e5b924..4b6f249 100644 --- a/internal/db/queries.sql +++ b/internal/db/queries.sql @@ -16,12 +16,10 @@ SELECT COUNT(*) FROM node; -- name: UpsertNode :one -INSERT INTO node(public_key, fqdn, motd) -VALUES(?, ?, ?) +INSERT INTO node(public_key) +VALUES(?) ON CONFLICT(public_key) -DO UPDATE SET fqdn = EXCLUDED.fqdn, - motd = EXCLUDED.motd, - last_seen_at = unixepoch('subsec') +DO UPDATE SET last_seen_at = unixepoch('subsec') RETURNING *; -- name: UpdateNodeBootstrapInfo :exec diff --git a/internal/db/queries.sql.go b/internal/db/queries.sql.go index d2c85d4..80237ff 100644 --- a/internal/db/queries.sql.go +++ b/internal/db/queries.sql.go @@ -445,23 +445,15 @@ func (q *Queries) UpdateNodeInfoRequestTime(ctx context.Context, arg *UpdateNode } const upsertNode = `-- name: UpsertNode :one -INSERT INTO node(public_key, fqdn, motd) -VALUES(?, ?, ?) +INSERT INTO node(public_key) +VALUES(?) ON CONFLICT(public_key) -DO UPDATE SET fqdn = EXCLUDED.fqdn, - motd = EXCLUDED.motd, - last_seen_at = unixepoch('subsec') +DO UPDATE SET last_seen_at = unixepoch('subsec') RETURNING id, created_at, last_seen_at, last_info_req_at, last_info_res_at, public_key, fqdn, motd, version ` -type UpsertNodeParams struct { - PublicKey *PublicKey - Fqdn sql.NullString - Motd sql.NullString -} - -func (q *Queries) UpsertNode(ctx context.Context, arg *UpsertNodeParams) (*Node, error) { - row := q.db.QueryRowContext(ctx, upsertNode, arg.PublicKey, arg.Fqdn, arg.Motd) +func (q *Queries) UpsertNode(ctx context.Context, publicKey *PublicKey) (*Node, error) { + row := q.db.QueryRowContext(ctx, upsertNode, publicKey) var i Node err := row.Scan( &i.ID, diff --git a/internal/repo/repo.go b/internal/repo/repo.go index 745cfbd..0f1debe 100644 --- a/internal/repo/repo.go +++ b/internal/repo/repo.go @@ -73,9 +73,7 @@ func (r *NodesRepo) TrackDHTNode(ctx context.Context, node *dht.Node) (*models.N defer tx.Rollback() q := r.q.WithTx(tx) - dbNode, err := q.UpsertNode(ctx, &db.UpsertNodeParams{ - PublicKey: (*db.PublicKey)(node.PublicKey), - }) + dbNode, err := q.UpsertNode(ctx, (*db.PublicKey)(node.PublicKey)) if err != nil { return nil, fmt.Errorf("upsert node: %w", err) } diff --git a/internal/repo/repo_test.go b/internal/repo/repo_test.go index 8596c6e..6e63c8c 100644 --- a/internal/repo/repo_test.go +++ b/internal/repo/repo_test.go @@ -68,7 +68,7 @@ func TestAddNode(t *testing.T) { defer close() node := generateNode(t) - dbNode, err := repo.q.UpsertNode(ctx, &db.UpsertNodeParams{PublicKey: (*db.PublicKey)(node.PublicKey)}) + dbNode, err := repo.q.UpsertNode(ctx, (*db.PublicKey)(node.PublicKey)) if err != nil { t.Fatal(err) } @@ -93,7 +93,7 @@ func TestHasNodeByPublicKey(t *testing.T) { defer close() node := generateNode(t) - _, err := repo.q.UpsertNode(ctx, &db.UpsertNodeParams{PublicKey: (*db.PublicKey)(node.PublicKey)}) + _, err := repo.q.UpsertNode(ctx, (*db.PublicKey)(node.PublicKey)) if err != nil { t.Fatal(err) } @@ -120,7 +120,7 @@ func TestPongNonExistentNode(t *testing.T) { defer close() pk := generatePublicKey(t) - _, err := repo.q.UpsertNode(ctx, &db.UpsertNodeParams{PublicKey: (*db.PublicKey)(pk)}) + _, err := repo.q.UpsertNode(ctx, (*db.PublicKey)(pk)) if err != nil { t.Fatal(err) }