Store encoding in cache database

This commit is contained in:
Philipp Heckel 2022-01-17 19:14:51 -05:00
parent 7e817f408c
commit df6611e8de
2 changed files with 54 additions and 9 deletions

View File

@ -29,30 +29,31 @@ const (
attachment_expires INT NOT NULL, attachment_expires INT NOT NULL,
attachment_url TEXT NOT NULL, attachment_url TEXT NOT NULL,
attachment_owner TEXT NOT NULL, attachment_owner TEXT NOT NULL,
encoding TEXT NOT NULL,
published INT NOT NULL published INT NOT NULL
); );
CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic); CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic);
COMMIT; COMMIT;
` `
insertMessageQuery = ` insertMessageQuery = `
INSERT INTO messages (id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_owner, published) INSERT INTO messages (id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_owner, encoding, published)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
` `
pruneMessagesQuery = `DELETE FROM messages WHERE time < ? AND published = 1` pruneMessagesQuery = `DELETE FROM messages WHERE time < ? AND published = 1`
selectMessagesSinceTimeQuery = ` selectMessagesSinceTimeQuery = `
SELECT id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_owner SELECT id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_owner, encoding
FROM messages FROM messages
WHERE topic = ? AND time >= ? AND published = 1 WHERE topic = ? AND time >= ? AND published = 1
ORDER BY time ASC ORDER BY time ASC
` `
selectMessagesSinceTimeIncludeScheduledQuery = ` selectMessagesSinceTimeIncludeScheduledQuery = `
SELECT id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_owner SELECT id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_owner, encoding
FROM messages FROM messages
WHERE topic = ? AND time >= ? WHERE topic = ? AND time >= ?
ORDER BY time ASC ORDER BY time ASC
` `
selectMessagesDueQuery = ` selectMessagesDueQuery = `
SELECT id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_owner SELECT id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_owner, encoding
FROM messages FROM messages
WHERE time <= ? AND published = 0 WHERE time <= ? AND published = 0
` `
@ -66,7 +67,7 @@ const (
// Schema management queries // Schema management queries
const ( const (
currentSchemaVersion = 3 currentSchemaVersion = 4
createSchemaVersionTableQuery = ` createSchemaVersionTableQuery = `
CREATE TABLE IF NOT EXISTS schemaVersion ( CREATE TABLE IF NOT EXISTS schemaVersion (
id INT PRIMARY KEY, id INT PRIMARY KEY,
@ -103,6 +104,10 @@ const (
ALTER TABLE messages ADD COLUMN attachment_url TEXT NOT NULL DEFAULT(''); ALTER TABLE messages ADD COLUMN attachment_url TEXT NOT NULL DEFAULT('');
COMMIT; COMMIT;
` `
// 3 -> 4
migrate3To4AlterMessagesTableQuery = `
ALTER TABLE messages ADD COLUMN encoding TEXT NOT NULL DEFAULT('');
`
) )
type sqliteCache struct { type sqliteCache struct {
@ -156,6 +161,7 @@ func (c *sqliteCache) AddMessage(m *message) error {
attachmentExpires, attachmentExpires,
attachmentURL, attachmentURL,
attachmentOwner, attachmentOwner,
m.Encoding,
published, published,
) )
return err return err
@ -278,8 +284,25 @@ func readMessages(rows *sql.Rows) ([]*message, error) {
for rows.Next() { for rows.Next() {
var timestamp, attachmentSize, attachmentExpires int64 var timestamp, attachmentSize, attachmentExpires int64
var priority int var priority int
var id, topic, msg, title, tagsStr, click, attachmentName, attachmentType, attachmentURL, attachmentOwner string var id, topic, msg, title, tagsStr, click, attachmentName, attachmentType, attachmentURL, attachmentOwner, encoding string
if err := rows.Scan(&id, &timestamp, &topic, &msg, &title, &priority, &tagsStr, &click, &attachmentName, &attachmentType, &attachmentSize, &attachmentExpires, &attachmentURL, &attachmentOwner); err != nil { err := rows.Scan(
&id,
&timestamp,
&topic,
&msg,
&title,
&priority,
&tagsStr,
&click,
&attachmentName,
&attachmentType,
&attachmentSize,
&attachmentExpires,
&attachmentURL,
&attachmentOwner,
&encoding,
)
if err != nil {
return nil, err return nil, err
} }
var tags []string var tags []string
@ -308,6 +331,7 @@ func readMessages(rows *sql.Rows) ([]*message, error) {
Tags: tags, Tags: tags,
Click: click, Click: click,
Attachment: att, Attachment: att,
Encoding: encoding,
}) })
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
@ -347,6 +371,8 @@ func setupDB(db *sql.DB) error {
return migrateFrom1(db) return migrateFrom1(db)
} else if schemaVersion == 2 { } else if schemaVersion == 2 {
return migrateFrom2(db) return migrateFrom2(db)
} else if schemaVersion == 3 {
return migrateFrom3(db)
} }
return fmt.Errorf("unexpected schema version found: %d", schemaVersion) return fmt.Errorf("unexpected schema version found: %d", schemaVersion)
} }
@ -397,5 +423,16 @@ func migrateFrom2(db *sql.DB) error {
if _, err := db.Exec(updateSchemaVersion, 3); err != nil { if _, err := db.Exec(updateSchemaVersion, 3); err != nil {
return err return err
} }
return migrateFrom3(db)
}
func migrateFrom3(db *sql.DB) error {
log.Print("Migrating cache database schema: from 3 to 4")
if _, err := db.Exec(migrate3To4AlterMessagesTableQuery); err != nil {
return err
}
if _, err := db.Exec(updateSchemaVersion, 4); err != nil {
return err
}
return nil // Update this when a new version is added return nil // Update this when a new version is added
} }

View File

@ -625,7 +625,7 @@ func TestServer_UnifiedPushDiscovery(t *testing.T) {
require.Equal(t, `{"unifiedpush":{"version":1}}`+"\n", response.Body.String()) require.Equal(t, `{"unifiedpush":{"version":1}}`+"\n", response.Body.String())
} }
func TestServer_PublishUnifiedPushBinary(t *testing.T) { func TestServer_PublishUnifiedPushBinary_AndPoll(t *testing.T) {
b := make([]byte, 12) // Max length b := make([]byte, 12) // Max length
_, err := rand.Read(b) _, err := rand.Read(b)
require.Nil(t, err) require.Nil(t, err)
@ -639,6 +639,14 @@ func TestServer_PublishUnifiedPushBinary(t *testing.T) {
b2, err := base64.StdEncoding.DecodeString(m.Message) b2, err := base64.StdEncoding.DecodeString(m.Message)
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, b, b2) require.Equal(t, b, b2)
response = request(t, s, "GET", "/mytopic/json?poll=1", string(b), nil)
require.Equal(t, 200, response.Code)
m = toMessage(t, response.Body.String())
require.Equal(t, "base64", m.Encoding)
b2, err = base64.StdEncoding.DecodeString(m.Message)
require.Nil(t, err)
require.Equal(t, b, b2)
} }
func TestServer_PublishUnifiedPushBinary_Truncated(t *testing.T) { func TestServer_PublishUnifiedPushBinary_Truncated(t *testing.T) {