Skip to content

Commit 18b0806

Browse files
authored
fix(db-postgres, db-sqlite): hasMany text, number, poly relationship, blocks, arrays within localized fields (#7900)
## Description In Postgres, localized blocks or arrays that contain other array / block / relationship fields were not properly storing locales in the database. Now they are! Need to check a few things yet: - Ensure test coverage is sufficient - Test localized array, with non-localized array inside of it - Test localized block with relationship field within it - Ensure `_rels` table gets the `locale` column added if a single non-localized relationship exists within a localized array / block Fixes step 6 as identified in #7805
1 parent 3d9051a commit 18b0806

File tree

17 files changed

+986
-92
lines changed

17 files changed

+986
-92
lines changed

packages/db-sqlite/src/schema/build.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,17 @@ type Args = {
5858
tableName: string
5959
timestamps?: boolean
6060
versions: boolean
61+
/**
62+
* Tracks whether or not this table is built
63+
* from the result of a localized array or block field at some point
64+
*/
65+
withinLocalizedArrayOrBlock?: boolean
6166
}
6267

6368
type Result = {
69+
hasLocalizedManyNumberField: boolean
70+
hasLocalizedManyTextField: boolean
71+
hasLocalizedRelationshipField: boolean
6472
hasManyNumberField: 'index' | boolean
6573
hasManyTextField: 'index' | boolean
6674
relationsToBuild: RelationMap
@@ -81,6 +89,7 @@ export const buildTable = ({
8189
tableName,
8290
timestamps,
8391
versions,
92+
withinLocalizedArrayOrBlock,
8493
}: Args): Result => {
8594
const isRoot = !incomingRootTableName
8695
const rootTableName = incomingRootTableName || tableName
@@ -128,6 +137,7 @@ export const buildTable = ({
128137
rootTableIDColType: rootTableIDColType || idColType,
129138
rootTableName,
130139
versions,
140+
withinLocalizedArrayOrBlock,
131141
})
132142

133143
// split the relationsToBuild by localized and non-localized
@@ -478,5 +488,12 @@ export const buildTable = ({
478488
return result
479489
})
480490

481-
return { hasManyNumberField, hasManyTextField, relationsToBuild }
491+
return {
492+
hasLocalizedManyNumberField,
493+
hasLocalizedManyTextField,
494+
hasLocalizedRelationshipField,
495+
hasManyNumberField,
496+
hasManyTextField,
497+
relationsToBuild,
498+
}
482499
}

packages/db-sqlite/src/schema/traverseFields.ts

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ type Args = {
5252
rootTableIDColType: IDType
5353
rootTableName: string
5454
versions: boolean
55+
/**
56+
* Tracks whether or not this table is built
57+
* from the result of a localized array or block field at some point
58+
*/
59+
withinLocalizedArrayOrBlock?: boolean
5560
}
5661

5762
type Result = {
@@ -84,6 +89,7 @@ export const traverseFields = ({
8489
rootTableIDColType,
8590
rootTableName,
8691
versions,
92+
withinLocalizedArrayOrBlock,
8793
}: Args): Result => {
8894
let hasLocalizedField = false
8995
let hasLocalizedRelationshipField = false
@@ -150,7 +156,11 @@ export const traverseFields = ({
150156
switch (field.type) {
151157
case 'text': {
152158
if (field.hasMany) {
153-
if (field.localized) {
159+
const isLocalized =
160+
Boolean(field.localized && adapter.payload.config.localization) ||
161+
withinLocalizedArrayOrBlock
162+
163+
if (isLocalized) {
154164
hasLocalizedManyTextField = true
155165
}
156166

@@ -179,7 +189,11 @@ export const traverseFields = ({
179189

180190
case 'number': {
181191
if (field.hasMany) {
182-
if (field.localized) {
192+
const isLocalized =
193+
Boolean(field.localized && adapter.payload.config.localization) ||
194+
withinLocalizedArrayOrBlock
195+
196+
if (isLocalized) {
183197
hasLocalizedManyNumberField = true
184198
}
185199

@@ -255,7 +269,11 @@ export const traverseFields = ({
255269
parentIdx: (cols) => index(`${selectTableName}_parent_idx`).on(cols.parent),
256270
}
257271

258-
if (field.localized) {
272+
const isLocalized =
273+
Boolean(field.localized && adapter.payload.config.localization) ||
274+
withinLocalizedArrayOrBlock
275+
276+
if (isLocalized) {
259277
baseColumns.locale = text('locale', { enum: locales }).notNull()
260278
baseExtraConfig.localeIdx = (cols) =>
261279
index(`${selectTableName}_locale_idx`).on(cols.locale)
@@ -337,13 +355,20 @@ export const traverseFields = ({
337355
_parentIDIdx: (cols) => index(`${arrayTableName}_parent_id_idx`).on(cols._parentID),
338356
}
339357

340-
if (field.localized && adapter.payload.config.localization) {
358+
const isLocalized =
359+
Boolean(field.localized && adapter.payload.config.localization) ||
360+
withinLocalizedArrayOrBlock
361+
362+
if (isLocalized) {
341363
baseColumns._locale = text('_locale', { enum: locales }).notNull()
342364
baseExtraConfig._localeIdx = (cols) =>
343365
index(`${arrayTableName}_locale_idx`).on(cols._locale)
344366
}
345367

346368
const {
369+
hasLocalizedManyNumberField: subHasLocalizedManyNumberField,
370+
hasLocalizedManyTextField: subHasLocalizedManyTextField,
371+
hasLocalizedRelationshipField: subHasLocalizedRelationshipField,
347372
hasManyNumberField: subHasManyNumberField,
348373
hasManyTextField: subHasManyTextField,
349374
relationsToBuild: subRelationsToBuild,
@@ -360,8 +385,21 @@ export const traverseFields = ({
360385
rootTableName,
361386
tableName: arrayTableName,
362387
versions,
388+
withinLocalizedArrayOrBlock: isLocalized,
363389
})
364390

391+
if (subHasLocalizedManyNumberField) {
392+
hasLocalizedManyNumberField = subHasLocalizedManyNumberField
393+
}
394+
395+
if (subHasLocalizedRelationshipField) {
396+
hasLocalizedRelationshipField = subHasLocalizedRelationshipField
397+
}
398+
399+
if (subHasLocalizedManyTextField) {
400+
hasLocalizedManyTextField = subHasLocalizedManyTextField
401+
}
402+
365403
if (subHasManyTextField) {
366404
if (!hasManyTextField || subHasManyTextField === 'index')
367405
hasManyTextField = subHasManyTextField
@@ -453,13 +491,20 @@ export const traverseFields = ({
453491
_pathIdx: (cols) => index(`${blockTableName}_path_idx`).on(cols._path),
454492
}
455493

456-
if (field.localized && adapter.payload.config.localization) {
494+
const isLocalized =
495+
Boolean(field.localized && adapter.payload.config.localization) ||
496+
withinLocalizedArrayOrBlock
497+
498+
if (isLocalized) {
457499
baseColumns._locale = text('_locale', { enum: locales }).notNull()
458500
baseExtraConfig._localeIdx = (cols) =>
459501
index(`${blockTableName}_locale_idx`).on(cols._locale)
460502
}
461503

462504
const {
505+
hasLocalizedManyNumberField: subHasLocalizedManyNumberField,
506+
hasLocalizedManyTextField: subHasLocalizedManyTextField,
507+
hasLocalizedRelationshipField: subHasLocalizedRelationshipField,
463508
hasManyNumberField: subHasManyNumberField,
464509
hasManyTextField: subHasManyTextField,
465510
relationsToBuild: subRelationsToBuild,
@@ -476,8 +521,21 @@ export const traverseFields = ({
476521
rootTableName,
477522
tableName: blockTableName,
478523
versions,
524+
withinLocalizedArrayOrBlock: isLocalized,
479525
})
480526

527+
if (subHasLocalizedManyNumberField) {
528+
hasLocalizedManyNumberField = subHasLocalizedManyNumberField
529+
}
530+
531+
if (subHasLocalizedRelationshipField) {
532+
hasLocalizedRelationshipField = subHasLocalizedRelationshipField
533+
}
534+
535+
if (subHasLocalizedManyTextField) {
536+
hasLocalizedManyTextField = subHasLocalizedManyTextField
537+
}
538+
481539
if (subHasManyTextField) {
482540
if (!hasManyTextField || subHasManyTextField === 'index')
483541
hasManyTextField = subHasManyTextField
@@ -577,6 +635,7 @@ export const traverseFields = ({
577635
rootTableIDColType,
578636
rootTableName,
579637
versions,
638+
withinLocalizedArrayOrBlock,
580639
})
581640

582641
if (groupHasLocalizedField) hasLocalizedField = true
@@ -618,6 +677,7 @@ export const traverseFields = ({
618677
rootTableIDColType,
619678
rootTableName,
620679
versions,
680+
withinLocalizedArrayOrBlock,
621681
})
622682

623683
if (groupHasLocalizedField) hasLocalizedField = true
@@ -660,6 +720,7 @@ export const traverseFields = ({
660720
rootTableIDColType,
661721
rootTableName,
662722
versions,
723+
withinLocalizedArrayOrBlock,
663724
})
664725

665726
if (tabHasLocalizedField) hasLocalizedField = true
@@ -702,6 +763,7 @@ export const traverseFields = ({
702763
rootTableIDColType,
703764
rootTableName,
704765
versions,
766+
withinLocalizedArrayOrBlock,
705767
})
706768

707769
if (rowHasLocalizedField) hasLocalizedField = true
@@ -753,7 +815,10 @@ export const traverseFields = ({
753815
}
754816
break
755817
}
756-
if (adapter.payload.config.localization && field.localized) {
818+
if (
819+
Boolean(field.localized && adapter.payload.config.localization) ||
820+
withinLocalizedArrayOrBlock
821+
) {
757822
hasLocalizedRelationshipField = true
758823
}
759824

packages/drizzle/src/postgres/schema/build.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ type Args = {
5454
tableName: string
5555
timestamps?: boolean
5656
versions: boolean
57+
/**
58+
* Tracks whether or not this table is built
59+
* from the result of a localized array or block field at some point
60+
*/
61+
withinLocalizedArrayOrBlock?: boolean
5762
}
5863

5964
type Result = {
65+
hasLocalizedManyNumberField: boolean
66+
hasLocalizedManyTextField: boolean
67+
hasLocalizedRelationshipField: boolean
6068
hasManyNumberField: 'index' | boolean
6169
hasManyTextField: 'index' | boolean
6270
relationsToBuild: RelationMap
@@ -76,6 +84,7 @@ export const buildTable = ({
7684
tableName,
7785
timestamps,
7886
versions,
87+
withinLocalizedArrayOrBlock,
7988
}: Args): Result => {
8089
const isRoot = !incomingRootTableName
8190
const rootTableName = incomingRootTableName || tableName
@@ -122,6 +131,7 @@ export const buildTable = ({
122131
rootTableIDColType: rootTableIDColType || idColType,
123132
rootTableName,
124133
versions,
134+
withinLocalizedArrayOrBlock,
125135
})
126136

127137
// split the relationsToBuild by localized and non-localized
@@ -464,5 +474,12 @@ export const buildTable = ({
464474
return result
465475
})
466476

467-
return { hasManyNumberField, hasManyTextField, relationsToBuild }
477+
return {
478+
hasLocalizedManyNumberField,
479+
hasLocalizedManyTextField,
480+
hasLocalizedRelationshipField,
481+
hasManyNumberField,
482+
hasManyTextField,
483+
relationsToBuild,
484+
}
468485
}

0 commit comments

Comments
 (0)