| 1 |
package message |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
|
| 6 |
"heckel.io/ntfy/v2/db" |
| 7 |
"heckel.io/ntfy/v2/db/schema" |
| 8 |
) |
| 9 |
|
| 10 |
// PostgreSQL runtime query constants |
| 11 |
const ( |
| 12 |
postgresInsertMessageQuery = ` |
| 13 |
INSERT INTO message (mid, sequence_id, time, event, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_deleted, sender, user_id, content_type, encoding, published) |
| 14 |
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24) |
| 15 |
` |
| 16 |
postgresSelectScheduledMessageIDsBySeqIDQuery = `SELECT mid FROM message WHERE topic = $1 AND sequence_id = $2 AND published = FALSE` |
| 17 |
postgresDeleteScheduledBySequenceIDQuery = `DELETE FROM message WHERE topic = $1 AND sequence_id = $2 AND published = FALSE` |
| 18 |
postgresUpdateMessagesForTopicExpiryQuery = `UPDATE message SET expires = $1 WHERE topic = $2` |
| 19 |
postgresSelectMessagesByIDQuery = ` |
| 20 |
SELECT mid, sequence_id, time, event, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user_id, content_type, encoding |
| 21 |
FROM message |
| 22 |
WHERE mid = $1 |
| 23 |
` |
| 24 |
postgresSelectMessagesSinceTimeQuery = ` |
| 25 |
SELECT mid, sequence_id, time, event, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user_id, content_type, encoding |
| 26 |
FROM message |
| 27 |
WHERE topic = $1 AND time >= $2 AND published = TRUE |
| 28 |
ORDER BY time, id |
| 29 |
` |
| 30 |
postgresSelectMessagesSinceTimeIncludeScheduledQuery = ` |
| 31 |
SELECT mid, sequence_id, time, event, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user_id, content_type, encoding |
| 32 |
FROM message |
| 33 |
WHERE topic = $1 AND time >= $2 |
| 34 |
ORDER BY time, id |
| 35 |
` |
| 36 |
postgresSelectMessagesSinceIDQuery = ` |
| 37 |
SELECT mid, sequence_id, time, event, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user_id, content_type, encoding |
| 38 |
FROM message |
| 39 |
WHERE topic = $1 |
| 40 |
AND id > COALESCE((SELECT id FROM message WHERE mid = $2), 0) |
| 41 |
AND published = TRUE |
| 42 |
ORDER BY time, id |
| 43 |
` |
| 44 |
postgresSelectMessagesSinceIDIncludeScheduledQuery = ` |
| 45 |
SELECT mid, sequence_id, time, event, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user_id, content_type, encoding |
| 46 |
FROM message |
| 47 |
WHERE topic = $1 |
| 48 |
AND (id > COALESCE((SELECT id FROM message WHERE mid = $2), 0) OR published = FALSE) |
| 49 |
ORDER BY time, id |
| 50 |
` |
| 51 |
postgresSelectMessagesLatestQuery = ` |
| 52 |
SELECT mid, sequence_id, time, event, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user_id, content_type, encoding |
| 53 |
FROM message |
| 54 |
WHERE topic = $1 AND published = TRUE |
| 55 |
ORDER BY time DESC, id DESC |
| 56 |
LIMIT 1 |
| 57 |
` |
| 58 |
postgresSelectMessagesDueQuery = ` |
| 59 |
SELECT mid, sequence_id, time, event, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user_id, content_type, encoding |
| 60 |
FROM message |
| 61 |
WHERE time <= $1 AND published = FALSE |
| 62 |
ORDER BY time, id |
| 63 |
` |
| 64 |
postgresUpdateMessagePublishedQuery = `UPDATE message SET published = TRUE WHERE mid = $1` |
| 65 |
postgresSelectMessagesCountQuery = `SELECT COUNT(*) FROM message` |
| 66 |
postgresSelectTopicsQuery = `SELECT topic FROM message GROUP BY topic` |
| 67 |
|
| 68 |
postgresDeleteExpiredMessagesQuery = `DELETE FROM message WHERE mid IN (SELECT mid FROM message WHERE expires <= $1 AND published = TRUE LIMIT $2)` |
| 69 |
postgresMarkExpiredAttachmentsDeletedQuery = `UPDATE message SET attachment_deleted = TRUE WHERE mid IN (SELECT mid FROM message WHERE attachment_expires > 0 AND attachment_expires <= $1 AND attachment_deleted = FALSE LIMIT $2)` |
| 70 |
postgresSelectAttachmentsSizeBySenderQuery = `SELECT COALESCE(SUM(attachment_size), 0) FROM message WHERE user_id = '' AND sender = $1 AND attachment_expires >= $2` |
| 71 |
postgresSelectAttachmentsSizeByUserIDQuery = `SELECT COALESCE(SUM(attachment_size), 0) FROM message WHERE user_id = $1 AND attachment_expires >= $2` |
| 72 |
postgresSelectAttachmentsWithSizesQuery = `SELECT mid, attachment_size FROM message WHERE attachment_expires > $1 AND attachment_deleted = FALSE` |
| 73 |
|
| 74 |
postgresSelectStatsQuery = `SELECT value FROM message_stats WHERE key = 'messages'` |
| 75 |
postgresUpdateStatsQuery = `UPDATE message_stats SET value = $1 WHERE key = 'messages'` |
| 76 |
postgresUpdateMessageTimeQuery = `UPDATE message SET time = $1 WHERE mid = $2` |
| 77 |
) |
| 78 |
|
| 79 |
var postgresQueries = queries{ |
| 80 |
insertMessage: postgresInsertMessageQuery, |
| 81 |
selectScheduledMessageIDsBySeqID: postgresSelectScheduledMessageIDsBySeqIDQuery, |
| 82 |
deleteScheduledBySequenceID: postgresDeleteScheduledBySequenceIDQuery, |
| 83 |
updateMessagesForTopicExpiry: postgresUpdateMessagesForTopicExpiryQuery, |
| 84 |
selectMessagesByID: postgresSelectMessagesByIDQuery, |
| 85 |
selectMessagesSinceTime: postgresSelectMessagesSinceTimeQuery, |
| 86 |
selectMessagesSinceTimeScheduled: postgresSelectMessagesSinceTimeIncludeScheduledQuery, |
| 87 |
selectMessagesSinceID: postgresSelectMessagesSinceIDQuery, |
| 88 |
selectMessagesSinceIDScheduled: postgresSelectMessagesSinceIDIncludeScheduledQuery, |
| 89 |
selectMessagesLatest: postgresSelectMessagesLatestQuery, |
| 90 |
selectMessagesDue: postgresSelectMessagesDueQuery, |
| 91 |
deleteExpiredMessages: postgresDeleteExpiredMessagesQuery, |
| 92 |
updateMessagePublished: postgresUpdateMessagePublishedQuery, |
| 93 |
selectMessagesCount: postgresSelectMessagesCountQuery, |
| 94 |
selectTopics: postgresSelectTopicsQuery, |
| 95 |
markExpiredAttachmentsDeleted: postgresMarkExpiredAttachmentsDeletedQuery, |
| 96 |
selectAttachmentsSizeBySender: postgresSelectAttachmentsSizeBySenderQuery, |
| 97 |
selectAttachmentsSizeByUserID: postgresSelectAttachmentsSizeByUserIDQuery, |
| 98 |
selectAttachmentsWithSizes: postgresSelectAttachmentsWithSizesQuery, |
| 99 |
selectStats: postgresSelectStatsQuery, |
| 100 |
updateStats: postgresUpdateStatsQuery, |
| 101 |
updateMessageTime: postgresUpdateMessageTimeQuery, |
| 102 |
} |
| 103 |
|
| 104 |
// NewPostgresStore creates a new PostgreSQL-backed message cache store using an existing database connection pool. |
| 105 |
func NewPostgresStore(d *db.DB, batchSize int, batchTimeout time.Duration) (*Cache, error) { |
| 106 |
if err := schema.Migrate(d.Primary(), schema.Postgres, schemaStore, postgresCurrentSchemaVersion, postgresCreateTables, postgresMigrations); err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
return newCache(d, postgresQueries, nil, batchSize, batchTimeout, false), nil |
| 110 |
} |