Skip to content

Commit

Permalink
Fix db query to restore previous doc version (vercel#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyphilemon authored Nov 6, 2024
1 parent d948b99 commit c5f6ac8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
User,
document,
Suggestion,
suggestion,
Message,
message,
vote,
Expand Down Expand Up @@ -227,6 +228,15 @@ export async function deleteDocumentsByIdAfterTimestamp({
timestamp: Date;
}) {
try {
await db
.delete(suggestion)
.where(
and(
eq(suggestion.documentId, id),
gt(suggestion.documentCreatedAt, timestamp)
)
);

return await db
.delete(document)
.where(and(eq(document.id, id), gt(document.createdAt, timestamp)));
Expand All @@ -244,7 +254,7 @@ export async function saveSuggestions({
suggestions: Array<Suggestion>;
}) {
try {
return await db.insert(Suggestion).values(suggestions);
return await db.insert(suggestion).values(suggestions);
} catch (error) {
console.error('Failed to save suggestions in database');
throw error;
Expand All @@ -259,8 +269,8 @@ export async function getSuggestionsByDocumentId({
try {
return await db
.select()
.from(Suggestion)
.where(and(eq(Suggestion.documentId, documentId)));
.from(suggestion)
.where(and(eq(suggestion.documentId, documentId)));
} catch (error) {
console.error(
'Failed to get suggestions by document version from database'
Expand Down
4 changes: 2 additions & 2 deletions db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const document = pgTable(

export type Document = InferSelectModel<typeof document>;

export const Suggestion = pgTable(
export const suggestion = pgTable(
'Suggestion',
{
id: uuid('id').notNull().defaultRandom(),
Expand All @@ -106,4 +106,4 @@ export const Suggestion = pgTable(
})
);

export type Suggestion = InferSelectModel<typeof Suggestion>;
export type Suggestion = InferSelectModel<typeof suggestion>;

0 comments on commit c5f6ac8

Please sign in to comment.