Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/drizzle/src/createVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export async function createVersion<T extends JsonObject = JsonObject>(
SET latest = false
WHERE ${table.id} != ${result.id}
Comment thread
gonzoblasco marked this conversation as resolved.
Outdated
AND ${table.parent} = ${parent}
AND ${table.updatedAt} < ${result.updatedAt || updatedAt}
`,
})
}
Expand Down
58 changes: 57 additions & 1 deletion test/versions/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import {
draftWithUploadCloudStorageCollectionSlug,
draftWithUploadCollectionSlug,
localizedCollectionSlug,
nestedArraySelectCollectionSlug,
localizedGlobalSlug,
nestedArraySelectCollectionSlug,
versionCollectionSlug,
} from './slugs.js'

Expand Down Expand Up @@ -1930,6 +1930,7 @@ describe('Versions', () => {
},
})

expect(docs).toHaveLength(1)
expect(docs[0]).toBeDefined()

await cleanupDocuments({
Expand All @@ -1938,6 +1939,61 @@ describe('Versions', () => {
})
})

it('should not produce duplicate latest=true rows on same-second writes', async () => {
// Reproduces the bug from #17216: autosave writes within the same
// second used to keep both rows with latest=true because the
// updatedAt < guard was strict and couldn't match same-second rows.
const doc = await payload.create({
collection: draftCollectionSlug,
data: {
description: 'test',
title: 'test',
},
})

// Force two writes that share the same updatedAt timestamp
const now = new Date().toISOString()
await payload.update({
id: doc.id,
collection: draftCollectionSlug,
data: { title: 'first' },
draft: true,
updatedAt: now,
})
await payload.update({
id: doc.id,
collection: draftCollectionSlug,
data: { title: 'second' },
draft: true,
updatedAt: now,
})

const { docs } = await payload.findVersions({
collection: draftCollectionSlug,
where: {
and: [
{
parent: {
equals: doc.id,
},
},
{
latest: {
equals: true,
},
},
],
},
})

expect(docs).toHaveLength(1)

await cleanupDocuments({
collectionSlugs: [draftCollectionSlug],
payload,
})
})

it('should fall back to creating a new version when updateVersion fails due to a concurrent write', async () => {
const doc = await payload.create({
collection: autosaveCollectionSlug,
Expand Down
Loading