1
+ import { hash } from 'node:crypto'
2
+
1
3
import { Kysely , Migrator } from 'kysely'
2
4
3
5
import type { DatabaseDriverName } from '~/lib/db/drivers'
@@ -9,6 +11,7 @@ import { logger } from '~/lib/logger'
9
11
import type { Selectable } from 'kysely'
10
12
11
13
export interface CacheKeysTable {
14
+ id : string
12
15
key : string
13
16
version : string
14
17
updated_at : string
@@ -18,11 +21,11 @@ export interface UploadsTable {
18
21
created_at : string
19
22
key : string
20
23
version : string
21
- id : number
24
+ id : string
22
25
driver_upload_id : string
23
26
}
24
27
export interface UploadPartsTable {
25
- upload_id : number
28
+ upload_id : string
26
29
part_number : number
27
30
e_tag : string | null
28
31
}
@@ -86,8 +89,7 @@ export async function findKeyMatch(
86
89
logger . debug ( 'Finding key match' , args )
87
90
const exactPrimaryMatch = await db
88
91
. selectFrom ( 'cache_keys' )
89
- . where ( 'key' , '=' , args . key )
90
- . where ( 'version' , '=' , args . version )
92
+ . where ( 'id' , '=' , getCacheKeyId ( args . key , args . version ) )
91
93
. selectAll ( )
92
94
. executeTakeFirst ( )
93
95
if ( exactPrimaryMatch ) {
@@ -117,8 +119,7 @@ export async function findKeyMatch(
117
119
for ( const key of args . restoreKeys ) {
118
120
const exactMatch = await db
119
121
. selectFrom ( 'cache_keys' )
120
- . where ( 'version' , '=' , args . version )
121
- . where ( 'key' , '=' , key )
122
+ . where ( 'id' , '=' , getCacheKeyId ( key , args . version ) )
122
123
. orderBy ( 'cache_keys.updated_at desc' )
123
124
. selectAll ( )
124
125
. executeTakeFirst ( )
@@ -161,8 +162,7 @@ export async function updateOrCreateKey(
161
162
. updateTable ( 'cache_keys' )
162
163
. set ( 'updated_at' , now . toISOString ( ) )
163
164
. set ( 'accessed_at' , now . toISOString ( ) )
164
- . where ( 'key' , '=' , key )
165
- . where ( 'version' , '=' , version )
165
+ . where ( 'id' , '=' , getCacheKeyId ( key , version ) )
166
166
. executeTakeFirst ( )
167
167
if ( Number ( updateResult . numUpdatedRows ) === 0 ) {
168
168
await createKey ( db , { key, version, date } )
@@ -177,8 +177,7 @@ export async function touchKey(
177
177
await db
178
178
. updateTable ( 'cache_keys' )
179
179
. set ( 'accessed_at' , now . toISOString ( ) )
180
- . where ( 'key' , '=' , key )
181
- . where ( 'version' , '=' , version )
180
+ . where ( 'id' , '=' , getCacheKeyId ( key , version ) )
182
181
. execute ( )
183
182
}
184
183
@@ -205,6 +204,7 @@ export async function createKey(
205
204
await db
206
205
. insertInto ( 'cache_keys' )
207
206
. values ( {
207
+ id : getCacheKeyId ( key , version ) ,
208
208
key,
209
209
version,
210
210
updated_at : now . toISOString ( ) ,
@@ -213,15 +213,15 @@ export async function createKey(
213
213
. execute ( )
214
214
}
215
215
216
+ function getCacheKeyId ( key : string , version : string ) {
217
+ return hash ( 'sha256' , Buffer . from ( `${ key } -${ version } ` ) )
218
+ }
219
+
216
220
export async function pruneKeys ( db : DB , keys ?: Selectable < CacheKeysTable > [ ] ) {
217
221
if ( keys ) {
218
222
await db . transaction ( ) . execute ( async ( tx ) => {
219
223
for ( const { key, version } of keys ?? [ ] ) {
220
- await tx
221
- . deleteFrom ( 'cache_keys' )
222
- . where ( 'key' , '=' , key )
223
- . where ( 'version' , '=' , version )
224
- . execute ( )
224
+ await tx . deleteFrom ( 'cache_keys' ) . where ( 'id' , '=' , getCacheKeyId ( key , version ) ) . execute ( )
225
225
}
226
226
} )
227
227
} else {
0 commit comments