message_cache.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. package server
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. _ "github.com/mattn/go-sqlite3" // SQLite driver
  8. "heckel.io/ntfy/log"
  9. "heckel.io/ntfy/util"
  10. "strings"
  11. "time"
  12. )
  13. var (
  14. errUnexpectedMessageType = errors.New("unexpected message type")
  15. )
  16. // Messages cache
  17. const (
  18. createMessagesTableQuery = `
  19. BEGIN;
  20. CREATE TABLE IF NOT EXISTS messages (
  21. id INTEGER PRIMARY KEY AUTOINCREMENT,
  22. mid TEXT NOT NULL,
  23. time INT NOT NULL,
  24. topic TEXT NOT NULL,
  25. message TEXT NOT NULL,
  26. title TEXT NOT NULL,
  27. priority INT NOT NULL,
  28. tags TEXT NOT NULL,
  29. click TEXT NOT NULL,
  30. icon TEXT NOT NULL,
  31. actions TEXT NOT NULL,
  32. attachment_name TEXT NOT NULL,
  33. attachment_type TEXT NOT NULL,
  34. attachment_size INT NOT NULL,
  35. attachment_expires INT NOT NULL,
  36. attachment_url TEXT NOT NULL,
  37. sender TEXT NOT NULL,
  38. encoding TEXT NOT NULL,
  39. published INT NOT NULL
  40. );
  41. CREATE INDEX IF NOT EXISTS idx_mid ON messages (mid);
  42. CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic);
  43. COMMIT;
  44. `
  45. insertMessageQuery = `
  46. INSERT INTO messages (mid, time, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, published)
  47. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  48. `
  49. pruneMessagesQuery = `DELETE FROM messages WHERE time < ? AND published = 1`
  50. selectRowIDFromMessageID = `SELECT id FROM messages WHERE mid = ?` // Do not include topic, see #336 and TestServer_PollSinceID_MultipleTopics
  51. selectMessagesSinceTimeQuery = `
  52. SELECT mid, time, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding
  53. FROM messages
  54. WHERE topic = ? AND time >= ? AND published = 1
  55. ORDER BY time, id
  56. `
  57. selectMessagesSinceTimeIncludeScheduledQuery = `
  58. SELECT mid, time, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding
  59. FROM messages
  60. WHERE topic = ? AND time >= ?
  61. ORDER BY time, id
  62. `
  63. selectMessagesSinceIDQuery = `
  64. SELECT mid, time, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding
  65. FROM messages
  66. WHERE topic = ? AND id > ? AND published = 1
  67. ORDER BY time, id
  68. `
  69. selectMessagesSinceIDIncludeScheduledQuery = `
  70. SELECT mid, time, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding
  71. FROM messages
  72. WHERE topic = ? AND (id > ? OR published = 0)
  73. ORDER BY time, id
  74. `
  75. selectMessagesDueQuery = `
  76. SELECT mid, time, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding
  77. FROM messages
  78. WHERE time <= ? AND published = 0
  79. ORDER BY time, id
  80. `
  81. updateMessagePublishedQuery = `UPDATE messages SET published = 1 WHERE mid = ?`
  82. selectMessagesCountQuery = `SELECT COUNT(*) FROM messages`
  83. selectMessageCountPerTopicQuery = `SELECT topic, COUNT(*) FROM messages GROUP BY topic`
  84. selectTopicsQuery = `SELECT topic FROM messages GROUP BY topic`
  85. selectAttachmentsSizeQuery = `SELECT IFNULL(SUM(attachment_size), 0) FROM messages WHERE sender = ? AND attachment_expires >= ?`
  86. )
  87. // Schema management queries
  88. const (
  89. currentSchemaVersion = 8
  90. createSchemaVersionTableQuery = `
  91. CREATE TABLE IF NOT EXISTS schemaVersion (
  92. id INT PRIMARY KEY,
  93. version INT NOT NULL
  94. );
  95. `
  96. insertSchemaVersion = `INSERT INTO schemaVersion VALUES (1, ?)`
  97. updateSchemaVersion = `UPDATE schemaVersion SET version = ? WHERE id = 1`
  98. selectSchemaVersionQuery = `SELECT version FROM schemaVersion WHERE id = 1`
  99. // 0 -> 1
  100. migrate0To1AlterMessagesTableQuery = `
  101. BEGIN;
  102. ALTER TABLE messages ADD COLUMN title TEXT NOT NULL DEFAULT('');
  103. ALTER TABLE messages ADD COLUMN priority INT NOT NULL DEFAULT(0);
  104. ALTER TABLE messages ADD COLUMN tags TEXT NOT NULL DEFAULT('');
  105. COMMIT;
  106. `
  107. // 1 -> 2
  108. migrate1To2AlterMessagesTableQuery = `
  109. ALTER TABLE messages ADD COLUMN published INT NOT NULL DEFAULT(1);
  110. `
  111. // 2 -> 3
  112. migrate2To3AlterMessagesTableQuery = `
  113. BEGIN;
  114. ALTER TABLE messages ADD COLUMN click TEXT NOT NULL DEFAULT('');
  115. ALTER TABLE messages ADD COLUMN attachment_name TEXT NOT NULL DEFAULT('');
  116. ALTER TABLE messages ADD COLUMN attachment_type TEXT NOT NULL DEFAULT('');
  117. ALTER TABLE messages ADD COLUMN attachment_size INT NOT NULL DEFAULT('0');
  118. ALTER TABLE messages ADD COLUMN attachment_expires INT NOT NULL DEFAULT('0');
  119. ALTER TABLE messages ADD COLUMN attachment_owner TEXT NOT NULL DEFAULT('');
  120. ALTER TABLE messages ADD COLUMN attachment_url TEXT NOT NULL DEFAULT('');
  121. COMMIT;
  122. `
  123. // 3 -> 4
  124. migrate3To4AlterMessagesTableQuery = `
  125. ALTER TABLE messages ADD COLUMN encoding TEXT NOT NULL DEFAULT('');
  126. `
  127. // 4 -> 5
  128. migrate4To5AlterMessagesTableQuery = `
  129. BEGIN;
  130. CREATE TABLE IF NOT EXISTS messages_new (
  131. id INTEGER PRIMARY KEY AUTOINCREMENT,
  132. mid TEXT NOT NULL,
  133. time INT NOT NULL,
  134. topic TEXT NOT NULL,
  135. message TEXT NOT NULL,
  136. title TEXT NOT NULL,
  137. priority INT NOT NULL,
  138. tags TEXT NOT NULL,
  139. click TEXT NOT NULL,
  140. attachment_name TEXT NOT NULL,
  141. attachment_type TEXT NOT NULL,
  142. attachment_size INT NOT NULL,
  143. attachment_expires INT NOT NULL,
  144. attachment_url TEXT NOT NULL,
  145. attachment_owner TEXT NOT NULL,
  146. encoding TEXT NOT NULL,
  147. published INT NOT NULL
  148. );
  149. CREATE INDEX IF NOT EXISTS idx_mid ON messages_new (mid);
  150. CREATE INDEX IF NOT EXISTS idx_topic ON messages_new (topic);
  151. INSERT
  152. INTO messages_new (
  153. mid, time, topic, message, title, priority, tags, click, attachment_name, attachment_type,
  154. attachment_size, attachment_expires, attachment_url, attachment_owner, encoding, published)
  155. SELECT
  156. id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type,
  157. attachment_size, attachment_expires, attachment_url, attachment_owner, encoding, published
  158. FROM messages;
  159. DROP TABLE messages;
  160. ALTER TABLE messages_new RENAME TO messages;
  161. COMMIT;
  162. `
  163. // 5 -> 6
  164. migrate5To6AlterMessagesTableQuery = `
  165. ALTER TABLE messages ADD COLUMN actions TEXT NOT NULL DEFAULT('');
  166. `
  167. // 6 -> 7
  168. migrate6To7AlterMessagesTableQuery = `
  169. ALTER TABLE messages RENAME COLUMN attachment_owner TO sender;
  170. `
  171. // 7 -> 8
  172. migrate7To8AlterMessagesTableQuery = `
  173. ALTER TABLE messages ADD COLUMN icon TEXT NOT NULL DEFAULT('');
  174. `
  175. )
  176. type messageCache struct {
  177. db *sql.DB
  178. nop bool
  179. }
  180. // newSqliteCache creates a SQLite file-backed cache
  181. func newSqliteCache(filename, startupQueries string, nop bool) (*messageCache, error) {
  182. db, err := sql.Open("sqlite3", filename)
  183. if err != nil {
  184. return nil, err
  185. }
  186. if err := setupCacheDB(db, startupQueries); err != nil {
  187. return nil, err
  188. }
  189. return &messageCache{
  190. db: db,
  191. nop: nop,
  192. }, nil
  193. }
  194. // newMemCache creates an in-memory cache
  195. func newMemCache() (*messageCache, error) {
  196. return newSqliteCache(createMemoryFilename(), "", false)
  197. }
  198. // newNopCache creates an in-memory cache that discards all messages;
  199. // it is always empty and can be used if caching is entirely disabled
  200. func newNopCache() (*messageCache, error) {
  201. return newSqliteCache(createMemoryFilename(), "", true)
  202. }
  203. // createMemoryFilename creates a unique memory filename to use for the SQLite backend.
  204. // From mattn/go-sqlite3: "Each connection to ":memory:" opens a brand new in-memory
  205. // sql database, so if the stdlib's sql engine happens to open another connection and
  206. // you've only specified ":memory:", that connection will see a brand new database.
  207. // A workaround is to use "file::memory:?cache=shared" (or "file:foobar?mode=memory&cache=shared").
  208. // Every connection to this string will point to the same in-memory database."
  209. func createMemoryFilename() string {
  210. return fmt.Sprintf("file:%s?mode=memory&cache=shared", util.RandomString(10))
  211. }
  212. func (c *messageCache) AddMessage(m *message) error {
  213. return c.addMessages([]*message{m})
  214. }
  215. func (c *messageCache) addMessages(ms []*message) error {
  216. if c.nop {
  217. return nil
  218. }
  219. tx, err := c.db.Begin()
  220. if err != nil {
  221. return err
  222. }
  223. defer tx.Rollback()
  224. for _, m := range ms {
  225. if m.Event != messageEvent {
  226. return errUnexpectedMessageType
  227. }
  228. published := m.Time <= time.Now().Unix()
  229. tags := strings.Join(m.Tags, ",")
  230. var attachmentName, attachmentType, attachmentURL string
  231. var attachmentSize, attachmentExpires int64
  232. if m.Attachment != nil {
  233. attachmentName = m.Attachment.Name
  234. attachmentType = m.Attachment.Type
  235. attachmentSize = m.Attachment.Size
  236. attachmentExpires = m.Attachment.Expires
  237. attachmentURL = m.Attachment.URL
  238. }
  239. var actionsStr string
  240. if len(m.Actions) > 0 {
  241. actionsBytes, err := json.Marshal(m.Actions)
  242. if err != nil {
  243. return err
  244. }
  245. actionsStr = string(actionsBytes)
  246. }
  247. _, err := tx.Exec(
  248. insertMessageQuery,
  249. m.ID,
  250. m.Time,
  251. m.Topic,
  252. m.Message,
  253. m.Title,
  254. m.Priority,
  255. tags,
  256. m.Click,
  257. m.Icon,
  258. actionsStr,
  259. attachmentName,
  260. attachmentType,
  261. attachmentSize,
  262. attachmentExpires,
  263. attachmentURL,
  264. m.Sender,
  265. m.Encoding,
  266. published,
  267. )
  268. if err != nil {
  269. return err
  270. }
  271. }
  272. return tx.Commit()
  273. }
  274. func (c *messageCache) Messages(topic string, since sinceMarker, scheduled bool) ([]*message, error) {
  275. if since.IsNone() {
  276. return make([]*message, 0), nil
  277. } else if since.IsID() {
  278. return c.messagesSinceID(topic, since, scheduled)
  279. }
  280. return c.messagesSinceTime(topic, since, scheduled)
  281. }
  282. func (c *messageCache) messagesSinceTime(topic string, since sinceMarker, scheduled bool) ([]*message, error) {
  283. var rows *sql.Rows
  284. var err error
  285. if scheduled {
  286. rows, err = c.db.Query(selectMessagesSinceTimeIncludeScheduledQuery, topic, since.Time().Unix())
  287. } else {
  288. rows, err = c.db.Query(selectMessagesSinceTimeQuery, topic, since.Time().Unix())
  289. }
  290. if err != nil {
  291. return nil, err
  292. }
  293. return readMessages(rows)
  294. }
  295. func (c *messageCache) messagesSinceID(topic string, since sinceMarker, scheduled bool) ([]*message, error) {
  296. idrows, err := c.db.Query(selectRowIDFromMessageID, since.ID())
  297. if err != nil {
  298. return nil, err
  299. }
  300. defer idrows.Close()
  301. if !idrows.Next() {
  302. return c.messagesSinceTime(topic, sinceAllMessages, scheduled)
  303. }
  304. var rowID int64
  305. if err := idrows.Scan(&rowID); err != nil {
  306. return nil, err
  307. }
  308. idrows.Close()
  309. var rows *sql.Rows
  310. if scheduled {
  311. rows, err = c.db.Query(selectMessagesSinceIDIncludeScheduledQuery, topic, rowID)
  312. } else {
  313. rows, err = c.db.Query(selectMessagesSinceIDQuery, topic, rowID)
  314. }
  315. if err != nil {
  316. return nil, err
  317. }
  318. return readMessages(rows)
  319. }
  320. func (c *messageCache) MessagesDue() ([]*message, error) {
  321. rows, err := c.db.Query(selectMessagesDueQuery, time.Now().Unix())
  322. if err != nil {
  323. return nil, err
  324. }
  325. return readMessages(rows)
  326. }
  327. func (c *messageCache) MarkPublished(m *message) error {
  328. _, err := c.db.Exec(updateMessagePublishedQuery, m.ID)
  329. return err
  330. }
  331. func (c *messageCache) MessageCounts() (map[string]int, error) {
  332. rows, err := c.db.Query(selectMessageCountPerTopicQuery)
  333. if err != nil {
  334. return nil, err
  335. }
  336. defer rows.Close()
  337. var topic string
  338. var count int
  339. counts := make(map[string]int)
  340. for rows.Next() {
  341. if err := rows.Scan(&topic, &count); err != nil {
  342. return nil, err
  343. } else if err := rows.Err(); err != nil {
  344. return nil, err
  345. }
  346. counts[topic] = count
  347. }
  348. return counts, nil
  349. }
  350. func (c *messageCache) Topics() (map[string]*topic, error) {
  351. rows, err := c.db.Query(selectTopicsQuery)
  352. if err != nil {
  353. return nil, err
  354. }
  355. defer rows.Close()
  356. topics := make(map[string]*topic)
  357. for rows.Next() {
  358. var id string
  359. if err := rows.Scan(&id); err != nil {
  360. return nil, err
  361. }
  362. topics[id] = newTopic(id)
  363. }
  364. if err := rows.Err(); err != nil {
  365. return nil, err
  366. }
  367. return topics, nil
  368. }
  369. func (c *messageCache) Prune(olderThan time.Time) error {
  370. _, err := c.db.Exec(pruneMessagesQuery, olderThan.Unix())
  371. return err
  372. }
  373. func (c *messageCache) AttachmentBytesUsed(sender string) (int64, error) {
  374. rows, err := c.db.Query(selectAttachmentsSizeQuery, sender, time.Now().Unix())
  375. if err != nil {
  376. return 0, err
  377. }
  378. defer rows.Close()
  379. var size int64
  380. if !rows.Next() {
  381. return 0, errors.New("no rows found")
  382. }
  383. if err := rows.Scan(&size); err != nil {
  384. return 0, err
  385. } else if err := rows.Err(); err != nil {
  386. return 0, err
  387. }
  388. return size, nil
  389. }
  390. func readMessages(rows *sql.Rows) ([]*message, error) {
  391. defer rows.Close()
  392. messages := make([]*message, 0)
  393. for rows.Next() {
  394. var timestamp, attachmentSize, attachmentExpires int64
  395. var priority int
  396. var id, topic, msg, title, tagsStr, click, icon, actionsStr, attachmentName, attachmentType, attachmentURL, sender, encoding string
  397. err := rows.Scan(
  398. &id,
  399. &timestamp,
  400. &topic,
  401. &msg,
  402. &title,
  403. &priority,
  404. &tagsStr,
  405. &click,
  406. &icon,
  407. &actionsStr,
  408. &attachmentName,
  409. &attachmentType,
  410. &attachmentSize,
  411. &attachmentExpires,
  412. &attachmentURL,
  413. &sender,
  414. &encoding,
  415. )
  416. if err != nil {
  417. return nil, err
  418. }
  419. var tags []string
  420. if tagsStr != "" {
  421. tags = strings.Split(tagsStr, ",")
  422. }
  423. var actions []*action
  424. if actionsStr != "" {
  425. if err := json.Unmarshal([]byte(actionsStr), &actions); err != nil {
  426. return nil, err
  427. }
  428. }
  429. var att *attachment
  430. if attachmentName != "" && attachmentURL != "" {
  431. att = &attachment{
  432. Name: attachmentName,
  433. Type: attachmentType,
  434. Size: attachmentSize,
  435. Expires: attachmentExpires,
  436. URL: attachmentURL,
  437. }
  438. }
  439. messages = append(messages, &message{
  440. ID: id,
  441. Time: timestamp,
  442. Event: messageEvent,
  443. Topic: topic,
  444. Message: msg,
  445. Title: title,
  446. Priority: priority,
  447. Tags: tags,
  448. Click: click,
  449. Icon: icon,
  450. Actions: actions,
  451. Attachment: att,
  452. Sender: sender,
  453. Encoding: encoding,
  454. })
  455. }
  456. if err := rows.Err(); err != nil {
  457. return nil, err
  458. }
  459. return messages, nil
  460. }
  461. func setupCacheDB(db *sql.DB, startupQueries string) error {
  462. // Run startup queries
  463. if startupQueries != "" {
  464. if _, err := db.Exec(startupQueries); err != nil {
  465. return err
  466. }
  467. }
  468. // If 'messages' table does not exist, this must be a new database
  469. rowsMC, err := db.Query(selectMessagesCountQuery)
  470. if err != nil {
  471. return setupNewCacheDB(db)
  472. }
  473. rowsMC.Close()
  474. // If 'messages' table exists, check 'schemaVersion' table
  475. schemaVersion := 0
  476. rowsSV, err := db.Query(selectSchemaVersionQuery)
  477. if err == nil {
  478. defer rowsSV.Close()
  479. if !rowsSV.Next() {
  480. return errors.New("cannot determine schema version: cache file may be corrupt")
  481. }
  482. if err := rowsSV.Scan(&schemaVersion); err != nil {
  483. return err
  484. }
  485. rowsSV.Close()
  486. }
  487. // Do migrations
  488. if schemaVersion == currentSchemaVersion {
  489. return nil
  490. } else if schemaVersion == 0 {
  491. return migrateFrom0(db)
  492. } else if schemaVersion == 1 {
  493. return migrateFrom1(db)
  494. } else if schemaVersion == 2 {
  495. return migrateFrom2(db)
  496. } else if schemaVersion == 3 {
  497. return migrateFrom3(db)
  498. } else if schemaVersion == 4 {
  499. return migrateFrom4(db)
  500. } else if schemaVersion == 5 {
  501. return migrateFrom5(db)
  502. } else if schemaVersion == 6 {
  503. return migrateFrom6(db)
  504. } else if schemaVersion == 7 {
  505. return migrateFrom7(db)
  506. }
  507. return fmt.Errorf("unexpected schema version found: %d", schemaVersion)
  508. }
  509. func setupNewCacheDB(db *sql.DB) error {
  510. if _, err := db.Exec(createMessagesTableQuery); err != nil {
  511. return err
  512. }
  513. if _, err := db.Exec(createSchemaVersionTableQuery); err != nil {
  514. return err
  515. }
  516. if _, err := db.Exec(insertSchemaVersion, currentSchemaVersion); err != nil {
  517. return err
  518. }
  519. return nil
  520. }
  521. func migrateFrom0(db *sql.DB) error {
  522. log.Info("Migrating cache database schema: from 0 to 1")
  523. if _, err := db.Exec(migrate0To1AlterMessagesTableQuery); err != nil {
  524. return err
  525. }
  526. if _, err := db.Exec(createSchemaVersionTableQuery); err != nil {
  527. return err
  528. }
  529. if _, err := db.Exec(insertSchemaVersion, 1); err != nil {
  530. return err
  531. }
  532. return migrateFrom1(db)
  533. }
  534. func migrateFrom1(db *sql.DB) error {
  535. log.Info("Migrating cache database schema: from 1 to 2")
  536. if _, err := db.Exec(migrate1To2AlterMessagesTableQuery); err != nil {
  537. return err
  538. }
  539. if _, err := db.Exec(updateSchemaVersion, 2); err != nil {
  540. return err
  541. }
  542. return migrateFrom2(db)
  543. }
  544. func migrateFrom2(db *sql.DB) error {
  545. log.Info("Migrating cache database schema: from 2 to 3")
  546. if _, err := db.Exec(migrate2To3AlterMessagesTableQuery); err != nil {
  547. return err
  548. }
  549. if _, err := db.Exec(updateSchemaVersion, 3); err != nil {
  550. return err
  551. }
  552. return migrateFrom3(db)
  553. }
  554. func migrateFrom3(db *sql.DB) error {
  555. log.Info("Migrating cache database schema: from 3 to 4")
  556. if _, err := db.Exec(migrate3To4AlterMessagesTableQuery); err != nil {
  557. return err
  558. }
  559. if _, err := db.Exec(updateSchemaVersion, 4); err != nil {
  560. return err
  561. }
  562. return migrateFrom4(db)
  563. }
  564. func migrateFrom4(db *sql.DB) error {
  565. log.Info("Migrating cache database schema: from 4 to 5")
  566. if _, err := db.Exec(migrate4To5AlterMessagesTableQuery); err != nil {
  567. return err
  568. }
  569. if _, err := db.Exec(updateSchemaVersion, 5); err != nil {
  570. return err
  571. }
  572. return migrateFrom5(db)
  573. }
  574. func migrateFrom5(db *sql.DB) error {
  575. log.Info("Migrating cache database schema: from 5 to 6")
  576. if _, err := db.Exec(migrate5To6AlterMessagesTableQuery); err != nil {
  577. return err
  578. }
  579. if _, err := db.Exec(updateSchemaVersion, 6); err != nil {
  580. return err
  581. }
  582. return migrateFrom6(db)
  583. }
  584. func migrateFrom6(db *sql.DB) error {
  585. log.Info("Migrating cache database schema: from 6 to 7")
  586. if _, err := db.Exec(migrate6To7AlterMessagesTableQuery); err != nil {
  587. return err
  588. }
  589. if _, err := db.Exec(updateSchemaVersion, 7); err != nil {
  590. return err
  591. }
  592. return migrateFrom7(db)
  593. }
  594. func migrateFrom7(db *sql.DB) error {
  595. log.Info("Migrating cache database schema: from 7 to 8")
  596. if _, err := db.Exec(migrate7To8AlterMessagesTableQuery); err != nil {
  597. return err
  598. }
  599. if _, err := db.Exec(updateSchemaVersion, 8); err != nil {
  600. return err
  601. }
  602. return nil // Update this when a new version is added
  603. }