Skip to content

Commit

Permalink
Correctly update fields when changing their slug (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Feb 19, 2025
1 parent ab31f1d commit 9468895
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,9 +1107,14 @@ export const transformMetaQuery = (
newSlug,
Object.getOwnPropertyDescriptor(targetEntities, slug)!,
);

// Assign the newly generated attributes to the new entity.
Object.assign(targetEntities[newSlug], entityValue);

// Remove the old entity.
delete targetEntities[slug];

const value = prepareStatementValue(statementParams, entityValue);
const value = prepareStatementValue(statementParams, targetEntities[newSlug]);
json = `json_insert(json_remove(${field}, '$.${slug}'), '$.${newSlug}', ${value})`;
}
// Otherwise, just update the existing property.
Expand Down
12 changes: 10 additions & 2 deletions tests/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ test('alter existing field (slug)', () => {
},
{
statement: `UPDATE "ronin_schema" SET "fields" = json_insert(json_remove("fields", '$.email'), '$.emailAddress', ?1), "ronin.updatedAt" = strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z' WHERE "slug" = ?2 RETURNING "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "name", "pluralName", "slug", "pluralSlug", "idPrefix", "table", "identifiers.name", "identifiers.slug", "fields", "indexes", "triggers", "presets"`,
params: [JSON.stringify({ name: 'Email Address' }), 'account'],
params: [JSON.stringify({ type: 'string', name: 'Email Address' }), 'account'],
returning: true,
},
]);
Expand Down Expand Up @@ -819,7 +819,15 @@ test('alter existing field (slug) with many-cardinality relationship', () => {
},
{
statement: `UPDATE "ronin_schema" SET "fields" = json_insert(json_remove("fields", '$.followers'), '$.subscribers', ?1), "ronin.updatedAt" = strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z' WHERE "slug" = ?2 RETURNING "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "name", "pluralName", "slug", "pluralSlug", "idPrefix", "table", "identifiers.name", "identifiers.slug", "fields", "indexes", "triggers", "presets"`,
params: [JSON.stringify({ name: 'Subscribers' }), 'account'],
params: [
JSON.stringify({
type: 'link',
target: 'account',
kind: 'many',
name: 'Subscribers',
}),
'account',
],
returning: true,
},
]);
Expand Down

0 comments on commit 9468895

Please sign in to comment.