Browse Source

refactor(nocodb): formula dry run logic

pull/4776/head
Wing-Kam Wong 2 years ago
parent
commit
c20d0b7ebe
  1. 24
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts
  2. 25
      packages/nocodb/src/lib/meta/api/columnApis.ts

24
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -1210,29 +1210,9 @@ class BaseModelSqlv2 {
formula.formula, formula.formula,
null, null,
this.dbDriver, this.dbDriver,
this.model this.model,
column
); );
try {
// dry run the existing qb.builder to see if it will break the grid view or not
// if so, set formula error and show empty selectQb instead
await this.dbDriver(this.tnPath)
.select(qb.builder)
.as(sanitize(column.title));
// clean the previous formula error if the formula works this time
if (formula.error) {
await FormulaColumn.update(formula.id, {
error: null,
});
}
} catch (e) {
console.error(e);
// add formula error to show in UI
await FormulaColumn.update(formula.id, {
error: e.message,
});
throw new Error(`Formula error: ${e.message}`);
}
return qb; return qb;
} }

25
packages/nocodb/src/lib/meta/api/columnApis.ts

@ -524,18 +524,11 @@ export async function columnAdd(
colBody.formula_raw || colBody.formula, colBody.formula_raw || colBody.formula,
table.columns table.columns
); );
try { try {
// test the query to see if it is valid in db level
const dbDriver = NcConnectionMgrv2.get(base); const dbDriver = NcConnectionMgrv2.get(base);
// retrieve the builder from formulaQueryBuilderv2 await formulaQueryBuilderv2(colBody.formula, null, dbDriver, table);
const builder = await formulaQueryBuilderv2(
colBody.formula,
null,
dbDriver,
table
);
// dry-run it to see if the query is valid
// if not, we show an error in UI to prevent from breaking the grid view
await dbDriver(table.table_name).select(builder).as('dry-run');
} catch (e) { } catch (e) {
console.error(e); console.error(e);
NcError.badRequest('Invalid Formula'); NcError.badRequest('Invalid Formula');
@ -779,17 +772,9 @@ export async function columnUpdate(req: Request, res: Response<TableType>) {
); );
try { try {
// test the query to see if it is valid in db level
const dbDriver = NcConnectionMgrv2.get(base); const dbDriver = NcConnectionMgrv2.get(base);
// retrieve the builder from formulaQueryBuilderv2 await formulaQueryBuilderv2(colBody.formula, null, dbDriver, table);
const builder = await formulaQueryBuilderv2(
colBody.formula,
null,
dbDriver,
table
);
// dry-run it to see if the query is valid
// if not, we show an error in UI to prevent from breaking the grid view
await dbDriver(table.table_name).select(builder).as('dry-run');
} catch (e) { } catch (e) {
console.error(e); console.error(e);
NcError.badRequest('Invalid Formula'); NcError.badRequest('Invalid Formula');

Loading…
Cancel
Save