message_cache.go 18 KB

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