Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not re-generate model attributes if slug changes #144

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/model/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,23 @@ type ComposableSettings =
* should be used to generate the new setting.
*/
const modelAttributes: Array<
[ComposableSettings, ComposableSettings, (arg: string) => string]
[
// The target attribute that is being generated.
ComposableSettings,
// The base attribute from which the aforementioned attribute is generated.
ComposableSettings,
// The transformation function for generating the new attribute.
(arg: string) => string,
// Whether the target attribute must be re-generated when the base attribute changes
// during the altering of a model.
boolean,
]
> = [
['pluralSlug', 'slug', pluralize],
['name', 'slug', slugToName],
['pluralName', 'pluralSlug', slugToName],
['idPrefix', 'slug', (slug: string) => slug.slice(0, 3).toLowerCase()],
['table', 'pluralSlug', convertToSnakeCase],
['pluralSlug', 'slug', pluralize, true],
['name', 'slug', slugToName, false],
['pluralName', 'pluralSlug', slugToName, false],
['idPrefix', 'slug', (slug: string) => slug.slice(0, 3).toLowerCase(), false],
['table', 'pluralSlug', convertToSnakeCase, true],
];

/**
Expand Down Expand Up @@ -124,9 +134,13 @@ export const addDefaultModelAttributes = (model: PartialModel, isNew: boolean):
// internal comparisons, before the resulting statements hit the database.
if (isNew && !copiedModel.id) copiedModel.id = getModelIdentifier();

for (const [setting, base, generator] of modelAttributes) {
// If a custom value was provided for the setting, or the setting from which the current
// one can be generated is not available, skip the generation.
for (const [setting, base, generator, mustRegenerate] of modelAttributes) {
// If an existing model is being altered, check whether the attribute must even be
// re-generated from its base attribute, or whether it should stay outdated.
if (!(isNew || mustRegenerate)) continue;

// If a custom value was provided for the setting, or the setting from which the
// current one can be generated is not available, skip the generation.
if (copiedModel[setting] || !copiedModel[base]) continue;

// Otherwise, if possible, generate the setting.
Expand Down
10 changes: 5 additions & 5 deletions tests/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ test('alter existing model (slug)', () => {
params: [],
},
{
statement: `UPDATE "ronin_schema" SET "slug" = ?1, "pluralSlug" = ?2, "name" = ?3, "pluralName" = ?4, "idPrefix" = ?5, "table" = ?6, "ronin.updatedAt" = strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z' WHERE "slug" = ?7 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: ['user', 'users', 'User', 'Users', 'use', 'users', 'account'],
statement: `UPDATE "ronin_schema" SET "slug" = ?1, "pluralSlug" = ?2, "table" = ?3, "ronin.updatedAt" = strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z' WHERE "slug" = ?4 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: ['user', 'users', 'users', 'account'],
returning: true,
},
]);
Expand Down Expand Up @@ -402,8 +402,8 @@ test('alter existing model (slug) that has system models associated with it', ()
params: [],
},
{
statement: `UPDATE "ronin_schema" SET "slug" = ?1, "pluralSlug" = ?2, "name" = ?3, "pluralName" = ?4, "idPrefix" = ?5, "table" = ?6, "ronin.updatedAt" = strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z' WHERE "slug" = ?7 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: ['account', 'accounts', 'Account', 'Accounts', 'acc', 'accounts', 'user'],
statement: `UPDATE "ronin_schema" SET "slug" = ?1, "pluralSlug" = ?2, "table" = ?3, "ronin.updatedAt" = strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z' WHERE "slug" = ?4 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: ['account', 'accounts', 'accounts', 'user'],
returning: true,
},
]);
Expand Down Expand Up @@ -567,7 +567,7 @@ test('query a model that was just updated', () => {
// order in which the queries are provided.
expect(transaction.statements.map(({ statement }) => statement)).toEqual([
'ALTER TABLE "accounts" RENAME TO "users"',
`UPDATE "ronin_schema" SET "slug" = ?1, "pluralSlug" = ?2, "name" = ?3, "pluralName" = ?4, "idPrefix" = ?5, "table" = ?6, "ronin.updatedAt" = strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z' WHERE "slug" = ?7 RETURNING "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "name", "pluralName", "slug", "pluralSlug", "idPrefix", "table", "identifiers.name", "identifiers.slug", "fields", "indexes", "triggers", "presets"`,
`UPDATE "ronin_schema" SET "slug" = ?1, "pluralSlug" = ?2, "table" = ?3, "ronin.updatedAt" = strftime('%Y-%m-%dT%H:%M:%f', 'now') || 'Z' WHERE "slug" = ?4 RETURNING "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "name", "pluralName", "slug", "pluralSlug", "idPrefix", "table", "identifiers.name", "identifiers.slug", "fields", "indexes", "triggers", "presets"`,
'SELECT "id", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy" FROM "users" LIMIT 1',
]);
});
Expand Down
Loading