message_cache_sqlite.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. package server
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "time"
  7. _ "github.com/mattn/go-sqlite3" // SQLite driver
  8. "heckel.io/ntfy/v2/log"
  9. "heckel.io/ntfy/v2/util"
  10. )
  11. var (
  12. errUnexpectedMessageType = errors.New("unexpected message type")
  13. errMessageNotFound = errors.New("message not found")
  14. errNoRows = errors.New("no rows found")
  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. expires INT NOT NULL,
  25. topic TEXT NOT NULL,
  26. message TEXT NOT NULL,
  27. title TEXT NOT NULL,
  28. priority INT NOT NULL,
  29. tags TEXT NOT NULL,
  30. click TEXT NOT NULL,
  31. icon TEXT NOT NULL,
  32. actions TEXT NOT NULL,
  33. attachment_name TEXT NOT NULL,
  34. attachment_type TEXT NOT NULL,
  35. attachment_size INT NOT NULL,
  36. attachment_expires INT NOT NULL,
  37. attachment_url TEXT NOT NULL,
  38. attachment_deleted INT NOT NULL,
  39. sender TEXT NOT NULL,
  40. user TEXT NOT NULL,
  41. content_type TEXT NOT NULL,
  42. encoding TEXT NOT NULL,
  43. published INT NOT NULL
  44. );
  45. CREATE INDEX IF NOT EXISTS idx_mid ON messages (mid);
  46. CREATE INDEX IF NOT EXISTS idx_time ON messages (time);
  47. CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic);
  48. CREATE INDEX IF NOT EXISTS idx_expires ON messages (expires);
  49. CREATE INDEX IF NOT EXISTS idx_sender ON messages (sender);
  50. CREATE INDEX IF NOT EXISTS idx_user ON messages (user);
  51. CREATE INDEX IF NOT EXISTS idx_attachment_expires ON messages (attachment_expires);
  52. CREATE TABLE IF NOT EXISTS stats (
  53. key TEXT PRIMARY KEY,
  54. value INT
  55. );
  56. INSERT INTO stats (key, value) VALUES ('messages', 0);
  57. COMMIT;
  58. `
  59. )
  60. var (
  61. sqliteMessageCacheQueries = &messageCacheQueries{
  62. insertMessage: `
  63. INSERT INTO messages (mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, attachment_deleted, sender, user, content_type, encoding, published)
  64. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  65. `,
  66. deleteMessage: `DELETE FROM messages WHERE mid = ?`,
  67. updateMessagesForTopicExpiry: `UPDATE messages SET expires = ? WHERE topic = ?`,
  68. selectRowIDFromMessageID: `SELECT id FROM messages WHERE mid = ?`, // Do not include topic, see #336 and TestServer_PollSinceID_MultipleTopics
  69. selectMessagesByID: `
  70. SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
  71. FROM messages
  72. WHERE mid = ?
  73. `,
  74. selectMessagesSinceTime: `
  75. SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
  76. FROM messages
  77. WHERE topic = ? AND time >= ? AND published = 1
  78. ORDER BY time, id
  79. `,
  80. selectMessagesSinceTimeIncludeScheduled: `
  81. SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
  82. FROM messages
  83. WHERE topic = ? AND time >= ?
  84. ORDER BY time, id
  85. `,
  86. selectMessagesSinceID: `
  87. SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
  88. FROM messages
  89. WHERE topic = ? AND id > ? AND published = 1
  90. ORDER BY time, id
  91. `,
  92. selectMessagesSinceIDIncludeScheduled: `
  93. SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
  94. FROM messages
  95. WHERE topic = ? AND (id > ? OR published = 0)
  96. ORDER BY time, id
  97. `,
  98. selectMessagesDue: `
  99. SELECT mid, time, expires, topic, message, title, priority, tags, click, icon, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, user, content_type, encoding
  100. FROM messages
  101. WHERE time <= ? AND published = 0
  102. ORDER BY time, id
  103. `,
  104. selectMessagesExpired: `SELECT mid FROM messages WHERE expires <= ? AND published = 1`,
  105. updateMessagePublished: `UPDATE messages SET published = 1 WHERE mid = ?`,
  106. selectMessageCountPerTopic: `SELECT topic, COUNT(*) FROM messages GROUP BY topic`,
  107. selectTopics: `SELECT topic FROM messages GROUP BY topic`,
  108. updateAttachmentDeleted: `UPDATE messages SET attachment_deleted = 1 WHERE mid = ?`,
  109. selectAttachmentsExpired: `SELECT mid FROM messages WHERE attachment_expires > 0 AND attachment_expires <= ? AND attachment_deleted = 0`,
  110. selectAttachmentsSizeBySender: `SELECT IFNULL(SUM(attachment_size), 0) FROM messages WHERE user = '' AND sender = ? AND attachment_expires >= ?`,
  111. selectAttachmentsSizeByUserID: `SELECT IFNULL(SUM(attachment_size), 0) FROM messages WHERE user = ? AND attachment_expires >= ?`,
  112. selectStats: `SELECT value FROM stats WHERE key = 'messages'`,
  113. updateStats: `UPDATE stats SET value = ? WHERE key = 'messages'`,
  114. }
  115. )
  116. // Schema management queries
  117. const (
  118. currentSchemaVersion = 12
  119. createSchemaVersionTableQuery = `
  120. CREATE TABLE IF NOT EXISTS schemaVersion (
  121. id INT PRIMARY KEY,
  122. version INT NOT NULL
  123. );
  124. `
  125. insertSchemaVersion = `INSERT INTO schemaVersion VALUES (1, ?)`
  126. updateSchemaVersion = `UPDATE schemaVersion SET version = ? WHERE id = 1`
  127. selectSchemaVersionQuery = `SELECT version FROM schemaVersion WHERE id = 1`
  128. selectMessagesCountQuery = `SELECT COUNT(*) FROM messages`
  129. // 0 -> 1
  130. migrate0To1AlterMessagesTableQuery = `
  131. BEGIN;
  132. ALTER TABLE messages ADD COLUMN title TEXT NOT NULL DEFAULT('');
  133. ALTER TABLE messages ADD COLUMN priority INT NOT NULL DEFAULT(0);
  134. ALTER TABLE messages ADD COLUMN tags TEXT NOT NULL DEFAULT('');
  135. COMMIT;
  136. `
  137. // 1 -> 2
  138. migrate1To2AlterMessagesTableQuery = `
  139. ALTER TABLE messages ADD COLUMN published INT NOT NULL DEFAULT(1);
  140. `
  141. // 2 -> 3
  142. migrate2To3AlterMessagesTableQuery = `
  143. BEGIN;
  144. ALTER TABLE messages ADD COLUMN click TEXT NOT NULL DEFAULT('');
  145. ALTER TABLE messages ADD COLUMN attachment_name TEXT NOT NULL DEFAULT('');
  146. ALTER TABLE messages ADD COLUMN attachment_type TEXT NOT NULL DEFAULT('');
  147. ALTER TABLE messages ADD COLUMN attachment_size INT NOT NULL DEFAULT('0');
  148. ALTER TABLE messages ADD COLUMN attachment_expires INT NOT NULL DEFAULT('0');
  149. ALTER TABLE messages ADD COLUMN attachment_owner TEXT NOT NULL DEFAULT('');
  150. ALTER TABLE messages ADD COLUMN attachment_url TEXT NOT NULL DEFAULT('');
  151. COMMIT;
  152. `
  153. // 3 -> 4
  154. migrate3To4AlterMessagesTableQuery = `
  155. ALTER TABLE messages ADD COLUMN encoding TEXT NOT NULL DEFAULT('');
  156. `
  157. // 4 -> 5
  158. migrate4To5AlterMessagesTableQuery = `
  159. BEGIN;
  160. CREATE TABLE IF NOT EXISTS messages_new (
  161. id INTEGER PRIMARY KEY AUTOINCREMENT,
  162. mid TEXT NOT NULL,
  163. time INT NOT NULL,
  164. topic TEXT NOT NULL,
  165. message TEXT NOT NULL,
  166. title TEXT NOT NULL,
  167. priority INT NOT NULL,
  168. tags TEXT NOT NULL,
  169. click TEXT NOT NULL,
  170. attachment_name TEXT NOT NULL,
  171. attachment_type TEXT NOT NULL,
  172. attachment_size INT NOT NULL,
  173. attachment_expires INT NOT NULL,
  174. attachment_url TEXT NOT NULL,
  175. attachment_owner TEXT NOT NULL,
  176. encoding TEXT NOT NULL,
  177. published INT NOT NULL
  178. );
  179. CREATE INDEX IF NOT EXISTS idx_mid ON messages_new (mid);
  180. CREATE INDEX IF NOT EXISTS idx_topic ON messages_new (topic);
  181. INSERT
  182. INTO messages_new (
  183. mid, time, topic, message, title, priority, tags, click, attachment_name, attachment_type,
  184. attachment_size, attachment_expires, attachment_url, attachment_owner, encoding, published)
  185. SELECT
  186. id, time, topic, message, title, priority, tags, click, attachment_name, attachment_type,
  187. attachment_size, attachment_expires, attachment_url, attachment_owner, encoding, published
  188. FROM messages;
  189. DROP TABLE messages;
  190. ALTER TABLE messages_new RENAME TO messages;
  191. COMMIT;
  192. `
  193. // 5 -> 6
  194. migrate5To6AlterMessagesTableQuery = `
  195. ALTER TABLE messages ADD COLUMN actions TEXT NOT NULL DEFAULT('');
  196. `
  197. // 6 -> 7
  198. migrate6To7AlterMessagesTableQuery = `
  199. ALTER TABLE messages RENAME COLUMN attachment_owner TO sender;
  200. `
  201. // 7 -> 8
  202. migrate7To8AlterMessagesTableQuery = `
  203. ALTER TABLE messages ADD COLUMN icon TEXT NOT NULL DEFAULT('');
  204. `
  205. // 8 -> 9
  206. migrate8To9AlterMessagesTableQuery = `
  207. CREATE INDEX IF NOT EXISTS idx_time ON messages (time);
  208. `
  209. // 9 -> 10
  210. migrate9To10AlterMessagesTableQuery = `
  211. ALTER TABLE messages ADD COLUMN user TEXT NOT NULL DEFAULT('');
  212. ALTER TABLE messages ADD COLUMN attachment_deleted INT NOT NULL DEFAULT('0');
  213. ALTER TABLE messages ADD COLUMN expires INT NOT NULL DEFAULT('0');
  214. CREATE INDEX IF NOT EXISTS idx_expires ON messages (expires);
  215. CREATE INDEX IF NOT EXISTS idx_sender ON messages (sender);
  216. CREATE INDEX IF NOT EXISTS idx_user ON messages (user);
  217. CREATE INDEX IF NOT EXISTS idx_attachment_expires ON messages (attachment_expires);
  218. `
  219. migrate9To10UpdateMessageExpiryQuery = `UPDATE messages SET expires = time + ?`
  220. // 10 -> 11
  221. migrate10To11AlterMessagesTableQuery = `
  222. CREATE TABLE IF NOT EXISTS stats (
  223. key TEXT PRIMARY KEY,
  224. value INT
  225. );
  226. INSERT INTO stats (key, value) VALUES ('messages', 0);
  227. `
  228. // 11 -> 12
  229. migrate11To12AlterMessagesTableQuery = `
  230. ALTER TABLE messages ADD COLUMN content_type TEXT NOT NULL DEFAULT('');
  231. `
  232. )
  233. var (
  234. migrations = map[int]func(db *sql.DB, cacheDuration time.Duration) error{
  235. 0: migrateFrom0,
  236. 1: migrateFrom1,
  237. 2: migrateFrom2,
  238. 3: migrateFrom3,
  239. 4: migrateFrom4,
  240. 5: migrateFrom5,
  241. 6: migrateFrom6,
  242. 7: migrateFrom7,
  243. 8: migrateFrom8,
  244. 9: migrateFrom9,
  245. 10: migrateFrom10,
  246. 11: migrateFrom11,
  247. }
  248. )
  249. type sqliteMessageCache struct {
  250. *commonMessageCache
  251. nop bool
  252. }
  253. var _ MessageCache = (*sqliteMessageCache)(nil)
  254. // newSqliteMessageCache creates a SQLite file-backed cache
  255. func newSqliteMessageCache(filename, startupQueries string, cacheDuration time.Duration, batchSize int, batchTimeout time.Duration, nop bool) (*sqliteMessageCache, error) {
  256. db, err := sql.Open("sqlite3", filename)
  257. if err != nil {
  258. return nil, err
  259. }
  260. if err := setupMessagesDB(db, startupQueries, cacheDuration); err != nil {
  261. return nil, err
  262. }
  263. var queue *util.BatchingQueue[*message]
  264. if batchSize > 0 || batchTimeout > 0 {
  265. queue = util.NewBatchingQueue[*message](batchSize, batchTimeout)
  266. }
  267. cache := &sqliteMessageCache{
  268. commonMessageCache: &commonMessageCache{
  269. db: db,
  270. queue: queue,
  271. queries: sqliteMessageCacheQueries,
  272. },
  273. nop: nop,
  274. }
  275. go cache.processMessageBatches()
  276. return cache, nil
  277. }
  278. // newMemCache creates an in-memory cache
  279. func newMemCache() (*sqliteMessageCache, error) {
  280. return newSqliteMessageCache(createMemoryFilename(), "", 0, 0, 0, false)
  281. }
  282. // newNopCache creates an in-memory cache that discards all messages;
  283. // it is always empty and can be used if caching is entirely disabled
  284. func newNopCache() (*sqliteMessageCache, error) {
  285. return newSqliteMessageCache(createMemoryFilename(), "", 0, 0, 0, true)
  286. }
  287. // createMemoryFilename creates a unique memory filename to use for the SQLite backend.
  288. // From mattn/go-sqlite3: "Each connection to ":memory:" opens a brand new in-memory
  289. // sql database, so if the stdlib's sql engine happens to open another connection and
  290. // you've only specified ":memory:", that connection will see a brand new database.
  291. // A workaround is to use "file::memory:?cache=shared" (or "file:foobar?mode=memory&cache=shared").
  292. // Every connection to this string will point to the same in-memory database."
  293. func createMemoryFilename() string {
  294. return fmt.Sprintf("file:%s?mode=memory&cache=shared", util.RandomString(10))
  295. }
  296. // AddMessage stores a message to the message cache synchronously, or queues it to be stored at a later date asyncronously.
  297. // The message is queued only if "batchSize" or "batchTimeout" are passed to the constructor.
  298. func (c *sqliteMessageCache) AddMessage(m *message) error {
  299. if c.nop {
  300. return nil
  301. }
  302. return c.commonMessageCache.AddMessage(m)
  303. }
  304. func setupMessagesDB(db *sql.DB, startupQueries string, cacheDuration time.Duration) error {
  305. // Run startup queries
  306. if startupQueries != "" {
  307. if _, err := db.Exec(startupQueries); err != nil {
  308. return err
  309. }
  310. }
  311. // If 'messages' table does not exist, this must be a new database
  312. rowsMC, err := db.Query(selectMessagesCountQuery)
  313. if err != nil {
  314. return setupNewCacheDB(db)
  315. }
  316. rowsMC.Close()
  317. // If 'messages' table exists, check 'schemaVersion' table
  318. schemaVersion := 0
  319. rowsSV, err := db.Query(selectSchemaVersionQuery)
  320. if err == nil {
  321. defer rowsSV.Close()
  322. if !rowsSV.Next() {
  323. return errors.New("cannot determine schema version: cache file may be corrupt")
  324. }
  325. if err := rowsSV.Scan(&schemaVersion); err != nil {
  326. return err
  327. }
  328. rowsSV.Close()
  329. }
  330. // Do migrations
  331. if schemaVersion == currentSchemaVersion {
  332. return nil
  333. } else if schemaVersion > currentSchemaVersion {
  334. return fmt.Errorf("unexpected schema version: version %d is higher than current version %d", schemaVersion, currentSchemaVersion)
  335. }
  336. for i := schemaVersion; i < currentSchemaVersion; i++ {
  337. fn, ok := migrations[i]
  338. if !ok {
  339. return fmt.Errorf("cannot find migration step from schema version %d to %d", i, i+1)
  340. } else if err := fn(db, cacheDuration); err != nil {
  341. return err
  342. }
  343. }
  344. return nil
  345. }
  346. func setupNewCacheDB(db *sql.DB) error {
  347. if _, err := db.Exec(createMessagesTableQuery); err != nil {
  348. return err
  349. }
  350. if _, err := db.Exec(createSchemaVersionTableQuery); err != nil {
  351. return err
  352. }
  353. if _, err := db.Exec(insertSchemaVersion, currentSchemaVersion); err != nil {
  354. return err
  355. }
  356. return nil
  357. }
  358. func migrateFrom0(db *sql.DB, _ time.Duration) error {
  359. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 0 to 1")
  360. if _, err := db.Exec(migrate0To1AlterMessagesTableQuery); err != nil {
  361. return err
  362. }
  363. if _, err := db.Exec(createSchemaVersionTableQuery); err != nil {
  364. return err
  365. }
  366. if _, err := db.Exec(insertSchemaVersion, 1); err != nil {
  367. return err
  368. }
  369. return nil
  370. }
  371. func migrateFrom1(db *sql.DB, _ time.Duration) error {
  372. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 1 to 2")
  373. if _, err := db.Exec(migrate1To2AlterMessagesTableQuery); err != nil {
  374. return err
  375. }
  376. if _, err := db.Exec(updateSchemaVersion, 2); err != nil {
  377. return err
  378. }
  379. return nil
  380. }
  381. func migrateFrom2(db *sql.DB, _ time.Duration) error {
  382. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 2 to 3")
  383. if _, err := db.Exec(migrate2To3AlterMessagesTableQuery); err != nil {
  384. return err
  385. }
  386. if _, err := db.Exec(updateSchemaVersion, 3); err != nil {
  387. return err
  388. }
  389. return nil
  390. }
  391. func migrateFrom3(db *sql.DB, _ time.Duration) error {
  392. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 3 to 4")
  393. if _, err := db.Exec(migrate3To4AlterMessagesTableQuery); err != nil {
  394. return err
  395. }
  396. if _, err := db.Exec(updateSchemaVersion, 4); err != nil {
  397. return err
  398. }
  399. return nil
  400. }
  401. func migrateFrom4(db *sql.DB, _ time.Duration) error {
  402. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 4 to 5")
  403. if _, err := db.Exec(migrate4To5AlterMessagesTableQuery); err != nil {
  404. return err
  405. }
  406. if _, err := db.Exec(updateSchemaVersion, 5); err != nil {
  407. return err
  408. }
  409. return nil
  410. }
  411. func migrateFrom5(db *sql.DB, _ time.Duration) error {
  412. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 5 to 6")
  413. if _, err := db.Exec(migrate5To6AlterMessagesTableQuery); err != nil {
  414. return err
  415. }
  416. if _, err := db.Exec(updateSchemaVersion, 6); err != nil {
  417. return err
  418. }
  419. return nil
  420. }
  421. func migrateFrom6(db *sql.DB, _ time.Duration) error {
  422. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 6 to 7")
  423. if _, err := db.Exec(migrate6To7AlterMessagesTableQuery); err != nil {
  424. return err
  425. }
  426. if _, err := db.Exec(updateSchemaVersion, 7); err != nil {
  427. return err
  428. }
  429. return nil
  430. }
  431. func migrateFrom7(db *sql.DB, _ time.Duration) error {
  432. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 7 to 8")
  433. if _, err := db.Exec(migrate7To8AlterMessagesTableQuery); err != nil {
  434. return err
  435. }
  436. if _, err := db.Exec(updateSchemaVersion, 8); err != nil {
  437. return err
  438. }
  439. return nil
  440. }
  441. func migrateFrom8(db *sql.DB, _ time.Duration) error {
  442. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 8 to 9")
  443. if _, err := db.Exec(migrate8To9AlterMessagesTableQuery); err != nil {
  444. return err
  445. }
  446. if _, err := db.Exec(updateSchemaVersion, 9); err != nil {
  447. return err
  448. }
  449. return nil
  450. }
  451. func migrateFrom9(db *sql.DB, cacheDuration time.Duration) error {
  452. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 9 to 10")
  453. tx, err := db.Begin()
  454. if err != nil {
  455. return err
  456. }
  457. defer tx.Rollback()
  458. if _, err := tx.Exec(migrate9To10AlterMessagesTableQuery); err != nil {
  459. return err
  460. }
  461. if _, err := tx.Exec(migrate9To10UpdateMessageExpiryQuery, int64(cacheDuration.Seconds())); err != nil {
  462. return err
  463. }
  464. if _, err := tx.Exec(updateSchemaVersion, 10); err != nil {
  465. return err
  466. }
  467. return tx.Commit()
  468. }
  469. func migrateFrom10(db *sql.DB, _ time.Duration) error {
  470. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 10 to 11")
  471. tx, err := db.Begin()
  472. if err != nil {
  473. return err
  474. }
  475. defer tx.Rollback()
  476. if _, err := tx.Exec(migrate10To11AlterMessagesTableQuery); err != nil {
  477. return err
  478. }
  479. if _, err := tx.Exec(updateSchemaVersion, 11); err != nil {
  480. return err
  481. }
  482. return tx.Commit()
  483. }
  484. func migrateFrom11(db *sql.DB, _ time.Duration) error {
  485. log.Tag(tagMessageCache).Info("Migrating cache database schema: from 11 to 12")
  486. tx, err := db.Begin()
  487. if err != nil {
  488. return err
  489. }
  490. defer tx.Rollback()
  491. if _, err := tx.Exec(migrate11To12AlterMessagesTableQuery); err != nil {
  492. return err
  493. }
  494. if _, err := tx.Exec(updateSchemaVersion, 12); err != nil {
  495. return err
  496. }
  497. return tx.Commit()
  498. }