fix(db-postgres): preserve parent createdAt when creating a new version (#8160)
## Description Fixes https://github.com/payloadcms/payload/issues/7915 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
This commit is contained in:
@@ -29,14 +29,20 @@ export async function createVersion<T extends TypeWithID>(
|
|||||||
delete version.id
|
delete version.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const data: Record<string, unknown> = {
|
||||||
|
autosave,
|
||||||
|
latest: true,
|
||||||
|
parent,
|
||||||
|
version,
|
||||||
|
}
|
||||||
|
|
||||||
|
if ('createdAt' in version) {
|
||||||
|
data.createdAt = version.createdAt
|
||||||
|
}
|
||||||
|
|
||||||
const result = await upsertRow<TypeWithVersion<T>>({
|
const result = await upsertRow<TypeWithVersion<T>>({
|
||||||
adapter: this,
|
adapter: this,
|
||||||
data: {
|
data,
|
||||||
autosave,
|
|
||||||
latest: true,
|
|
||||||
parent,
|
|
||||||
version,
|
|
||||||
},
|
|
||||||
db,
|
db,
|
||||||
fields: buildVersionCollectionFields(collection),
|
fields: buildVersionCollectionFields(collection),
|
||||||
operation: 'create',
|
operation: 'create',
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import type { Payload } from 'payload'
|
|||||||
|
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { ValidationError } from 'payload'
|
import { ValidationError } from 'payload'
|
||||||
|
import { wait } from 'payload/shared'
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
|
|
||||||
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
|
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
|
||||||
@@ -299,6 +300,23 @@ describe('Versions', () => {
|
|||||||
draftsDescending.docs[draftsDescending.docs.length - 1],
|
draftsDescending.docs[draftsDescending.docs.length - 1],
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should have the same createdAt on new version create', async () => {
|
||||||
|
const doc = await payload.create({
|
||||||
|
collection: autosaveCollectionSlug,
|
||||||
|
data: { description: 'descr', title: 'title' },
|
||||||
|
})
|
||||||
|
|
||||||
|
await wait(30)
|
||||||
|
|
||||||
|
const updated = await payload.update({
|
||||||
|
collection: autosaveCollectionSlug,
|
||||||
|
id: doc.id,
|
||||||
|
data: {},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(doc.createdAt).toBe(updated.createdAt)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Restore', () => {
|
describe('Restore', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user