diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index e3a003bc0f..852f5eb8d0 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -948,6 +948,7 @@ export default class Column implements ColumnType { colId: string, column: Partial & Partial>, ncMeta = Noco.ncMeta, + skipFormulaInvalidate = false, ) { const oldCol = await Column.get({ colId }, ncMeta); @@ -1165,28 +1166,29 @@ export default class Column implements ColumnType { await NocoCache.delAll(CacheScope.SINGLE_QUERY, `${oldCol.fk_model_id}:*`); const updatedColumn = await Column.get({ colId }); - - // invalidate formula parsed-tree in which current column is used - // whenever a new request comes for that formula, it will be populated again - getFormulasReferredTheColumn({ - column: updatedColumn, - columns: await Column.list({ fk_model_id: column.fk_model_id }, ncMeta), - }) - .then(async (formulas) => { - for (const formula of formulas) { - await FormulaColumn.update( - formula.id, - { - parsed_tree: null, - }, - ncMeta, - ); - } + if (!skipFormulaInvalidate) { + // invalidate formula parsed-tree in which current column is used + // whenever a new request comes for that formula, it will be populated again + getFormulasReferredTheColumn({ + column: updatedColumn, + columns: await Column.list({ fk_model_id: column.fk_model_id }, ncMeta), }) - // ignore the error and continue, if formula is no longer valid it will be captured in the next run - .catch((err) => { - logger.error(err); - }); + .then(async (formulas) => { + for (const formula of formulas) { + await FormulaColumn.update( + formula.id, + { + parsed_tree: null, + }, + ncMeta, + ); + } + }) + // ignore the error and continue, if formula is no longer valid it will be captured in the next run + .catch((err) => { + logger.error(err); + }); + } } static async updateAlias( diff --git a/packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts b/packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts index 0ab6bd00c3..37b05b3185 100644 --- a/packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts +++ b/packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts @@ -46,7 +46,7 @@ async function upgradeModels({ source, }: { ncMeta: MetaService; - source: any; + source: Source; }) { const models = await Model.list( { @@ -68,6 +68,8 @@ async function upgradeModels({ let isLastModifiedTimeExists = false; for (const column of columns) { if (column.uidt !== UITypes.DateTime) continue; + + // if column is created_at or updated_at, update the uidt in meta if (column.column_name === 'created_at') { isCreatedTimeExists = true; await Column.update( @@ -78,6 +80,7 @@ async function upgradeModels({ system: true, }, ncMeta, + true, ); /* Enable if planning to remove trigger @@ -98,6 +101,7 @@ async function upgradeModels({ au: false, }, ncMeta, + true, ); } } @@ -110,25 +114,24 @@ async function upgradeModels({ ncMeta.knex, ); - const dbColumns = ( - await sqlClient.columnList({ - tn: model.table_name, - schema: source.getConfig()?.schema, - }) - )?.data?.list; - // - // - // // create created_at and updated_at columns + const dbColumns = + ( + await sqlClient.columnList({ + tn: model.table_name, + schema: source.getConfig()?.schema, + }) + )?.data?.list?.map((c) => ({ ...c, column_name: c.cn })) || []; + + // create created_at and updated_at columns const newColumns = []; const existingDbColumns = []; - //, + if (!isCreatedTimeExists) { - // - // // check column exist and add to meta if found + // check column exist and add to meta if found const columnName = getUniqueColumnName(columns, 'created_at'); - // const dbColumn = dbColumns.find((c) => c.cn === columnName); + // if column already exist, just update the meta if ( dbColumn && getColumnUiType(source, dbColumn) === UITypes.DateTime @@ -136,7 +139,7 @@ async function upgradeModels({ existingDbColumns.push({ ...dbColumn, uidt: UITypes.CreatedTime, - column_name: getUniqueColumnName(columns, 'created_at'), + column_name: columnName, title: getUniqueColumnAliasName(columns, 'CreatedAt'), system: true, }); @@ -145,7 +148,10 @@ async function upgradeModels({ ...(await getColumnPropsFromUIDT( { uidt: UITypes.CreatedTime, - column_name: getUniqueColumnName(columns, 'created_at'), + column_name: getUniqueColumnName( + [...columns, ...dbColumns], + 'created_at', + ), title: getUniqueColumnAliasName(columns, 'CreatedAt'), }, source, @@ -159,9 +165,9 @@ async function upgradeModels({ if (!isLastModifiedTimeExists) { const columnName = getUniqueColumnName(columns, 'created_at'); - // const dbColumn = dbColumns.find((c) => c.cn === columnName); + // if column already exist, just update the meta if ( dbColumn && getColumnUiType(source, dbColumn) === UITypes.DateTime @@ -169,7 +175,7 @@ async function upgradeModels({ existingDbColumns.push({ uidt: UITypes.LastModifiedTime, ...dbColumn, - column_name: getUniqueColumnName(columns, 'updated_at'), + column_name: columnName, title: getUniqueColumnAliasName(columns, 'UpdatedAt'), system: true, }); @@ -178,7 +184,10 @@ async function upgradeModels({ ...(await getColumnPropsFromUIDT( { uidt: UITypes.LastModifiedTime, - column_name: getUniqueColumnName(columns, 'updated_at'), + column_name: getUniqueColumnName( + [...columns, ...dbColumns], + 'updated_at', + ), title: getUniqueColumnAliasName(columns, 'UpdatedAt'), }, source, @@ -190,7 +199,11 @@ async function upgradeModels({ } } + // alter table and add new columns if any if (newColumns.length) { + logger.log( + `Altering table ${model.title} from source ${source.alias} for new columns`, + ); // update column in db const tableUpdateBody = { ...model, @@ -246,10 +259,15 @@ export default async function ({ ncMeta }: NcUpgraderCtx) { const source = new Source(_source); // skip deleted project bases - if (await source.getProject(ncMeta).then((p) => !p || p.deleted)) continue; + if (await source.getProject(ncMeta).then((p) => !p || p.deleted)) { + logger.log(`Skipped deleted base source ${source.alias}`); + continue; + } logger.log(`Upgrading source ${source.alias}`); // update the meta props await upgradeModels({ ncMeta, source }); + + logger.log(`Upgraded source ${source.alias}`); } }