From df8be92d47c93886a428eff54a78263d0862e183 Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Thu, 12 Jun 2025 19:37:07 +0300 Subject: [PATCH] fix(db-postgres): x3 and more nested blocks regression (#12770) --- packages/drizzle/src/schema/traverseFields.ts | 1 + test/database/config.ts | 27 ++++++++ test/database/int.spec.ts | 24 ++++++++ test/database/payload-types.ts | 61 +++++++++++++++++++ ...50528_153134.json => 20250611_163948.json} | 2 +- ...{20250528_153134.ts => 20250611_163948.ts} | 2 +- .../up-down-migration/migrations/index.ts | 8 +-- 7 files changed, 119 insertions(+), 6 deletions(-) rename test/database/up-down-migration/migrations/{20250528_153134.json => 20250611_163948.json} (99%) rename test/database/up-down-migration/migrations/{20250528_153134.ts => 20250611_163948.ts} (98%) diff --git a/packages/drizzle/src/schema/traverseFields.ts b/packages/drizzle/src/schema/traverseFields.ts index 2b0b0d719..d1eb397fe 100644 --- a/packages/drizzle/src/schema/traverseFields.ts +++ b/packages/drizzle/src/schema/traverseFields.ts @@ -387,6 +387,7 @@ export const traverseFields = ({ if (typeof blocksTableNameMap[blockTableName] === 'undefined') { blocksTableNameMap[blockTableName] = 1 } else if ( + !adapter.rawTables[blockTableName] || !validateExistingBlockIsIdentical({ block, localized: field.localized, diff --git a/test/database/config.ts b/test/database/config.ts index 2842d1e0f..5306d9373 100644 --- a/test/database/config.ts +++ b/test/database/config.ts @@ -72,6 +72,33 @@ export default buildConfigWithDefaults({ name: 'number', type: 'number', }, + { + type: 'blocks', + name: 'blocks', + blocks: [ + { + slug: 'block', + fields: [ + { + type: 'blocks', + name: 'nested', + blocks: [ + { + slug: 'block', + fields: [ + { + type: 'blocks', + name: 'nested', + blocks: [], + }, + ], + }, + ], + }, + ], + }, + ], + }, { type: 'tabs', tabs: [ diff --git a/test/database/int.spec.ts b/test/database/int.spec.ts index d02ebaae5..0d9d6afc5 100644 --- a/test/database/int.spec.ts +++ b/test/database/int.spec.ts @@ -2666,4 +2666,28 @@ describe('database', () => { expect((e as ValidationError).message).toEqual('The following field is invalid: slugField') } }) + + it('should support x3 nesting blocks', async () => { + const res = await payload.create({ + collection: 'posts', + data: { + title: 'title', + blocks: [ + { + blockType: 'block', + nested: [ + { + blockType: 'block', + nested: [], + }, + ], + }, + ], + }, + }) + + expect(res.blocks).toHaveLength(1) + expect(res.blocks[0]?.nested).toHaveLength(1) + expect(res.blocks[0]?.nested[0]?.nested).toHaveLength(0) + }) }) diff --git a/test/database/payload-types.ts b/test/database/payload-types.ts index fe1a91c3a..886a1cf63 100644 --- a/test/database/payload-types.ts +++ b/test/database/payload-types.ts @@ -84,6 +84,7 @@ export interface Config { 'compound-indexes': CompoundIndex; aliases: Alias; 'blocks-docs': BlocksDoc; + 'unique-fields': UniqueField; users: User; 'payload-locked-documents': PayloadLockedDocument; 'payload-preferences': PayloadPreference; @@ -108,6 +109,7 @@ export interface Config { 'compound-indexes': CompoundIndexesSelect | CompoundIndexesSelect; aliases: AliasesSelect | AliasesSelect; 'blocks-docs': BlocksDocsSelect | BlocksDocsSelect; + 'unique-fields': UniqueFieldsSelect | UniqueFieldsSelect; users: UsersSelect | UsersSelect; 'payload-locked-documents': PayloadLockedDocumentsSelect | PayloadLockedDocumentsSelect; 'payload-preferences': PayloadPreferencesSelect | PayloadPreferencesSelect; @@ -178,6 +180,21 @@ export interface Post { localized?: string | null; text?: string | null; number?: number | null; + blocks?: + | { + nested?: + | { + nested?: unknown[] | null; + id?: string | null; + blockName?: string | null; + blockType: 'block'; + }[] + | null; + id?: string | null; + blockName?: string | null; + blockType: 'block'; + }[] + | null; D1?: { D2?: { D3?: { @@ -525,6 +542,16 @@ export interface BlocksDoc { updatedAt: string; createdAt: string; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "unique-fields". + */ +export interface UniqueField { + id: string; + slugField?: string | null; + updatedAt: string; + createdAt: string; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "users". @@ -617,6 +644,10 @@ export interface PayloadLockedDocument { relationTo: 'blocks-docs'; value: string | BlocksDoc; } | null) + | ({ + relationTo: 'unique-fields'; + value: string | UniqueField; + } | null) | ({ relationTo: 'users'; value: string | User; @@ -682,6 +713,27 @@ export interface PostsSelect { localized?: T; text?: T; number?: T; + blocks?: + | T + | { + block?: + | T + | { + nested?: + | T + | { + block?: + | T + | { + nested?: T | {}; + id?: T; + blockName?: T; + }; + }; + id?: T; + blockName?: T; + }; + }; D1?: | T | { @@ -997,6 +1049,15 @@ export interface BlocksDocsSelect { updatedAt?: T; createdAt?: T; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "unique-fields_select". + */ +export interface UniqueFieldsSelect { + slugField?: T; + updatedAt?: T; + createdAt?: T; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "users_select". diff --git a/test/database/up-down-migration/migrations/20250528_153134.json b/test/database/up-down-migration/migrations/20250611_163948.json similarity index 99% rename from test/database/up-down-migration/migrations/20250528_153134.json rename to test/database/up-down-migration/migrations/20250611_163948.json index 8e1af1ba2..253082539 100644 --- a/test/database/up-down-migration/migrations/20250528_153134.json +++ b/test/database/up-down-migration/migrations/20250611_163948.json @@ -1,5 +1,5 @@ { - "id": "945960df-cc37-406f-a343-58ea672a2286", + "id": "353cac31-1e1a-4190-8584-025abe855faa", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", diff --git a/test/database/up-down-migration/migrations/20250528_153134.ts b/test/database/up-down-migration/migrations/20250611_163948.ts similarity index 98% rename from test/database/up-down-migration/migrations/20250528_153134.ts rename to test/database/up-down-migration/migrations/20250611_163948.ts index 5409567ee..086047bb6 100644 --- a/test/database/up-down-migration/migrations/20250528_153134.ts +++ b/test/database/up-down-migration/migrations/20250611_163948.ts @@ -1,4 +1,4 @@ -import type { MigrateDownArgs, MigrateUpArgs } from '@payloadcms/db-postgres' +import type { MigrateDownArgs, MigrateUpArgs} from '@payloadcms/db-postgres'; import { sql } from '@payloadcms/db-postgres' diff --git a/test/database/up-down-migration/migrations/index.ts b/test/database/up-down-migration/migrations/index.ts index 9a9b0fa86..efadce63b 100644 --- a/test/database/up-down-migration/migrations/index.ts +++ b/test/database/up-down-migration/migrations/index.ts @@ -1,9 +1,9 @@ -import * as migration_20250528_153134 from './20250528_153134.js' +import * as migration_20250611_163948 from './20250611_163948.js' export const migrations = [ { - up: migration_20250528_153134.up, - down: migration_20250528_153134.down, - name: '20250528_153134', + up: migration_20250611_163948.up, + down: migration_20250611_163948.down, + name: '20250611_163948', }, ]