mirror of
https://github.com/binwiederhier/ntfy.git
synced 2026-03-18 21:30:44 +01:00
Rename queries for consistency
This commit is contained in:
@@ -70,9 +70,9 @@ const (
|
|||||||
postgresSelectAttachmentsSizeBySenderQuery = `SELECT COALESCE(SUM(attachment_size), 0) FROM message WHERE user_id = '' AND sender = $1 AND attachment_expires >= $2`
|
postgresSelectAttachmentsSizeBySenderQuery = `SELECT COALESCE(SUM(attachment_size), 0) FROM message WHERE user_id = '' AND sender = $1 AND attachment_expires >= $2`
|
||||||
postgresSelectAttachmentsSizeByUserIDQuery = `SELECT COALESCE(SUM(attachment_size), 0) FROM message WHERE user_id = $1 AND attachment_expires >= $2`
|
postgresSelectAttachmentsSizeByUserIDQuery = `SELECT COALESCE(SUM(attachment_size), 0) FROM message WHERE user_id = $1 AND attachment_expires >= $2`
|
||||||
|
|
||||||
postgresSelectStatsQuery = `SELECT value FROM message_stats WHERE key = 'messages'`
|
postgresSelectStatsQuery = `SELECT value FROM message_stats WHERE key = 'messages'`
|
||||||
postgresUpdateStatsQuery = `UPDATE message_stats SET value = $1 WHERE key = 'messages'`
|
postgresUpdateStatsQuery = `UPDATE message_stats SET value = $1 WHERE key = 'messages'`
|
||||||
postgresUpdateMessageTimesQuery = `UPDATE message SET time = $1 WHERE mid = $2`
|
postgresUpdateMessageTimeQuery = `UPDATE message SET time = $1 WHERE mid = $2`
|
||||||
)
|
)
|
||||||
|
|
||||||
var pgQueries = queries{
|
var pgQueries = queries{
|
||||||
@@ -98,7 +98,7 @@ var pgQueries = queries{
|
|||||||
selectAttachmentsSizeByUserID: postgresSelectAttachmentsSizeByUserIDQuery,
|
selectAttachmentsSizeByUserID: postgresSelectAttachmentsSizeByUserIDQuery,
|
||||||
selectStats: postgresSelectStatsQuery,
|
selectStats: postgresSelectStatsQuery,
|
||||||
updateStats: postgresUpdateStatsQuery,
|
updateStats: postgresUpdateStatsQuery,
|
||||||
updateMessageTime: postgresUpdateMessageTimesQuery,
|
updateMessageTime: postgresUpdateMessageTimeQuery,
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPostgresStore creates a new PostgreSQL-backed message cache store using an existing database connection pool.
|
// NewPostgresStore creates a new PostgreSQL-backed message cache store using an existing database connection pool.
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
// Initial SQLite schema
|
// Initial SQLite schema
|
||||||
const (
|
const (
|
||||||
sqliteCreateMessagesTableQuery = `
|
sqliteCreateTablesQuery = `
|
||||||
BEGIN;
|
BEGIN;
|
||||||
CREATE TABLE IF NOT EXISTS messages (
|
CREATE TABLE IF NOT EXISTS messages (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -259,7 +259,7 @@ func setupSQLite(db *sql.DB, startupQueries string, cacheDuration time.Duration)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setupNewSQLite(db *sql.DB) error {
|
func setupNewSQLite(db *sql.DB) error {
|
||||||
if _, err := db.Exec(sqliteCreateMessagesTableQuery); err != nil {
|
if _, err := db.Exec(sqliteCreateTablesQuery); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := db.Exec(sqliteCreateSchemaVersionTableQuery); err != nil {
|
if _, err := db.Exec(sqliteCreateSchemaVersionTableQuery); err != nil {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sqliteCreateWebPushSubscriptionsTableQuery = `
|
sqliteCreateTablesQuery = `
|
||||||
CREATE TABLE IF NOT EXISTS subscription (
|
CREATE TABLE IF NOT EXISTS subscription (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
endpoint TEXT NOT NULL,
|
endpoint TEXT NOT NULL,
|
||||||
@@ -37,42 +37,42 @@ const (
|
|||||||
PRAGMA foreign_keys = ON;
|
PRAGMA foreign_keys = ON;
|
||||||
`
|
`
|
||||||
|
|
||||||
sqliteSelectWebPushSubscriptionIDByEndpointQuery = `SELECT id FROM subscription WHERE endpoint = ?`
|
sqliteSelectSubscriptionIDByEndpointQuery = `SELECT id FROM subscription WHERE endpoint = ?`
|
||||||
sqliteSelectWebPushSubscriptionCountBySubscriberIPQuery = `SELECT COUNT(*) FROM subscription WHERE subscriber_ip = ?`
|
sqliteSelectSubscriptionCountBySubscriberIPQuery = `SELECT COUNT(*) FROM subscription WHERE subscriber_ip = ?`
|
||||||
sqliteSelectWebPushSubscriptionsForTopicQuery = `
|
sqliteSelectSubscriptionsForTopicQuery = `
|
||||||
SELECT id, endpoint, key_auth, key_p256dh, user_id
|
SELECT id, endpoint, key_auth, key_p256dh, user_id
|
||||||
FROM subscription_topic st
|
FROM subscription_topic st
|
||||||
JOIN subscription s ON s.id = st.subscription_id
|
JOIN subscription s ON s.id = st.subscription_id
|
||||||
WHERE st.topic = ?
|
WHERE st.topic = ?
|
||||||
ORDER BY endpoint
|
ORDER BY endpoint
|
||||||
`
|
`
|
||||||
sqliteSelectWebPushSubscriptionsExpiringSoonQuery = `
|
sqliteSelectSubscriptionsExpiringSoonQuery = `
|
||||||
SELECT id, endpoint, key_auth, key_p256dh, user_id
|
SELECT id, endpoint, key_auth, key_p256dh, user_id
|
||||||
FROM subscription
|
FROM subscription
|
||||||
WHERE warned_at = 0 AND updated_at <= ?
|
WHERE warned_at = 0 AND updated_at <= ?
|
||||||
`
|
`
|
||||||
sqliteInsertWebPushSubscriptionQuery = `
|
sqliteUpsertSubscriptionQuery = `
|
||||||
INSERT INTO subscription (id, endpoint, key_auth, key_p256dh, user_id, subscriber_ip, updated_at, warned_at)
|
INSERT INTO subscription (id, endpoint, key_auth, key_p256dh, user_id, subscriber_ip, updated_at, warned_at)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
ON CONFLICT (endpoint)
|
ON CONFLICT (endpoint)
|
||||||
DO UPDATE SET key_auth = excluded.key_auth, key_p256dh = excluded.key_p256dh, user_id = excluded.user_id, subscriber_ip = excluded.subscriber_ip, updated_at = excluded.updated_at, warned_at = excluded.warned_at
|
DO UPDATE SET key_auth = excluded.key_auth, key_p256dh = excluded.key_p256dh, user_id = excluded.user_id, subscriber_ip = excluded.subscriber_ip, updated_at = excluded.updated_at, warned_at = excluded.warned_at
|
||||||
`
|
`
|
||||||
sqliteUpdateWebPushSubscriptionWarningSentQuery = `UPDATE subscription SET warned_at = ? WHERE id = ?`
|
sqliteUpdateSubscriptionWarningSentQuery = `UPDATE subscription SET warned_at = ? WHERE id = ?`
|
||||||
sqliteUpdateWebPushSubscriptionUpdatedAtQuery = `UPDATE subscription SET updated_at = ? WHERE endpoint = ?`
|
sqliteUpdateSubscriptionUpdatedAtQuery = `UPDATE subscription SET updated_at = ? WHERE endpoint = ?`
|
||||||
sqliteDeleteWebPushSubscriptionByEndpointQuery = `DELETE FROM subscription WHERE endpoint = ?`
|
sqliteDeleteSubscriptionByEndpointQuery = `DELETE FROM subscription WHERE endpoint = ?`
|
||||||
sqliteDeleteWebPushSubscriptionByUserIDQuery = `DELETE FROM subscription WHERE user_id = ?`
|
sqliteDeleteSubscriptionByUserIDQuery = `DELETE FROM subscription WHERE user_id = ?`
|
||||||
sqliteDeleteWebPushSubscriptionByAgeQuery = `DELETE FROM subscription WHERE updated_at <= ?` // Full table scan!
|
sqliteDeleteSubscriptionByAgeQuery = `DELETE FROM subscription WHERE updated_at <= ?` // Full table scan!
|
||||||
|
|
||||||
sqliteInsertWebPushSubscriptionTopicQuery = `INSERT INTO subscription_topic (subscription_id, topic) VALUES (?, ?)`
|
sqliteInsertSubscriptionTopicQuery = `INSERT INTO subscription_topic (subscription_id, topic) VALUES (?, ?)`
|
||||||
sqliteDeleteWebPushSubscriptionTopicAllQuery = `DELETE FROM subscription_topic WHERE subscription_id = ?`
|
sqliteDeleteSubscriptionTopicAllQuery = `DELETE FROM subscription_topic WHERE subscription_id = ?`
|
||||||
sqliteDeleteWebPushSubscriptionTopicWithoutSubscriptionQuery = `DELETE FROM subscription_topic WHERE subscription_id NOT IN (SELECT id FROM subscription)`
|
sqliteDeleteSubscriptionTopicWithoutSubscriptionQuery = `DELETE FROM subscription_topic WHERE subscription_id NOT IN (SELECT id FROM subscription)`
|
||||||
)
|
)
|
||||||
|
|
||||||
// SQLite schema management queries
|
// SQLite schema management queries
|
||||||
const (
|
const (
|
||||||
sqliteCurrentWebPushSchemaVersion = 1
|
sqliteCurrentSchemaVersion = 1
|
||||||
sqliteInsertWebPushSchemaVersionQuery = `INSERT INTO schemaVersion VALUES (1, ?)`
|
sqliteInsertSchemaVersionQuery = `INSERT INTO schemaVersion VALUES (1, ?)`
|
||||||
sqliteSelectWebPushSchemaVersionQuery = `SELECT version FROM schemaVersion WHERE id = 1`
|
sqliteSelectSchemaVersionQuery = `SELECT version FROM schemaVersion WHERE id = 1`
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewSQLiteStore creates a new SQLite-backed web push store.
|
// NewSQLiteStore creates a new SQLite-backed web push store.
|
||||||
@@ -90,31 +90,31 @@ func NewSQLiteStore(filename, startupQueries string) (*Store, error) {
|
|||||||
return &Store{
|
return &Store{
|
||||||
db: db,
|
db: db,
|
||||||
queries: queries{
|
queries: queries{
|
||||||
selectSubscriptionIDByEndpoint: sqliteSelectWebPushSubscriptionIDByEndpointQuery,
|
selectSubscriptionIDByEndpoint: sqliteSelectSubscriptionIDByEndpointQuery,
|
||||||
selectSubscriptionCountBySubscriberIP: sqliteSelectWebPushSubscriptionCountBySubscriberIPQuery,
|
selectSubscriptionCountBySubscriberIP: sqliteSelectSubscriptionCountBySubscriberIPQuery,
|
||||||
selectSubscriptionsForTopic: sqliteSelectWebPushSubscriptionsForTopicQuery,
|
selectSubscriptionsForTopic: sqliteSelectSubscriptionsForTopicQuery,
|
||||||
selectSubscriptionsExpiringSoon: sqliteSelectWebPushSubscriptionsExpiringSoonQuery,
|
selectSubscriptionsExpiringSoon: sqliteSelectSubscriptionsExpiringSoonQuery,
|
||||||
upsertSubscription: sqliteInsertWebPushSubscriptionQuery,
|
upsertSubscription: sqliteUpsertSubscriptionQuery,
|
||||||
updateSubscriptionWarningSent: sqliteUpdateWebPushSubscriptionWarningSentQuery,
|
updateSubscriptionWarningSent: sqliteUpdateSubscriptionWarningSentQuery,
|
||||||
updateSubscriptionUpdatedAt: sqliteUpdateWebPushSubscriptionUpdatedAtQuery,
|
updateSubscriptionUpdatedAt: sqliteUpdateSubscriptionUpdatedAtQuery,
|
||||||
deleteSubscriptionByEndpoint: sqliteDeleteWebPushSubscriptionByEndpointQuery,
|
deleteSubscriptionByEndpoint: sqliteDeleteSubscriptionByEndpointQuery,
|
||||||
deleteSubscriptionByUserID: sqliteDeleteWebPushSubscriptionByUserIDQuery,
|
deleteSubscriptionByUserID: sqliteDeleteSubscriptionByUserIDQuery,
|
||||||
deleteSubscriptionByAge: sqliteDeleteWebPushSubscriptionByAgeQuery,
|
deleteSubscriptionByAge: sqliteDeleteSubscriptionByAgeQuery,
|
||||||
insertSubscriptionTopic: sqliteInsertWebPushSubscriptionTopicQuery,
|
insertSubscriptionTopic: sqliteInsertSubscriptionTopicQuery,
|
||||||
deleteSubscriptionTopicAll: sqliteDeleteWebPushSubscriptionTopicAllQuery,
|
deleteSubscriptionTopicAll: sqliteDeleteSubscriptionTopicAllQuery,
|
||||||
deleteSubscriptionTopicWithoutSubscription: sqliteDeleteWebPushSubscriptionTopicWithoutSubscriptionQuery,
|
deleteSubscriptionTopicWithoutSubscription: sqliteDeleteSubscriptionTopicWithoutSubscriptionQuery,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupSQLite(db *sql.DB) error {
|
func setupSQLite(db *sql.DB) error {
|
||||||
var schemaVersion int
|
var schemaVersion int
|
||||||
err := db.QueryRow(sqliteSelectWebPushSchemaVersionQuery).Scan(&schemaVersion)
|
err := db.QueryRow(sqliteSelectSchemaVersionQuery).Scan(&schemaVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return setupNewSQLite(db)
|
return setupNewSQLite(db)
|
||||||
}
|
}
|
||||||
if schemaVersion > sqliteCurrentWebPushSchemaVersion {
|
if schemaVersion > sqliteCurrentSchemaVersion {
|
||||||
return fmt.Errorf("unexpected schema version: version %d is higher than current version %d", schemaVersion, sqliteCurrentWebPushSchemaVersion)
|
return fmt.Errorf("unexpected schema version: version %d is higher than current version %d", schemaVersion, sqliteCurrentSchemaVersion)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -125,10 +125,10 @@ func setupNewSQLite(db *sql.DB) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
if _, err := tx.Exec(sqliteCreateWebPushSubscriptionsTableQuery); err != nil {
|
if _, err := tx.Exec(sqliteCreateTablesQuery); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := tx.Exec(sqliteInsertWebPushSchemaVersionQuery, sqliteCurrentWebPushSchemaVersion); err != nil {
|
if _, err := tx.Exec(sqliteInsertSchemaVersionQuery, sqliteCurrentSchemaVersion); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user