Fix foreign key errors for feed_id
All checks were successful
continuous-integration/drone/push Build is passing

Fix usage of QueryRow.Scan
This commit is contained in:
Peter Stuifzand 2021-10-31 14:51:31 +01:00
parent 26b85152fd
commit 3a43844e93
Signed by: peter
GPG Key ID: 374322D56E5209E8
3 changed files with 26 additions and 14 deletions

View File

@ -312,23 +312,23 @@ func (b *memoryBackend) FollowURL(uid string, url string) (microsub.Feed, error)
feed := microsub.Feed{Type: "feed", URL: url} feed := microsub.Feed{Type: "feed", URL: url}
var channelID int var channelID int
if row := b.database.QueryRow(`SELECT "id" FROM "channels" WHERE "uid" = $1`, uid); row != nil { err := b.database.QueryRow(`SELECT "id" FROM "channels" WHERE "uid" = $1`, uid).Scan(&channelID)
err := row.Scan(&channelID) if err != nil {
if err != nil { if err == sql.ErrNoRows {
log.Fatal(err) return microsub.Feed{}, fmt.Errorf("channel does not exist: %w", err)
} }
return microsub.Feed{}, err
} }
row := b.database.QueryRow( var feedID int
err = b.database.QueryRow(
`INSERT INTO "feeds" ("channel_id", "url") VALUES ($1, $2) RETURNING "id"`, `INSERT INTO "feeds" ("channel_id", "url") VALUES ($1, $2) RETURNING "id"`,
channelID, channelID,
feed.URL, feed.URL,
) ).Scan(&feedID)
if row == nil { if err != nil {
return microsub.Feed{}, fmt.Errorf("no feed_id") return feed, err
} }
var feedID int
_ = row.Scan(&feedID)
resp, err := b.Fetch3(uid, feed.URL) resp, err := b.Fetch3(uid, feed.URL)
if err != nil { if err != nil {

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.16
require ( require (
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/blevesearch/bleve/v2 v2.0.3 github.com/blevesearch/bleve/v2 v2.0.3
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gilliek/go-opml v1.0.0 github.com/gilliek/go-opml v1.0.0
github.com/golang-migrate/migrate/v4 v4.15.1 github.com/golang-migrate/migrate/v4 v4.15.1
github.com/gomodule/redigo v1.8.2 github.com/gomodule/redigo v1.8.2

View File

@ -212,20 +212,31 @@ func (p *postgresStream) AddItem(item microsub.Item) (bool, error) {
t = t2 t = t2
} }
if item.UID == "" { if item.UID == "" {
// FIXME: This won't work when we receive the item multiple times
h := sha256.Sum256([]byte(fmt.Sprintf("%s:%d", p.channel, time.Now().UnixNano()))) h := sha256.Sum256([]byte(fmt.Sprintf("%s:%d", p.channel, time.Now().UnixNano())))
item.UID = hex.EncodeToString(h[:]) item.UID = hex.EncodeToString(h[:])
} }
feedID, err := strconv.ParseInt(item.Source.ID, 10, 64) var optFeedID sql.NullInt64
if err != nil { if item.Source == nil || item.Source.ID == "" {
return false, fmt.Errorf("ERROR: item.Source.ID is not an integer %q: %w", item.Source.ID, err) optFeedID.Valid = false
optFeedID.Int64 = 0
} else {
feedID, err := strconv.ParseInt(item.Source.ID, 10, 64)
if err != nil {
optFeedID.Valid = false
optFeedID.Int64 = 0
} else {
optFeedID.Valid = true
optFeedID.Int64 = feedID
}
} }
result, err := conn.ExecContext(context.Background(), ` result, err := conn.ExecContext(context.Background(), `
INSERT INTO "items" ("channel_id", "feed_id", "uid", "data", "published_at", "created_at") INSERT INTO "items" ("channel_id", "feed_id", "uid", "data", "published_at", "created_at")
VALUES ($1, $2, $3, $4, $5, DEFAULT) VALUES ($1, $2, $3, $4, $5, DEFAULT)
ON CONFLICT ON CONSTRAINT "items_uid_key" DO NOTHING ON CONFLICT ON CONSTRAINT "items_uid_key" DO NOTHING
`, p.channelID, feedID, item.UID, &item, t) `, p.channelID, optFeedID, item.UID, &item, t)
if err != nil { if err != nil {
return false, fmt.Errorf("insert item: %w", err) return false, fmt.Errorf("insert item: %w", err)
} }