Skip to content

Commit df8be92

Browse files
authored
fix(db-postgres): x3 and more nested blocks regression (#12770)
1 parent e7b5884 commit df8be92

File tree

7 files changed

+119
-6
lines changed

7 files changed

+119
-6
lines changed

packages/drizzle/src/schema/traverseFields.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ export const traverseFields = ({
387387
if (typeof blocksTableNameMap[blockTableName] === 'undefined') {
388388
blocksTableNameMap[blockTableName] = 1
389389
} else if (
390+
!adapter.rawTables[blockTableName] ||
390391
!validateExistingBlockIsIdentical({
391392
block,
392393
localized: field.localized,

test/database/config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ export default buildConfigWithDefaults({
7272
name: 'number',
7373
type: 'number',
7474
},
75+
{
76+
type: 'blocks',
77+
name: 'blocks',
78+
blocks: [
79+
{
80+
slug: 'block',
81+
fields: [
82+
{
83+
type: 'blocks',
84+
name: 'nested',
85+
blocks: [
86+
{
87+
slug: 'block',
88+
fields: [
89+
{
90+
type: 'blocks',
91+
name: 'nested',
92+
blocks: [],
93+
},
94+
],
95+
},
96+
],
97+
},
98+
],
99+
},
100+
],
101+
},
75102
{
76103
type: 'tabs',
77104
tabs: [

test/database/int.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,4 +2666,28 @@ describe('database', () => {
26662666
expect((e as ValidationError).message).toEqual('The following field is invalid: slugField')
26672667
}
26682668
})
2669+
2670+
it('should support x3 nesting blocks', async () => {
2671+
const res = await payload.create({
2672+
collection: 'posts',
2673+
data: {
2674+
title: 'title',
2675+
blocks: [
2676+
{
2677+
blockType: 'block',
2678+
nested: [
2679+
{
2680+
blockType: 'block',
2681+
nested: [],
2682+
},
2683+
],
2684+
},
2685+
],
2686+
},
2687+
})
2688+
2689+
expect(res.blocks).toHaveLength(1)
2690+
expect(res.blocks[0]?.nested).toHaveLength(1)
2691+
expect(res.blocks[0]?.nested[0]?.nested).toHaveLength(0)
2692+
})
26692693
})

test/database/payload-types.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface Config {
8484
'compound-indexes': CompoundIndex;
8585
aliases: Alias;
8686
'blocks-docs': BlocksDoc;
87+
'unique-fields': UniqueField;
8788
users: User;
8889
'payload-locked-documents': PayloadLockedDocument;
8990
'payload-preferences': PayloadPreference;
@@ -108,6 +109,7 @@ export interface Config {
108109
'compound-indexes': CompoundIndexesSelect<false> | CompoundIndexesSelect<true>;
109110
aliases: AliasesSelect<false> | AliasesSelect<true>;
110111
'blocks-docs': BlocksDocsSelect<false> | BlocksDocsSelect<true>;
112+
'unique-fields': UniqueFieldsSelect<false> | UniqueFieldsSelect<true>;
111113
users: UsersSelect<false> | UsersSelect<true>;
112114
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
113115
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
@@ -178,6 +180,21 @@ export interface Post {
178180
localized?: string | null;
179181
text?: string | null;
180182
number?: number | null;
183+
blocks?:
184+
| {
185+
nested?:
186+
| {
187+
nested?: unknown[] | null;
188+
id?: string | null;
189+
blockName?: string | null;
190+
blockType: 'block';
191+
}[]
192+
| null;
193+
id?: string | null;
194+
blockName?: string | null;
195+
blockType: 'block';
196+
}[]
197+
| null;
181198
D1?: {
182199
D2?: {
183200
D3?: {
@@ -525,6 +542,16 @@ export interface BlocksDoc {
525542
updatedAt: string;
526543
createdAt: string;
527544
}
545+
/**
546+
* This interface was referenced by `Config`'s JSON-Schema
547+
* via the `definition` "unique-fields".
548+
*/
549+
export interface UniqueField {
550+
id: string;
551+
slugField?: string | null;
552+
updatedAt: string;
553+
createdAt: string;
554+
}
528555
/**
529556
* This interface was referenced by `Config`'s JSON-Schema
530557
* via the `definition` "users".
@@ -617,6 +644,10 @@ export interface PayloadLockedDocument {
617644
relationTo: 'blocks-docs';
618645
value: string | BlocksDoc;
619646
} | null)
647+
| ({
648+
relationTo: 'unique-fields';
649+
value: string | UniqueField;
650+
} | null)
620651
| ({
621652
relationTo: 'users';
622653
value: string | User;
@@ -682,6 +713,27 @@ export interface PostsSelect<T extends boolean = true> {
682713
localized?: T;
683714
text?: T;
684715
number?: T;
716+
blocks?:
717+
| T
718+
| {
719+
block?:
720+
| T
721+
| {
722+
nested?:
723+
| T
724+
| {
725+
block?:
726+
| T
727+
| {
728+
nested?: T | {};
729+
id?: T;
730+
blockName?: T;
731+
};
732+
};
733+
id?: T;
734+
blockName?: T;
735+
};
736+
};
685737
D1?:
686738
| T
687739
| {
@@ -997,6 +1049,15 @@ export interface BlocksDocsSelect<T extends boolean = true> {
9971049
updatedAt?: T;
9981050
createdAt?: T;
9991051
}
1052+
/**
1053+
* This interface was referenced by `Config`'s JSON-Schema
1054+
* via the `definition` "unique-fields_select".
1055+
*/
1056+
export interface UniqueFieldsSelect<T extends boolean = true> {
1057+
slugField?: T;
1058+
updatedAt?: T;
1059+
createdAt?: T;
1060+
}
10001061
/**
10011062
* This interface was referenced by `Config`'s JSON-Schema
10021063
* via the `definition` "users_select".

test/database/up-down-migration/migrations/20250528_153134.json renamed to test/database/up-down-migration/migrations/20250611_163948.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "945960df-cc37-406f-a343-58ea672a2286",
2+
"id": "353cac31-1e1a-4190-8584-025abe855faa",
33
"prevId": "00000000-0000-0000-0000-000000000000",
44
"version": "7",
55
"dialect": "postgresql",

test/database/up-down-migration/migrations/20250528_153134.ts renamed to test/database/up-down-migration/migrations/20250611_163948.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { MigrateDownArgs, MigrateUpArgs } from '@payloadcms/db-postgres'
1+
import type { MigrateDownArgs, MigrateUpArgs} from '@payloadcms/db-postgres';
22

33
import { sql } from '@payloadcms/db-postgres'
44

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as migration_20250528_153134 from './20250528_153134.js'
1+
import * as migration_20250611_163948 from './20250611_163948.js'
22

33
export const migrations = [
44
{
5-
up: migration_20250528_153134.up,
6-
down: migration_20250528_153134.down,
7-
name: '20250528_153134',
5+
up: migration_20250611_163948.up,
6+
down: migration_20250611_163948.down,
7+
name: '20250611_163948',
88
},
99
]

0 commit comments

Comments
 (0)