Manual refactor

This commit is contained in:
binwiederhier
2026-02-22 14:25:15 -05:00
parent 5d301e7dce
commit f726cc768e
3 changed files with 6 additions and 7 deletions

View File

@@ -40,7 +40,7 @@ type storeQueries struct {
selectSubscriptionCountBySubscriberIP string selectSubscriptionCountBySubscriberIP string
selectSubscriptionsForTopic string selectSubscriptionsForTopic string
selectSubscriptionsExpiringSoon string selectSubscriptionsExpiringSoon string
insertSubscription string upsertSubscription string
updateSubscriptionWarningSent string updateSubscriptionWarningSent string
updateSubscriptionUpdatedAt string updateSubscriptionUpdatedAt string
deleteSubscriptionByEndpoint string deleteSubscriptionByEndpoint string
@@ -71,8 +71,7 @@ func (s *commonStore) UpsertSubscription(endpoint string, auth, p256dh, userID s
} }
// Read existing subscription ID for endpoint (or create new ID) // Read existing subscription ID for endpoint (or create new ID)
var subscriptionID string var subscriptionID string
err = tx.QueryRow(s.queries.selectSubscriptionIDByEndpoint, endpoint).Scan(&subscriptionID) if err := tx.QueryRow(s.queries.selectSubscriptionIDByEndpoint, endpoint).Scan(&subscriptionID); errors.Is(err, sql.ErrNoRows) {
if errors.Is(err, sql.ErrNoRows) {
if subscriptionCount >= subscriptionEndpointLimitPerSubscriberIP { if subscriptionCount >= subscriptionEndpointLimitPerSubscriberIP {
return ErrWebPushTooManySubscriptions return ErrWebPushTooManySubscriptions
} }
@@ -82,7 +81,7 @@ func (s *commonStore) UpsertSubscription(endpoint string, auth, p256dh, userID s
} }
// Insert or update subscription // Insert or update subscription
updatedAt, warnedAt := time.Now().Unix(), 0 updatedAt, warnedAt := time.Now().Unix(), 0
if _, err = tx.Exec(s.queries.insertSubscription, subscriptionID, endpoint, auth, p256dh, userID, subscriberIP.String(), updatedAt, warnedAt); err != nil { if _, err := tx.Exec(s.queries.upsertSubscription, subscriptionID, endpoint, auth, p256dh, userID, subscriberIP.String(), updatedAt, warnedAt); err != nil {
return err return err
} }
// Replace all subscription topics // Replace all subscription topics

View File

@@ -44,7 +44,7 @@ const (
FROM webpush_subscription FROM webpush_subscription
WHERE warned_at = 0 AND updated_at <= $1 WHERE warned_at = 0 AND updated_at <= $1
` `
postgresInsertSubscriptionQuery = ` postgresUpsertSubscriptionQuery = `
INSERT INTO webpush_subscription (id, endpoint, key_auth, key_p256dh, user_id, subscriber_ip, updated_at, warned_at) INSERT INTO webpush_subscription (id, endpoint, key_auth, key_p256dh, user_id, subscriber_ip, updated_at, warned_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (endpoint) ON CONFLICT (endpoint)
@@ -80,7 +80,7 @@ func NewPostgresStore(db *sql.DB) (Store, error) {
selectSubscriptionCountBySubscriberIP: postgresSelectSubscriptionCountBySubscriberIPQuery, selectSubscriptionCountBySubscriberIP: postgresSelectSubscriptionCountBySubscriberIPQuery,
selectSubscriptionsForTopic: postgresSelectSubscriptionsForTopicQuery, selectSubscriptionsForTopic: postgresSelectSubscriptionsForTopicQuery,
selectSubscriptionsExpiringSoon: postgresSelectSubscriptionsExpiringSoonQuery, selectSubscriptionsExpiringSoon: postgresSelectSubscriptionsExpiringSoonQuery,
insertSubscription: postgresInsertSubscriptionQuery, upsertSubscription: postgresUpsertSubscriptionQuery,
updateSubscriptionWarningSent: postgresUpdateSubscriptionWarningSentQuery, updateSubscriptionWarningSent: postgresUpdateSubscriptionWarningSentQuery,
updateSubscriptionUpdatedAt: postgresUpdateSubscriptionUpdatedAtQuery, updateSubscriptionUpdatedAt: postgresUpdateSubscriptionUpdatedAtQuery,
deleteSubscriptionByEndpoint: postgresDeleteSubscriptionByEndpointQuery, deleteSubscriptionByEndpoint: postgresDeleteSubscriptionByEndpointQuery,

View File

@@ -96,7 +96,7 @@ func NewSQLiteStore(filename, startupQueries string) (Store, error) {
selectSubscriptionCountBySubscriberIP: sqliteSelectWebPushSubscriptionCountBySubscriberIPQuery, selectSubscriptionCountBySubscriberIP: sqliteSelectWebPushSubscriptionCountBySubscriberIPQuery,
selectSubscriptionsForTopic: sqliteSelectWebPushSubscriptionsForTopicQuery, selectSubscriptionsForTopic: sqliteSelectWebPushSubscriptionsForTopicQuery,
selectSubscriptionsExpiringSoon: sqliteSelectWebPushSubscriptionsExpiringSoonQuery, selectSubscriptionsExpiringSoon: sqliteSelectWebPushSubscriptionsExpiringSoonQuery,
insertSubscription: sqliteInsertWebPushSubscriptionQuery, upsertSubscription: sqliteInsertWebPushSubscriptionQuery,
updateSubscriptionWarningSent: sqliteUpdateWebPushSubscriptionWarningSentQuery, updateSubscriptionWarningSent: sqliteUpdateWebPushSubscriptionWarningSentQuery,
updateSubscriptionUpdatedAt: sqliteUpdateWebPushSubscriptionUpdatedAtQuery, updateSubscriptionUpdatedAt: sqliteUpdateWebPushSubscriptionUpdatedAtQuery,
deleteSubscriptionByEndpoint: sqliteDeleteWebPushSubscriptionByEndpointQuery, deleteSubscriptionByEndpoint: sqliteDeleteWebPushSubscriptionByEndpointQuery,