Skip to content

Commit b41b4b2

Browse files
committed
fix(db): migration index names & save upload id as string
1 parent dc22943 commit b41b4b2

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

lib/db/index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { hash } from 'node:crypto'
2+
13
import { Kysely, Migrator } from 'kysely'
24

35
import type { DatabaseDriverName } from '~/lib/db/drivers'
@@ -9,6 +11,7 @@ import { logger } from '~/lib/logger'
911
import type { Selectable } from 'kysely'
1012

1113
export interface CacheKeysTable {
14+
id: string
1215
key: string
1316
version: string
1417
updated_at: string
@@ -18,11 +21,11 @@ export interface UploadsTable {
1821
created_at: string
1922
key: string
2023
version: string
21-
id: number
24+
id: string
2225
driver_upload_id: string
2326
}
2427
export interface UploadPartsTable {
25-
upload_id: number
28+
upload_id: string
2629
part_number: number
2730
e_tag: string | null
2831
}
@@ -86,8 +89,7 @@ export async function findKeyMatch(
8689
logger.debug('Finding key match', args)
8790
const exactPrimaryMatch = await db
8891
.selectFrom('cache_keys')
89-
.where('key', '=', args.key)
90-
.where('version', '=', args.version)
92+
.where('id', '=', getCacheKeyId(args.key, args.version))
9193
.selectAll()
9294
.executeTakeFirst()
9395
if (exactPrimaryMatch) {
@@ -117,8 +119,7 @@ export async function findKeyMatch(
117119
for (const key of args.restoreKeys) {
118120
const exactMatch = await db
119121
.selectFrom('cache_keys')
120-
.where('version', '=', args.version)
121-
.where('key', '=', key)
122+
.where('id', '=', getCacheKeyId(key, args.version))
122123
.orderBy('cache_keys.updated_at desc')
123124
.selectAll()
124125
.executeTakeFirst()
@@ -161,8 +162,7 @@ export async function updateOrCreateKey(
161162
.updateTable('cache_keys')
162163
.set('updated_at', now.toISOString())
163164
.set('accessed_at', now.toISOString())
164-
.where('key', '=', key)
165-
.where('version', '=', version)
165+
.where('id', '=', getCacheKeyId(key, version))
166166
.executeTakeFirst()
167167
if (Number(updateResult.numUpdatedRows) === 0) {
168168
await createKey(db, { key, version, date })
@@ -177,8 +177,7 @@ export async function touchKey(
177177
await db
178178
.updateTable('cache_keys')
179179
.set('accessed_at', now.toISOString())
180-
.where('key', '=', key)
181-
.where('version', '=', version)
180+
.where('id', '=', getCacheKeyId(key, version))
182181
.execute()
183182
}
184183

@@ -205,6 +204,7 @@ export async function createKey(
205204
await db
206205
.insertInto('cache_keys')
207206
.values({
207+
id: getCacheKeyId(key, version),
208208
key,
209209
version,
210210
updated_at: now.toISOString(),
@@ -213,15 +213,15 @@ export async function createKey(
213213
.execute()
214214
}
215215

216+
function getCacheKeyId(key: string, version: string) {
217+
return hash('sha256', Buffer.from(`${key}-${version}`))
218+
}
219+
216220
export async function pruneKeys(db: DB, keys?: Selectable<CacheKeysTable>[]) {
217221
if (keys) {
218222
await db.transaction().execute(async (tx) => {
219223
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()
225225
}
226226
})
227227
} else {

lib/db/migrations.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ export function migrations(dbType: DatabaseDriverName) {
88
async up(db) {
99
let query = db.schema
1010
.createTable('cache_keys')
11-
.addColumn('key', dbType === 'mysql' ? 'varchar(512)' : 'text', (col) => col.notNull())
12-
.addColumn('version', dbType === 'mysql' ? 'varchar(512)' : 'text', (col) =>
13-
col.notNull(),
14-
)
11+
.addColumn('id', 'varchar(255)', (col) => col.notNull().primaryKey())
12+
.addColumn('key', 'text', (col) => col.notNull())
13+
.addColumn('version', 'text', (col) => col.notNull())
1514
.addColumn('updated_at', 'text', (col) => col.notNull())
1615
.addColumn('accessed_at', 'text', (col) => col.notNull())
17-
.addPrimaryKeyConstraint('pk', ['key', 'version'])
1816

1917
if (dbType === 'mysql') query = query.modifyEnd(sql`engine=InnoDB CHARSET=latin1`)
2018

@@ -32,17 +30,17 @@ export function migrations(dbType: DatabaseDriverName) {
3230
.addColumn('key', 'text', (col) => col.notNull())
3331
.addColumn('version', 'text', (col) => col.notNull())
3432
.addColumn('driver_upload_id', 'text', (col) => col.notNull())
35-
.addColumn('id', 'integer', (col) => col.notNull())
36-
.addPrimaryKeyConstraint('pk', ['id'])
37-
.addUniqueConstraint('key_version', ['key', 'version'])
33+
.addColumn('id', dbType === 'mysql' ? 'varchar(255)' : 'text', (col) =>
34+
col.notNull().primaryKey(),
35+
)
3836
.ifNotExists()
3937
.execute()
4038
await db.schema
4139
.createTable('upload_parts')
42-
.addColumn('upload_id', 'integer')
40+
.addColumn('upload_id', dbType === 'mysql' ? 'varchar(255)' : 'text')
4341
.addColumn('part_number', 'integer')
4442
.addColumn('e_tag', 'text')
45-
.addPrimaryKeyConstraint('pk', ['upload_id', 'part_number'])
43+
.addPrimaryKeyConstraint('pk_upload_parts', ['upload_id', 'part_number'])
4644
.addForeignKeyConstraint(
4745
'fk_upload_parts_uploads',
4846
['upload_id'],
@@ -55,6 +53,7 @@ export function migrations(dbType: DatabaseDriverName) {
5553
},
5654
async down(db) {
5755
await db.schema.dropTable('uploads').ifExists().execute()
56+
await db.schema.dropTable('upload_parts').ifExists().execute()
5857
},
5958
},
6059
} satisfies Record<string, Migration>

lib/storage/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export async function initializeStorage() {
8484
.values({
8585
created_at: new Date().toISOString(),
8686
driver_upload_id: driverUploadId,
87-
id: uploadId,
87+
id: uploadId.toString(),
8888
key,
8989
version,
9090
})
@@ -105,7 +105,7 @@ export async function initializeStorage() {
105105
const upload = await db
106106
.selectFrom('uploads')
107107
.selectAll()
108-
.where('id', '=', uploadId)
108+
.where('id', '=', uploadId.toString())
109109
.executeTakeFirst()
110110
if (!upload) {
111111
logger.debug(`Upload: Upload not found. Ignoring...`, {
@@ -138,7 +138,7 @@ export async function initializeStorage() {
138138
.insertInto('upload_parts')
139139
.values({
140140
part_number: partNumber,
141-
upload_id: uploadId,
141+
upload_id: uploadId.toString(),
142142
e_tag: eTag,
143143
})
144144
.execute()
@@ -157,7 +157,7 @@ export async function initializeStorage() {
157157
const upload = await db
158158
.selectFrom('uploads')
159159
.selectAll()
160-
.where('id', '=', uploadId)
160+
.where('id', '=', uploadId.toString())
161161
.executeTakeFirst()
162162

163163
if (!upload) {

0 commit comments

Comments
 (0)