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

Fix table updates wiping out view calculation fields. #14756

Merged
merged 2 commits into from
Oct 10, 2024
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
53 changes: 53 additions & 0 deletions packages/server/src/api/routes/tests/viewV2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,59 @@ describe.each([
})
})
})

describe("calculation views", () => {
it("should not remove calculation columns when modifying table schema", async () => {
let table = await config.api.table.save(
saveTableRequest({
schema: {
name: {
name: "name",
type: FieldType.STRING,
},
age: {
name: "age",
type: FieldType.NUMBER,
},
},
})
)

let view = await config.api.viewV2.create({
tableId: table._id!,
name: generator.guid(),
type: ViewV2Type.CALCULATION,
schema: {
sum: {
visible: true,
calculationType: CalculationType.SUM,
field: "age",
},
},
})

table = await config.api.table.get(table._id!)
await config.api.table.save({
...table,
schema: {
...table.schema,
name: {
name: "name",
type: FieldType.STRING,
constraints: { presence: true },
},
},
})

view = await config.api.viewV2.get(view.id)
expect(Object.keys(view.schema!).sort()).toEqual([
"age",
"id",
"name",
"sum",
])
})
})
})

describe("row operations", () => {
Expand Down
43 changes: 21 additions & 22 deletions packages/server/src/api/routes/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,28 @@ const OPTIONAL_NUMBER = Joi.number().optional().allow(null)
const OPTIONAL_BOOLEAN = Joi.boolean().optional().allow(null)
const APP_NAME_REGEX = /^[\w\s]+$/

const validateViewSchemas: CustomValidator<Table> = (table, helpers) => {
if (table.views && Object.entries(table.views).length) {
const requiredFields = Object.entries(table.schema)
.filter(([_, v]) => isRequired(v.constraints))
const validateViewSchemas: CustomValidator<Table> = (table, joiHelpers) => {
if (!table.views || Object.keys(table.views).length === 0) {
return table
}
const required = Object.keys(table.schema).filter(key =>
isRequired(table.schema[key].constraints)
)
if (required.length === 0) {
return table
}
for (const view of Object.values(table.views)) {
if (!sdk.views.isV2(view) || helpers.views.isCalculationView(view)) {
continue
}
Comment on lines +34 to +36
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only meaningful change in here, but I did a little refactoring to reduce the nesting and make this more readable while I was here.

const editable = Object.entries(view.schema || {})
.filter(([_, f]) => f.visible && !f.readonly)
.map(([key]) => key)
if (requiredFields.length) {
for (const view of Object.values(table.views)) {
if (!sdk.views.isV2(view)) {
continue
}

const editableViewFields = Object.entries(view.schema || {})
.filter(([_, f]) => f.visible && !f.readonly)
.map(([key]) => key)
const missingField = requiredFields.find(
f => !editableViewFields.includes(f)
)
if (missingField) {
return helpers.message({
custom: `To make field "${missingField}" required, this field must be present and writable in views: ${view.name}.`,
})
}
}
const missingField = required.find(f => !editable.includes(f))
if (missingField) {
return joiHelpers.message({
custom: `To make field "${missingField}" required, this field must be present and writable in views: ${view.name}.`,
})
}
}
return table
Expand Down
10 changes: 9 additions & 1 deletion packages/server/src/sdk/app/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ async function guardCalculationViewSchema(
continue
}

if (!schema.field) {
throw new HTTPError(
`Calculation field "${name}" is missing a "field" property`,
400
)
}
Comment on lines +100 to +105
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a separate problem I ran into accidentally. It would error previously, but the message was unhelpful. This just improves the error message.


const targetSchema = table.schema[schema.field]
if (!targetSchema) {
throw new HTTPError(
Expand Down Expand Up @@ -377,7 +384,8 @@ export function syncSchema(

if (view.schema) {
for (const fieldName of Object.keys(view.schema)) {
if (!schema[fieldName]) {
const viewSchema = view.schema[fieldName]
if (!helpers.views.isCalculationField(viewSchema) && !schema[fieldName]) {
Comment on lines +387 to +388
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the primary fix for the problem this PR addresses.

delete view.schema[fieldName]
}
}
Expand Down
Loading