2021-11-03 01:09:49 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2021-11-28 19:07:29 +00:00
|
|
|
"fmt"
|
2021-11-03 01:09:49 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3" // SQLite driver
|
2021-11-29 20:34:29 +00:00
|
|
|
"log"
|
2021-11-28 19:07:29 +00:00
|
|
|
"strings"
|
2021-11-03 01:09:49 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-11-28 19:07:29 +00:00
|
|
|
// Messages cache
|
2021-11-03 01:09:49 +00:00
|
|
|
const (
|
2021-11-28 19:07:29 +00:00
|
|
|
createMessagesTableQuery = `
|
2021-11-03 01:09:49 +00:00
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE IF NOT EXISTS messages (
|
2021-12-31 15:19:41 +00:00
|
|
|
id TEXT PRIMARY KEY,
|
2021-11-03 01:09:49 +00:00
|
|
|
time INT NOT NULL,
|
2021-12-31 15:19:41 +00:00
|
|
|
topic TEXT NOT NULL,
|
|
|
|
message TEXT NOT NULL,
|
|
|
|
title TEXT NOT NULL,
|
2021-11-28 19:07:29 +00:00
|
|
|
priority INT NOT NULL,
|
2021-12-31 15:19:41 +00:00
|
|
|
tags TEXT NOT NULL,
|
2022-01-04 22:40:41 +00:00
|
|
|
click TEXT NOT NULL,
|
2021-12-10 16:31:42 +00:00
|
|
|
published INT NOT NULL
|
2021-11-03 01:09:49 +00:00
|
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic);
|
|
|
|
COMMIT;
|
|
|
|
`
|
2022-01-04 22:40:41 +00:00
|
|
|
insertMessageQuery = `INSERT INTO messages (id, time, topic, message, title, priority, tags, click, published) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
2021-12-11 03:57:01 +00:00
|
|
|
pruneMessagesQuery = `DELETE FROM messages WHERE time < ? AND published = 1`
|
2021-11-03 01:09:49 +00:00
|
|
|
selectMessagesSinceTimeQuery = `
|
2022-01-04 22:40:41 +00:00
|
|
|
SELECT id, time, topic, message, title, priority, tags, click
|
2021-12-10 16:31:42 +00:00
|
|
|
FROM messages
|
|
|
|
WHERE topic = ? AND time >= ? AND published = 1
|
|
|
|
ORDER BY time ASC
|
|
|
|
`
|
|
|
|
selectMessagesSinceTimeIncludeScheduledQuery = `
|
2022-01-04 22:40:41 +00:00
|
|
|
SELECT id, time, topic, message, title, priority, tags, click
|
2021-11-03 01:09:49 +00:00
|
|
|
FROM messages
|
|
|
|
WHERE topic = ? AND time >= ?
|
|
|
|
ORDER BY time ASC
|
|
|
|
`
|
2021-12-10 16:31:42 +00:00
|
|
|
selectMessagesDueQuery = `
|
2022-01-04 22:40:41 +00:00
|
|
|
SELECT id, time, topic, message, title, priority, tags, click
|
2021-12-10 16:31:42 +00:00
|
|
|
FROM messages
|
|
|
|
WHERE time <= ? AND published = 0
|
|
|
|
`
|
|
|
|
updateMessagePublishedQuery = `UPDATE messages SET published = 1 WHERE id = ?`
|
2021-11-28 19:07:29 +00:00
|
|
|
selectMessagesCountQuery = `SELECT COUNT(*) FROM messages`
|
2021-11-29 16:48:34 +00:00
|
|
|
selectMessageCountForTopicQuery = `SELECT COUNT(*) FROM messages WHERE topic = ?`
|
2021-12-09 03:57:31 +00:00
|
|
|
selectTopicsQuery = `SELECT topic FROM messages GROUP BY topic`
|
2021-11-28 19:07:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Schema management queries
|
|
|
|
const (
|
2022-01-04 22:40:41 +00:00
|
|
|
currentSchemaVersion = 3
|
2021-11-28 19:07:29 +00:00
|
|
|
createSchemaVersionTableQuery = `
|
|
|
|
CREATE TABLE IF NOT EXISTS schemaVersion (
|
|
|
|
id INT PRIMARY KEY,
|
|
|
|
version INT NOT NULL
|
|
|
|
);
|
|
|
|
`
|
|
|
|
insertSchemaVersion = `INSERT INTO schemaVersion VALUES (1, ?)`
|
2021-12-10 16:31:42 +00:00
|
|
|
updateSchemaVersion = `UPDATE schemaVersion SET version = ? WHERE id = 1`
|
2021-11-28 19:07:29 +00:00
|
|
|
selectSchemaVersionQuery = `SELECT version FROM schemaVersion WHERE id = 1`
|
|
|
|
|
|
|
|
// 0 -> 1
|
|
|
|
migrate0To1AlterMessagesTableQuery = `
|
|
|
|
BEGIN;
|
2021-12-31 15:19:41 +00:00
|
|
|
ALTER TABLE messages ADD COLUMN title TEXT NOT NULL DEFAULT('');
|
2021-11-28 19:07:29 +00:00
|
|
|
ALTER TABLE messages ADD COLUMN priority INT NOT NULL DEFAULT(0);
|
2021-12-31 15:19:41 +00:00
|
|
|
ALTER TABLE messages ADD COLUMN tags TEXT NOT NULL DEFAULT('');
|
2021-11-28 19:07:29 +00:00
|
|
|
COMMIT;
|
|
|
|
`
|
2021-12-10 16:31:42 +00:00
|
|
|
|
|
|
|
// 1 -> 2
|
|
|
|
migrate1To2AlterMessagesTableQuery = `
|
|
|
|
ALTER TABLE messages ADD COLUMN published INT NOT NULL DEFAULT(1);
|
|
|
|
`
|
2022-01-04 22:40:41 +00:00
|
|
|
|
|
|
|
// 2 -> 3
|
|
|
|
migrate2To3AlterMessagesTableQuery = `
|
|
|
|
ALTER TABLE messages ADD COLUMN click TEXT NOT NULL DEFAULT('');
|
|
|
|
`
|
2021-11-03 01:09:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type sqliteCache struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ cache = (*sqliteCache)(nil)
|
|
|
|
|
|
|
|
func newSqliteCache(filename string) (*sqliteCache, error) {
|
|
|
|
db, err := sql.Open("sqlite3", filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-28 19:07:29 +00:00
|
|
|
if err := setupDB(db); err != nil {
|
2021-11-03 01:09:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &sqliteCache{
|
2021-11-03 15:33:34 +00:00
|
|
|
db: db,
|
2021-11-03 01:09:49 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sqliteCache) AddMessage(m *message) error {
|
2021-12-07 16:45:15 +00:00
|
|
|
if m.Event != messageEvent {
|
|
|
|
return errUnexpectedMessageType
|
|
|
|
}
|
2021-12-10 16:31:42 +00:00
|
|
|
published := m.Time <= time.Now().Unix()
|
2022-01-04 22:40:41 +00:00
|
|
|
_, err := c.db.Exec(insertMessageQuery, m.ID, m.Time, m.Topic, m.Message, m.Title, m.Priority, strings.Join(m.Tags, ","), m.Click, published)
|
2021-11-03 01:09:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:31:42 +00:00
|
|
|
func (c *sqliteCache) Messages(topic string, since sinceTime, scheduled bool) ([]*message, error) {
|
2021-12-07 16:45:15 +00:00
|
|
|
if since.IsNone() {
|
|
|
|
return make([]*message, 0), nil
|
|
|
|
}
|
2021-12-10 16:31:42 +00:00
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
if scheduled {
|
|
|
|
rows, err = c.db.Query(selectMessagesSinceTimeIncludeScheduledQuery, topic, since.Time().Unix())
|
|
|
|
} else {
|
|
|
|
rows, err = c.db.Query(selectMessagesSinceTimeQuery, topic, since.Time().Unix())
|
|
|
|
}
|
2021-11-03 01:09:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-10 16:31:42 +00:00
|
|
|
return readMessages(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sqliteCache) MessagesDue() ([]*message, error) {
|
|
|
|
rows, err := c.db.Query(selectMessagesDueQuery, time.Now().Unix())
|
|
|
|
if err != nil {
|
2021-11-03 01:09:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-10 16:31:42 +00:00
|
|
|
return readMessages(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sqliteCache) MarkPublished(m *message) error {
|
|
|
|
_, err := c.db.Exec(updateMessagePublishedQuery, m.ID)
|
|
|
|
return err
|
2021-11-03 01:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sqliteCache) MessageCount(topic string) (int, error) {
|
2021-11-28 19:07:29 +00:00
|
|
|
rows, err := c.db.Query(selectMessageCountForTopicQuery, topic)
|
2021-11-03 01:09:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var count int
|
|
|
|
if !rows.Next() {
|
|
|
|
return 0, errors.New("no rows found")
|
|
|
|
}
|
|
|
|
if err := rows.Scan(&count); err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if err := rows.Err(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 16:45:15 +00:00
|
|
|
func (c *sqliteCache) Topics() (map[string]*topic, error) {
|
|
|
|
rows, err := c.db.Query(selectTopicsQuery)
|
2021-11-03 01:09:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2021-12-07 16:45:15 +00:00
|
|
|
topics := make(map[string]*topic)
|
2021-11-03 01:09:49 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var id string
|
2021-12-09 03:57:31 +00:00
|
|
|
if err := rows.Scan(&id); err != nil {
|
2021-11-03 01:09:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-09 03:57:31 +00:00
|
|
|
topics[id] = newTopic(id)
|
2021-11-03 01:09:49 +00:00
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return topics, nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 03:57:31 +00:00
|
|
|
func (c *sqliteCache) Prune(olderThan time.Time) error {
|
|
|
|
_, err := c.db.Exec(pruneMessagesQuery, olderThan.Unix())
|
2021-11-03 01:09:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-28 19:07:29 +00:00
|
|
|
|
2021-12-10 16:31:42 +00:00
|
|
|
func readMessages(rows *sql.Rows) ([]*message, error) {
|
|
|
|
defer rows.Close()
|
|
|
|
messages := make([]*message, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var timestamp int64
|
|
|
|
var priority int
|
2022-01-04 22:40:41 +00:00
|
|
|
var id, topic, msg, title, tagsStr, click string
|
|
|
|
if err := rows.Scan(&id, ×tamp, &topic, &msg, &title, &priority, &tagsStr, &click); err != nil {
|
2021-12-10 16:31:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tags []string
|
|
|
|
if tagsStr != "" {
|
|
|
|
tags = strings.Split(tagsStr, ",")
|
|
|
|
}
|
|
|
|
messages = append(messages, &message{
|
|
|
|
ID: id,
|
|
|
|
Time: timestamp,
|
|
|
|
Event: messageEvent,
|
|
|
|
Topic: topic,
|
|
|
|
Message: msg,
|
|
|
|
Title: title,
|
|
|
|
Priority: priority,
|
|
|
|
Tags: tags,
|
2022-01-04 22:40:41 +00:00
|
|
|
Click: click,
|
2021-12-10 16:31:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return messages, nil
|
|
|
|
}
|
|
|
|
|
2021-11-28 19:07:29 +00:00
|
|
|
func setupDB(db *sql.DB) error {
|
|
|
|
// If 'messages' table does not exist, this must be a new database
|
|
|
|
rowsMC, err := db.Query(selectMessagesCountQuery)
|
|
|
|
if err != nil {
|
|
|
|
return setupNewDB(db)
|
|
|
|
}
|
2021-12-11 01:28:56 +00:00
|
|
|
rowsMC.Close()
|
2021-11-28 19:07:29 +00:00
|
|
|
|
|
|
|
// If 'messages' table exists, check 'schemaVersion' table
|
|
|
|
schemaVersion := 0
|
|
|
|
rowsSV, err := db.Query(selectSchemaVersionQuery)
|
|
|
|
if err == nil {
|
|
|
|
defer rowsSV.Close()
|
|
|
|
if !rowsSV.Next() {
|
|
|
|
return errors.New("cannot determine schema version: cache file may be corrupt")
|
|
|
|
}
|
|
|
|
if err := rowsSV.Scan(&schemaVersion); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 01:28:56 +00:00
|
|
|
rowsSV.Close()
|
2021-11-28 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do migrations
|
|
|
|
if schemaVersion == currentSchemaVersion {
|
|
|
|
return nil
|
|
|
|
} else if schemaVersion == 0 {
|
2021-12-10 16:31:42 +00:00
|
|
|
return migrateFrom0(db)
|
|
|
|
} else if schemaVersion == 1 {
|
|
|
|
return migrateFrom1(db)
|
2022-01-04 22:40:41 +00:00
|
|
|
} else if schemaVersion == 2 {
|
|
|
|
return migrateFrom2(db)
|
2021-11-28 19:07:29 +00:00
|
|
|
}
|
|
|
|
return fmt.Errorf("unexpected schema version found: %d", schemaVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupNewDB(db *sql.DB) error {
|
|
|
|
if _, err := db.Exec(createMessagesTableQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(createSchemaVersionTableQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(insertSchemaVersion, currentSchemaVersion); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:31:42 +00:00
|
|
|
func migrateFrom0(db *sql.DB) error {
|
2021-11-29 20:34:29 +00:00
|
|
|
log.Print("Migrating cache database schema: from 0 to 1")
|
2021-11-28 19:07:29 +00:00
|
|
|
if _, err := db.Exec(migrate0To1AlterMessagesTableQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(createSchemaVersionTableQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(insertSchemaVersion, 1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-10 16:31:42 +00:00
|
|
|
return migrateFrom1(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateFrom1(db *sql.DB) error {
|
|
|
|
log.Print("Migrating cache database schema: from 1 to 2")
|
|
|
|
if _, err := db.Exec(migrate1To2AlterMessagesTableQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(updateSchemaVersion, 2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-04 22:40:41 +00:00
|
|
|
return migrateFrom2(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateFrom2(db *sql.DB) error {
|
|
|
|
log.Print("Migrating cache database schema: from 2 to 3")
|
|
|
|
if _, err := db.Exec(migrate2To3AlterMessagesTableQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(updateSchemaVersion, 3); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-10 16:31:42 +00:00
|
|
|
return nil // Update this when a new version is added
|
2021-11-28 19:07:29 +00:00
|
|
|
}
|