From 0ea7897f303aef84b10b23710a5a4d14992096ff Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 22 Nov 2024 17:53:29 +0100 Subject: [PATCH] refactor: sqlite versioning (#3870) --- lib/cache/sqlite-cache-store.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/cache/sqlite-cache-store.js b/lib/cache/sqlite-cache-store.js index e0407fc0403..a9184c3c49c 100644 --- a/lib/cache/sqlite-cache-store.js +++ b/lib/cache/sqlite-cache-store.js @@ -4,6 +4,8 @@ const { DatabaseSync } = require('node:sqlite') const { Writable } = require('stream') const { assertCacheKey, assertCacheValue } = require('../util/cache.js') +const VERSION = 1 + /** * @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore * @implements {CacheStore} @@ -94,7 +96,7 @@ class SqliteCacheStore { this.#db = new DatabaseSync(opts?.location ?? ':memory:') this.#db.exec(` - CREATE TABLE IF NOT EXISTS cacheInterceptorV1 ( + CREATE TABLE IF NOT EXISTS cacheInterceptorV${VERSION} ( -- Data specific to us id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT NOT NULL, @@ -112,9 +114,9 @@ class SqliteCacheStore { staleAt INTEGER NOT NULL ); - CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_url ON cacheInterceptorV1(url); - CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_method ON cacheInterceptorV1(method); - CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_deleteAt ON cacheInterceptorV1(deleteAt); + CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_url ON cacheInterceptorV${VERSION}(url); + CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_method ON cacheInterceptorV${VERSION}(method); + CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_deleteAt ON cacheInterceptorV${VERSION}(deleteAt); `) this.#getValuesQuery = this.#db.prepare(` @@ -129,7 +131,7 @@ class SqliteCacheStore { vary, cachedAt, staleAt - FROM cacheInterceptorV1 + FROM cacheInterceptorV${VERSION} WHERE url = ? AND method = ? @@ -138,7 +140,7 @@ class SqliteCacheStore { `) this.#updateValueQuery = this.#db.prepare(` - UPDATE cacheInterceptorV1 SET + UPDATE cacheInterceptorV${VERSION} SET body = ?, deleteAt = ?, statusCode = ?, @@ -153,7 +155,7 @@ class SqliteCacheStore { `) this.#insertValueQuery = this.#db.prepare(` - INSERT INTO cacheInterceptorV1 ( + INSERT INTO cacheInterceptorV${VERSION} ( url, method, body, @@ -170,25 +172,25 @@ class SqliteCacheStore { `) this.#deleteByUrlQuery = this.#db.prepare( - 'DELETE FROM cacheInterceptorV1 WHERE url = ?' + `DELETE FROM cacheInterceptorV${VERSION} WHERE url = ?` ) this.#countEntriesQuery = this.#db.prepare( - 'SELECT COUNT(*) AS total FROM cacheInterceptorV1' + `SELECT COUNT(*) AS total FROM cacheInterceptorV${VERSION}` ) this.#deleteExpiredValuesQuery = this.#db.prepare( - 'DELETE FROM cacheInterceptorV1 WHERE deleteAt <= ?' + `DELETE FROM cacheInterceptorV${VERSION} WHERE deleteAt <= ?` ) this.#deleteOldValuesQuery = this.#maxCount === Infinity ? null : this.#db.prepare(` - DELETE FROM cacheInterceptorV1 + DELETE FROM cacheInterceptorV${VERSION} WHERE id IN ( SELECT id - FROM cacheInterceptorV1 + FROM cacheInterceptorV${VERSION} ORDER BY cachedAt DESC LIMIT ? )