Browse Source

fix: circular ref - validation correction

pull/7268/head
Pranav C 11 months ago
parent
commit
76adc81c35
  1. 8
      packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts
  2. 6
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  3. 1
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  4. 7
      packages/nocodb/src/services/columns.service.ts

8
packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts

@ -10,7 +10,7 @@ describe('Formula parsing and type validation', () => {
formula: '1 + 2', formula: '1 + 2',
columns: [], columns: [],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
getMeta: async () => ({}) getMeta: async () => ({}),
}); });
expect(result.dataType).toEqual(FormulaDataTypes.NUMERIC); expect(result.dataType).toEqual(FormulaDataTypes.NUMERIC);
@ -27,7 +27,7 @@ describe('Formula parsing and type validation', () => {
}, },
], ],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
getMeta: async () => ({}) getMeta: async () => ({}),
}); });
expect(result.dataType).toEqual(FormulaDataTypes.STRING); expect(result.dataType).toEqual(FormulaDataTypes.STRING);
@ -49,7 +49,7 @@ describe('Formula parsing and type validation', () => {
}, },
], ],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
getMeta: async () => ({}) getMeta: async () => ({}),
}); });
expect(result.dataType).toEqual(FormulaDataTypes.STRING); expect(result.dataType).toEqual(FormulaDataTypes.STRING);
@ -69,7 +69,7 @@ describe('Formula parsing and type validation', () => {
}, },
], ],
clientOrSqlUi: 'mysql2', clientOrSqlUi: 'mysql2',
getMeta: async () => ({}) getMeta: async () => ({}),
}); });
expect(result1.dataType).toEqual(FormulaDataTypes.NUMERIC); expect(result1.dataType).toEqual(FormulaDataTypes.NUMERIC);

6
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -1400,6 +1400,7 @@ async function extractColumnIdentifierType({
export async function validateFormulaAndExtractTreeWithType({ export async function validateFormulaAndExtractTreeWithType({
formula, formula,
column,
columns, columns,
clientOrSqlUi, clientOrSqlUi,
getMeta, getMeta,
@ -1420,6 +1421,7 @@ export async function validateFormulaAndExtractTreeWithType({
| typeof MssqlUi | typeof MssqlUi
| typeof SnowflakeUi | typeof SnowflakeUi
| typeof PgUi; | typeof PgUi;
column?: ColumnType;
getMeta: (tableId: string) => Promise<any>; getMeta: (tableId: string) => Promise<any>;
}) { }) {
const colAliasToColMap = {}; const colAliasToColMap = {};
@ -1582,9 +1584,9 @@ export async function validateFormulaAndExtractTreeWithType({
res.name = col.id; res.name = col.id;
if (col?.uidt === UITypes.Formula) { if (col?.uidt === UITypes.Formula && column) {
// check for circular reference // check for circular reference
checkForCircularFormulaRef(col, parsedTree, columns); checkForCircularFormulaRef(column, parsedTree, columns);
const formulaRes = const formulaRes =
col.colOptions?.parsed_tree || col.colOptions?.parsed_tree ||

1
packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

@ -80,6 +80,7 @@ async function _formulaQueryBuilder(
tree = await validateFormulaAndExtractTreeWithType({ tree = await validateFormulaAndExtractTreeWithType({
formula: _tree.replaceAll('{{', '{').replaceAll('}}', '}'), formula: _tree.replaceAll('{{', '{').replaceAll('}}', '}'),
columns, columns,
column,
clientOrSqlUi: baseModelSqlv2.clientType as clientOrSqlUi: baseModelSqlv2.clientType as
| 'mysql' | 'mysql'
| 'pg' | 'pg'

7
packages/nocodb/src/services/columns.service.ts

@ -213,6 +213,7 @@ export class ColumnsService {
colBody.parsed_tree = await validateFormulaAndExtractTreeWithType({ colBody.parsed_tree = await validateFormulaAndExtractTreeWithType({
formula: colBody.formula_raw || colBody.formula, formula: colBody.formula_raw || colBody.formula,
columns: table.columns, columns: table.columns,
column,
clientOrSqlUi: source.type, clientOrSqlUi: source.type,
getMeta: async (modelId) => { getMeta: async (modelId) => {
const model = await Model.get(modelId); const model = await Model.get(modelId);
@ -947,6 +948,7 @@ export class ColumnsService {
parsed_tree: await validateFormulaAndExtractTreeWithType({ parsed_tree: await validateFormulaAndExtractTreeWithType({
formula: new_formula_raw, formula: new_formula_raw,
columns: table.columns, columns: table.columns,
column,
clientOrSqlUi: source.type, clientOrSqlUi: source.type,
getMeta: async (modelId) => { getMeta: async (modelId) => {
const model = await Model.get(modelId); const model = await Model.get(modelId);
@ -1018,6 +1020,7 @@ export class ColumnsService {
parsed_tree: await validateFormulaAndExtractTreeWithType({ parsed_tree: await validateFormulaAndExtractTreeWithType({
formula: new_formula_raw, formula: new_formula_raw,
columns: table.columns, columns: table.columns,
column,
clientOrSqlUi: source.type, clientOrSqlUi: source.type,
getMeta: async (modelId) => { getMeta: async (modelId) => {
const model = await Model.get(modelId); const model = await Model.get(modelId);
@ -1236,6 +1239,10 @@ export class ColumnsService {
formula: formula:
colBody.formula_raw || colBody.formula_raw ||
colBody.formula?.replaceAll('{{', '{').replaceAll('}}', '}'), colBody.formula?.replaceAll('{{', '{').replaceAll('}}', '}'),
column:{
...colBody,
colOptions: colBody,
},
columns: table.columns, columns: table.columns,
clientOrSqlUi: source.type, clientOrSqlUi: source.type,
getMeta: async (modelId) => { getMeta: async (modelId) => {

Loading…
Cancel
Save