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

MariaDB - avoid using correlated sub-queries #14630

Merged
merged 11 commits into from
Sep 24, 2024
Prev Previous commit
Next Next commit
PR comments and type improvements.
  • Loading branch information
mike12345567 committed Sep 24, 2024
commit 956df101e84891aeef9e7c49a22772e03ae33ef9
8 changes: 7 additions & 1 deletion packages/server/src/api/routes/tests/search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe.each([
table = await createTable(
{
name: { name: "name", type: FieldType.STRING },
//@ts-ignore - API accepts this structure, will build out rest of definition
productCat: {
type: FieldType.LINK,
relationshipType: type,
Expand Down Expand Up @@ -2297,12 +2298,17 @@ describe.each([
}
})

it("can only pull 500 related rows", async () => {
it("can only pull 10 related rows", async () => {
await withCoreEnv({ SQL_MAX_RELATED_ROWS: "10" }, async () => {
const response = await expectQuery({}).toContain([{ name: "foo" }])
expect(response.rows[0].productCat).toBeArrayOfSize(10)
mike12345567 marked this conversation as resolved.
Show resolved Hide resolved
})
})

it("can pull max rows when env not set (defaults to 500)", async () => {
const response = await expectQuery({}).toContain([{ name: "foo" }])
expect(response.rows[0].productCat).toBeArrayOfSize(11)
})
})
;(isSqs || isLucene) &&
describe("relations to same table", () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/server/src/sdk/app/tables/external/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ export async function save(

// add in the new table for relationship purposes
tables[tableToSave.name] = tableToSave
cleanupRelationships(tableToSave, tables, oldTable)
if (oldTable) {
cleanupRelationships(tableToSave, tables, { oldTable })
}

const operation = tableId ? Operation.UPDATE_TABLE : Operation.CREATE_TABLE
await makeTableRequest(
Expand Down Expand Up @@ -259,7 +261,7 @@ export async function destroy(datasourceId: string, table: Table) {
const operation = Operation.DELETE_TABLE
if (tables) {
await makeTableRequest(datasource, operation, table, tables)
cleanupRelationships(table, tables)
cleanupRelationships(table, tables, { deleting: true })
delete tables[table.name]
datasource.entities = tables
}
Expand Down
20 changes: 14 additions & 6 deletions packages/server/src/sdk/app/tables/external/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@ import { cloneDeep } from "lodash/fp"
export function cleanupRelationships(
table: Table,
tables: Record<string, Table>,
oldTable?: Table
) {
if (!oldTable) {
return
}
opts: { oldTable: Table }
): void
export function cleanupRelationships(
table: Table,
tables: Record<string, Table>,
opts: { deleting: boolean }
): void
export function cleanupRelationships(
table: Table,
tables: Record<string, Table>,
opts?: { oldTable?: Table; deleting?: boolean }
): void {
const oldTable = opts?.oldTable
const tableToIterate = oldTable ? oldTable : table
// clean up relationships in couch table schemas
for (let [key, schema] of Object.entries(tableToIterate.schema)) {
if (
schema.type === FieldType.LINK &&
oldTable.schema[key] != null &&
(opts?.deleting || oldTable?.schema[key] != null) &&
table.schema[key] == null
) {
const schemaTableId = schema.tableId
Expand Down
10 changes: 4 additions & 6 deletions packages/server/src/tests/utilities/structures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,10 @@ export function fullSchemaWithoutLinks({
allRequired,
}: {
allRequired?: boolean
}) {
const schema: {
[type in Exclude<FieldType, FieldType.LINK>]: FieldSchema & { type: type }
} = {
}): {
[type in Exclude<FieldType, FieldType.LINK>]: FieldSchema & { type: type }
} {
return {
[FieldType.STRING]: {
name: "string",
type: FieldType.STRING,
Expand Down Expand Up @@ -741,8 +741,6 @@ export function fullSchemaWithoutLinks({
},
},
}

return schema
}
export function basicAttachment() {
return {
Expand Down