Skip to content

Commit a03d9dd

Browse files
committed
[WIP add test] fix(server-core): Handle empty query in getSqlGenerator
api-gateway calls getSqlGenerator with empty query and concrete data source to initialize SQL API But because query is empty `sqlGenerator.dataSource` can be undefined, and it would trigger re-creating query with new data source, which would be `default`
1 parent cd90d56 commit a03d9dd

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

packages/cubejs-server-core/src/core/CompilerApi.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,26 @@ export class CompilerApi {
137137
throw new Error(`Unknown dbType: ${dbType}`);
138138
}
139139

140+
// sqlGenerator.dataSource can return undefined for query without members
141+
// Queries like this are used by api-gateway to initialize SQL API
142+
// At the same time, those queries should use concrete dataSource, so we should be good to go with it
140143
dataSource = compilers.compiler.withQuery(sqlGenerator, () => sqlGenerator.dataSource);
141-
const _dbType = await this.getDbType(dataSource);
142-
if (dataSource !== 'default' && dbType !== _dbType) {
143-
// TODO consider more efficient way than instantiating query
144-
sqlGenerator = await this.createQueryByDataSource(
145-
compilers,
146-
query,
147-
dataSource,
148-
_dbType
149-
);
144+
if (dataSource !== undefined) {
145+
const _dbType = await this.getDbType(dataSource);
146+
if (dataSource !== 'default' && dbType !== _dbType) {
147+
// TODO consider more efficient way than instantiating query
148+
sqlGenerator = await this.createQueryByDataSource(
149+
compilers,
150+
query,
151+
dataSource,
152+
_dbType
153+
);
150154

151-
if (!sqlGenerator) {
152-
throw new Error(`Can't find dialect for '${dataSource}' data source: ${_dbType}`);
155+
if (!sqlGenerator) {
156+
throw new Error(
157+
`Can't find dialect for '${dataSource}' data source: ${_dbType}`
158+
);
159+
}
153160
}
154161
}
155162

0 commit comments

Comments
 (0)