From 44442b2b62a812257811402c477913a8ab2b4de6 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Mon, 5 Nov 2018 19:15:29 +0530 Subject: [PATCH] Switch UI subscriber addition to a new insert-only query --- queries.go | 1 + queries.sql | 28 +++++++++++++++++++++++----- subscribers.go | 6 ++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/queries.go b/queries.go index f6acd3b..e60f157 100644 --- a/queries.go +++ b/queries.go @@ -8,6 +8,7 @@ import ( // Queries contains all prepared SQL queries. type Queries struct { + InsertSubscriber *sqlx.Stmt `query:"insert-subscriber"` UpsertSubscriber *sqlx.Stmt `query:"upsert-subscriber"` BlacklistSubscriber *sqlx.Stmt `query:"blacklist-subscriber"` GetSubscriber *sqlx.Stmt `query:"get-subscriber"` diff --git a/queries.sql b/queries.sql index e57431d..195e4f3 100644 --- a/queries.sql +++ b/queries.sql @@ -39,22 +39,39 @@ SELECT COUNT(subscribers.id) as num FROM subscribers INNER JOIN subscriber_lists WHERE subscriber_lists.list_id = %d %s; +-- name: insert-subscriber +WITH sub AS ( + INSERT INTO subscribers (uuid, email, name, attribs) + VALUES($1, $2, $3, $4) + returning id +), +subs AS ( + INSERT INTO subscriber_lists (subscriber_id, list_id) + VALUES((SELECT id FROM sub), UNNEST($5::INT[])) + ON CONFLICT (subscriber_id, list_id) DO UPDATE + SET updated_at=NOW() +) +SELECT id from sub; + -- name: upsert-subscriber -- Upserts a subscriber where existing subscribers get their names and attributes overwritten. -- The status field is only updated when $6 = 'override_status'. -WITH s AS ( +WITH sub AS ( INSERT INTO subscribers (uuid, email, name, attribs) VALUES($1, $2, $3, $4) ON CONFLICT (email) DO UPDATE SET name=$3, attribs=$4, updated_at=NOW() - RETURNING id -) INSERT INTO subscriber_lists (subscriber_id, list_id) - VALUES((SELECT id FROM s), UNNEST($5::INT[])) + RETURNING uuid, id +), +subs AS ( + INSERT INTO subscriber_lists (subscriber_id, list_id) + VALUES((SELECT id FROM sub), UNNEST($5::INT[])) ON CONFLICT (subscriber_id, list_id) DO UPDATE SET updated_at=NOW() - RETURNING subscriber_id; +) +SELECT uuid, id from sub; -- name: blacklist-subscriber -- Upserts a subscriber where the update will only set the status to blacklisted @@ -247,6 +264,7 @@ subs AS ( WHERE subscriber_lists.list_id=ANY( SELECT list_id FROM campaign_lists where campaign_id=$1 AND list_id IS NOT NULL ) + AND subscribers.status != 'blacklisted' AND id > (SELECT last_subscriber_id FROM camp) AND id <= (SELECT max_subscriber_id FROM camp) ORDER BY id LIMIT $2 diff --git a/subscribers.go b/subscribers.go index 4fb9f3d..955f2b6 100644 --- a/subscribers.go +++ b/subscribers.go @@ -180,16 +180,14 @@ func handleCreateSubscriber(c echo.Context) error { // Insert and read ID. var newID int - err := app.Queries.UpsertSubscriber.Get(&newID, + err := app.Queries.InsertSubscriber.Get(&newID, uuid.NewV4(), strings.ToLower(strings.TrimSpace(req.Email)), strings.TrimSpace(req.Name), - req.Status, req.Attribs, - true, req.Lists) if err != nil { - if err == sql.ErrNoRows { + if pqErr, ok := err.(*pq.Error); ok && pqErr.Constraint == "subscribers_email_key" { return echo.NewHTTPError(http.StatusBadRequest, "The e-mail already exists.") }