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: WithSchemaPlugin is adding schema to function arguments and causes db errors. #827

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions src/plugin/with-schema/with-schema-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { AggregateFunctionNode } from '../../operation-node/aggregate-function-node.js'
import { AliasNode } from '../../operation-node/alias-node.js'
import { FunctionNode } from '../../operation-node/function-node.js'
import { IdentifierNode } from '../../operation-node/identifier-node.js'
import { OperationNodeTransformer } from '../../operation-node/operation-node-transformer.js'
import { OperationNode } from '../../operation-node/operation-node.js'
Expand Down Expand Up @@ -34,6 +36,11 @@ const ROOT_OPERATION_NODES: Record<RootOperationNode['kind'], true> = freeze({
MergeQueryNode: true,
})

const SCHEMALESS_FUNCTIONS: Record<string, true> = {
json_agg: true,
to_json: true,
}

export class WithSchemaTransformer extends OperationNodeTransformer {
readonly #schema: string
readonly #schemableIds = new Set<string>()
Expand Down Expand Up @@ -105,6 +112,45 @@ export class WithSchemaTransformer extends OperationNodeTransformer {
}
}

protected override transformAggregateFunction(
node: AggregateFunctionNode
): AggregateFunctionNode {
const transformed = super.transformAggregateFunction(node)

return SCHEMALESS_FUNCTIONS[node.func]
? {
...transformed,
aggregated: this.#revertTableTransformations(
node.aggregated,
transformed.aggregated
),
}
: transformed
}

protected override transformFunction(node: FunctionNode): FunctionNode {
const transformed = super.transformFunction(node)

return SCHEMALESS_FUNCTIONS[node.func]
? {
...transformed,
arguments: this.#revertTableTransformations(
node.arguments,
transformed.arguments
),
}
: transformed
}

#revertTableTransformations(
before: readonly OperationNode[],
after: readonly OperationNode[]
): OperationNode[] {
return after.map((transformed, index) =>
TableNode.is(before[index]) ? before[index] : transformed
)
}

#isRootOperationNode(node: OperationNode): node is RootOperationNode {
return node.kind in ROOT_OPERATION_NODES
}
Expand Down
46 changes: 46 additions & 0 deletions test/node/src/with-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,52 @@ for (const dialect of DIALECTS.filter(

await query.execute()
})

if (dialect === 'postgres') {
it('should not add schema for json_agg parameters', async () => {
const query = ctx.db
.withSchema('mammals')
.selectFrom('pet')
.select((eb) => [
eb.fn.jsonAgg('pet').as('one'),
eb.fn.jsonAgg(eb.table('pet')).as('two'),
])

testSql(query, dialect, {
postgres: {
sql: 'select json_agg("pet") as "one", json_agg("pet") as "two" from "mammals"."pet"',
parameters: [],
},
mysql: NOT_SUPPORTED,
mssql: NOT_SUPPORTED,
sqlite: NOT_SUPPORTED,
})

await query.execute()
})

it('should not add schema for to_json parameters', async () => {
const query = ctx.db
.withSchema('mammals')
.selectFrom('pet')
.select((eb) => [
eb.fn.toJson('pet').as('one'),
eb.fn.toJson(eb.table('pet')).as('two'),
])

testSql(query, dialect, {
postgres: {
sql: 'select to_json("pet") as "one", to_json("pet") as "two" from "mammals"."pet"',
parameters: [],
},
mysql: NOT_SUPPORTED,
mssql: NOT_SUPPORTED,
sqlite: NOT_SUPPORTED,
})

await query.execute()
})
}
})

describe('insert into', () => {
Expand Down