Skip to content

Commit 0ea7897

Browse files
authored
refactor: sqlite versioning (#3870)
1 parent 7283a54 commit 0ea7897

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

lib/cache/sqlite-cache-store.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const { DatabaseSync } = require('node:sqlite')
44
const { Writable } = require('stream')
55
const { assertCacheKey, assertCacheValue } = require('../util/cache.js')
66

7+
const VERSION = 1
8+
79
/**
810
* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore
911
* @implements {CacheStore}
@@ -94,7 +96,7 @@ class SqliteCacheStore {
9496
this.#db = new DatabaseSync(opts?.location ?? ':memory:')
9597

9698
this.#db.exec(`
97-
CREATE TABLE IF NOT EXISTS cacheInterceptorV1 (
99+
CREATE TABLE IF NOT EXISTS cacheInterceptorV${VERSION} (
98100
-- Data specific to us
99101
id INTEGER PRIMARY KEY AUTOINCREMENT,
100102
url TEXT NOT NULL,
@@ -112,9 +114,9 @@ class SqliteCacheStore {
112114
staleAt INTEGER NOT NULL
113115
);
114116
115-
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_url ON cacheInterceptorV1(url);
116-
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_method ON cacheInterceptorV1(method);
117-
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_deleteAt ON cacheInterceptorV1(deleteAt);
117+
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_url ON cacheInterceptorV${VERSION}(url);
118+
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_method ON cacheInterceptorV${VERSION}(method);
119+
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_deleteAt ON cacheInterceptorV${VERSION}(deleteAt);
118120
`)
119121

120122
this.#getValuesQuery = this.#db.prepare(`
@@ -129,7 +131,7 @@ class SqliteCacheStore {
129131
vary,
130132
cachedAt,
131133
staleAt
132-
FROM cacheInterceptorV1
134+
FROM cacheInterceptorV${VERSION}
133135
WHERE
134136
url = ?
135137
AND method = ?
@@ -138,7 +140,7 @@ class SqliteCacheStore {
138140
`)
139141

140142
this.#updateValueQuery = this.#db.prepare(`
141-
UPDATE cacheInterceptorV1 SET
143+
UPDATE cacheInterceptorV${VERSION} SET
142144
body = ?,
143145
deleteAt = ?,
144146
statusCode = ?,
@@ -153,7 +155,7 @@ class SqliteCacheStore {
153155
`)
154156

155157
this.#insertValueQuery = this.#db.prepare(`
156-
INSERT INTO cacheInterceptorV1 (
158+
INSERT INTO cacheInterceptorV${VERSION} (
157159
url,
158160
method,
159161
body,
@@ -170,25 +172,25 @@ class SqliteCacheStore {
170172
`)
171173

172174
this.#deleteByUrlQuery = this.#db.prepare(
173-
'DELETE FROM cacheInterceptorV1 WHERE url = ?'
175+
`DELETE FROM cacheInterceptorV${VERSION} WHERE url = ?`
174176
)
175177

176178
this.#countEntriesQuery = this.#db.prepare(
177-
'SELECT COUNT(*) AS total FROM cacheInterceptorV1'
179+
`SELECT COUNT(*) AS total FROM cacheInterceptorV${VERSION}`
178180
)
179181

180182
this.#deleteExpiredValuesQuery = this.#db.prepare(
181-
'DELETE FROM cacheInterceptorV1 WHERE deleteAt <= ?'
183+
`DELETE FROM cacheInterceptorV${VERSION} WHERE deleteAt <= ?`
182184
)
183185

184186
this.#deleteOldValuesQuery = this.#maxCount === Infinity
185187
? null
186188
: this.#db.prepare(`
187-
DELETE FROM cacheInterceptorV1
189+
DELETE FROM cacheInterceptorV${VERSION}
188190
WHERE id IN (
189191
SELECT
190192
id
191-
FROM cacheInterceptorV1
193+
FROM cacheInterceptorV${VERSION}
192194
ORDER BY cachedAt DESC
193195
LIMIT ?
194196
)

0 commit comments

Comments
 (0)