Skip to content

Commit 8fff15c

Browse files
committed
fix(dev): do not create hash column on local cache table
1 parent c06cea8 commit 8fff15c

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/utils/collection.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export const MAX_SQL_QUERY_SIZE = 100000
151151
export const SLICE_SIZE = 70000
152152

153153
// Convert collection data to SQL insert statement
154-
export function generateCollectionInsert(collection: ResolvedCollection, data: ParsedContentFile): { queries: string[], hash: string } {
154+
export function generateCollectionInsert(collection: ResolvedCollection, data: ParsedContentFile, opts: { hashColumn?: boolean } = {}): { queries: string[], hash: string } {
155155
const fields: string[] = []
156156
const values: Array<string | number | boolean> = []
157157
const sortedKeys = getOrderedSchemaKeys((collection.extendedSchema).shape)
@@ -191,8 +191,9 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: P
191191
})
192192

193193
const valuesHash = hash(values)
194-
195-
values.push(`'${valuesHash}'`)
194+
if (opts.hashColumn !== false) {
195+
values.push(`'${valuesHash}'`)
196+
}
196197

197198
let index = 0
198199
const sql = `INSERT INTO ${collection.tableName} VALUES (${'?, '.repeat(values.length).slice(0, -2)});`
@@ -236,7 +237,7 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: P
236237
}
237238

238239
// Convert a collection with Zod schema to SQL table definition
239-
export function generateCollectionTableDefinition(collection: ResolvedCollection, opts: { drop?: boolean } = {}) {
240+
export function generateCollectionTableDefinition(collection: ResolvedCollection, opts: { drop?: boolean, hashColumn?: boolean } = {}) {
240241
const sortedKeys = getOrderedSchemaKeys((collection.extendedSchema).shape)
241242
const sqlFields = sortedKeys.map((key) => {
242243
const type = (collection.extendedSchema).shape[key]!
@@ -281,8 +282,10 @@ export function generateCollectionTableDefinition(collection: ResolvedCollection
281282
return `"${key}" ${sqlType}${constraints.join(' ')}`
282283
})
283284

284-
// add __hash__ field for inserts
285-
sqlFields.push('"__hash__" TEXT UNIQUE')
285+
if (opts.hashColumn !== false) {
286+
// add __hash__ field for inserts
287+
sqlFields.push('"__hash__" TEXT UNIQUE')
288+
}
286289

287290
let definition = `CREATE TABLE IF NOT EXISTS ${collection.tableName} (${sqlFields.join(', ')});`
288291

src/utils/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export async function getLocalDatabase(database: SqliteDatabaseConfig | D1Databa
8080
} as unknown as ResolvedCollection
8181

8282
_localDatabase[databaseLocation] = db
83-
await db.exec(generateCollectionTableDefinition(cacheCollection))
83+
await db.exec(generateCollectionTableDefinition(cacheCollection, { hashColumn: false }))
8484

8585
const fetchDevelopmentCache = async () => {
8686
const result = await db.prepare('SELECT * FROM _development_cache').all() as CacheEntry[]
@@ -93,7 +93,7 @@ export async function getLocalDatabase(database: SqliteDatabaseConfig | D1Databa
9393

9494
const insertDevelopmentCache = async (id: string, checksum: string, parsedContent: string) => {
9595
deleteDevelopmentCache(id)
96-
const insert = generateCollectionInsert(cacheCollection, { id, checksum, parsedContent })
96+
const insert = generateCollectionInsert(cacheCollection, { id, checksum, parsedContent }, { hashColumn: false })
9797
for (const query of insert.queries) {
9898
await db.exec(query)
9999
}

0 commit comments

Comments
 (0)