fix(db-mongodb): strip deleted from the config blocks from the result (#12869)
If you (using the MongoDB adapter) delete a block from the payload config, but still have some data with that block in the DB, you'd receive in the admin panel an error like: ``` Block with type "cta" was found in block data, but no block with that type is defined in the config for field with schema path pages.blocks ``` Now, we remove those "unknown" blocks at the DB adapter level. Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* eslint-disable no-restricted-exports */
|
||||
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import path from 'path'
|
||||
const filename = fileURLToPath(import.meta.url)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { CustomView as CustomView_c4f0e2747eca2be436a06a63cea31567 } from '../../CustomView/index.js'
|
||||
|
||||
export const importMap = {
|
||||
"/CustomView/index.js#CustomView": CustomView_c4f0e2747eca2be436a06a63cea31567
|
||||
'/CustomView/index.js#CustomView': CustomView_c4f0e2747eca2be436a06a63cea31567,
|
||||
}
|
||||
|
||||
@@ -2727,6 +2727,53 @@ describe('database', () => {
|
||||
expect(res.blocks[0]?.nested[0]?.nested).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('should ignore blocks that exist in the db but not in the config', async () => {
|
||||
// not possible w/ SQL anyway
|
||||
// eslint-disable-next-line jest/no-conditional-in-test
|
||||
if (payload.db.name !== 'mongoose') {
|
||||
return
|
||||
}
|
||||
|
||||
const res = await payload.db.collections['blocks-docs']?.collection.insertOne({
|
||||
testBlocks: [
|
||||
{
|
||||
id: '1',
|
||||
blockType: 'cta',
|
||||
text: 'valid block',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
blockType: 'cta_2',
|
||||
text: 'non-valid block',
|
||||
},
|
||||
],
|
||||
testBlocksLocalized: {
|
||||
en: [
|
||||
{
|
||||
id: '1',
|
||||
blockType: 'cta',
|
||||
text: 'valid block',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
blockType: 'cta_2',
|
||||
text: 'non-valid block',
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const doc = await payload.findByID({
|
||||
collection: 'blocks-docs',
|
||||
id: res?.insertedId?.toHexString() as string,
|
||||
locale: 'en',
|
||||
})
|
||||
expect(doc.testBlocks).toHaveLength(1)
|
||||
expect(doc.testBlocks[0].id).toBe('1')
|
||||
expect(doc.testBlocksLocalized).toHaveLength(1)
|
||||
expect(doc.testBlocksLocalized[0].id).toBe('1')
|
||||
})
|
||||
|
||||
it('should CRUD with blocks as JSON in SQL adapters', async () => {
|
||||
// eslint-disable-next-line jest/no-conditional-in-test
|
||||
if (!('drizzle' in payload.db)) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"id": "e7129a5b-a5af-490a-8ad9-318e3caeca26",
|
||||
"id": "a3dd8ca0-5e09-407b-9178-e0ff7f15da59",
|
||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||
"version": "7",
|
||||
"dialect": "postgresql",
|
||||
@@ -1,9 +1,9 @@
|
||||
import * as migration_20250624_142145 from './20250624_142145.js'
|
||||
import * as migration_20250624_214621 from './20250624_214621.js'
|
||||
|
||||
export const migrations = [
|
||||
{
|
||||
up: migration_20250624_142145.up,
|
||||
down: migration_20250624_142145.down,
|
||||
name: '20250624_142145',
|
||||
up: migration_20250624_214621.up,
|
||||
down: migration_20250624_214621.down,
|
||||
name: '20250624_214621',
|
||||
},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user