Browse Source

refactor: skip formula invalidate since type is same, better comments and logs

pull/7369/head
Pranav C 10 months ago
parent
commit
1ea17a45f4
  1. 44
      packages/nocodb/src/models/Column.ts
  2. 58
      packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts

44
packages/nocodb/src/models/Column.ts

@ -948,6 +948,7 @@ export default class Column<T = any> implements ColumnType {
colId: string, colId: string,
column: Partial<Column> & Partial<Pick<ColumnReqType, 'column_order'>>, column: Partial<Column> & Partial<Pick<ColumnReqType, 'column_order'>>,
ncMeta = Noco.ncMeta, ncMeta = Noco.ncMeta,
skipFormulaInvalidate = false,
) { ) {
const oldCol = await Column.get({ colId }, ncMeta); const oldCol = await Column.get({ colId }, ncMeta);
@ -1165,28 +1166,29 @@ export default class Column<T = any> implements ColumnType {
await NocoCache.delAll(CacheScope.SINGLE_QUERY, `${oldCol.fk_model_id}:*`); await NocoCache.delAll(CacheScope.SINGLE_QUERY, `${oldCol.fk_model_id}:*`);
const updatedColumn = await Column.get({ colId }); const updatedColumn = await Column.get({ colId });
if (!skipFormulaInvalidate) {
// invalidate formula parsed-tree in which current column is used // invalidate formula parsed-tree in which current column is used
// whenever a new request comes for that formula, it will be populated again // whenever a new request comes for that formula, it will be populated again
getFormulasReferredTheColumn({ getFormulasReferredTheColumn({
column: updatedColumn, column: updatedColumn,
columns: await Column.list({ fk_model_id: column.fk_model_id }, ncMeta), 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,
);
}
}) })
// ignore the error and continue, if formula is no longer valid it will be captured in the next run .then(async (formulas) => {
.catch((err) => { for (const formula of formulas) {
logger.error(err); 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( static async updateAlias(

58
packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts

@ -46,7 +46,7 @@ async function upgradeModels({
source, source,
}: { }: {
ncMeta: MetaService; ncMeta: MetaService;
source: any; source: Source;
}) { }) {
const models = await Model.list( const models = await Model.list(
{ {
@ -68,6 +68,8 @@ async function upgradeModels({
let isLastModifiedTimeExists = false; let isLastModifiedTimeExists = false;
for (const column of columns) { for (const column of columns) {
if (column.uidt !== UITypes.DateTime) continue; 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') { if (column.column_name === 'created_at') {
isCreatedTimeExists = true; isCreatedTimeExists = true;
await Column.update( await Column.update(
@ -78,6 +80,7 @@ async function upgradeModels({
system: true, system: true,
}, },
ncMeta, ncMeta,
true,
); );
/* Enable if planning to remove trigger /* Enable if planning to remove trigger
@ -98,6 +101,7 @@ async function upgradeModels({
au: false, au: false,
}, },
ncMeta, ncMeta,
true,
); );
} }
} }
@ -110,25 +114,24 @@ async function upgradeModels({
ncMeta.knex, ncMeta.knex,
); );
const dbColumns = ( const dbColumns =
await sqlClient.columnList({ (
tn: model.table_name, await sqlClient.columnList({
schema: source.getConfig()?.schema, tn: model.table_name,
}) schema: source.getConfig()?.schema,
)?.data?.list; })
// )?.data?.list?.map((c) => ({ ...c, column_name: c.cn })) || [];
//
// // create created_at and updated_at columns // create created_at and updated_at columns
const newColumns = []; const newColumns = [];
const existingDbColumns = []; const existingDbColumns = [];
//,
if (!isCreatedTimeExists) { 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 columnName = getUniqueColumnName(columns, 'created_at');
//
const dbColumn = dbColumns.find((c) => c.cn === columnName); const dbColumn = dbColumns.find((c) => c.cn === columnName);
// if column already exist, just update the meta
if ( if (
dbColumn && dbColumn &&
getColumnUiType(source, dbColumn) === UITypes.DateTime getColumnUiType(source, dbColumn) === UITypes.DateTime
@ -136,7 +139,7 @@ async function upgradeModels({
existingDbColumns.push({ existingDbColumns.push({
...dbColumn, ...dbColumn,
uidt: UITypes.CreatedTime, uidt: UITypes.CreatedTime,
column_name: getUniqueColumnName(columns, 'created_at'), column_name: columnName,
title: getUniqueColumnAliasName(columns, 'CreatedAt'), title: getUniqueColumnAliasName(columns, 'CreatedAt'),
system: true, system: true,
}); });
@ -145,7 +148,10 @@ async function upgradeModels({
...(await getColumnPropsFromUIDT( ...(await getColumnPropsFromUIDT(
{ {
uidt: UITypes.CreatedTime, uidt: UITypes.CreatedTime,
column_name: getUniqueColumnName(columns, 'created_at'), column_name: getUniqueColumnName(
[...columns, ...dbColumns],
'created_at',
),
title: getUniqueColumnAliasName(columns, 'CreatedAt'), title: getUniqueColumnAliasName(columns, 'CreatedAt'),
}, },
source, source,
@ -159,9 +165,9 @@ async function upgradeModels({
if (!isLastModifiedTimeExists) { if (!isLastModifiedTimeExists) {
const columnName = getUniqueColumnName(columns, 'created_at'); const columnName = getUniqueColumnName(columns, 'created_at');
//
const dbColumn = dbColumns.find((c) => c.cn === columnName); const dbColumn = dbColumns.find((c) => c.cn === columnName);
// if column already exist, just update the meta
if ( if (
dbColumn && dbColumn &&
getColumnUiType(source, dbColumn) === UITypes.DateTime getColumnUiType(source, dbColumn) === UITypes.DateTime
@ -169,7 +175,7 @@ async function upgradeModels({
existingDbColumns.push({ existingDbColumns.push({
uidt: UITypes.LastModifiedTime, uidt: UITypes.LastModifiedTime,
...dbColumn, ...dbColumn,
column_name: getUniqueColumnName(columns, 'updated_at'), column_name: columnName,
title: getUniqueColumnAliasName(columns, 'UpdatedAt'), title: getUniqueColumnAliasName(columns, 'UpdatedAt'),
system: true, system: true,
}); });
@ -178,7 +184,10 @@ async function upgradeModels({
...(await getColumnPropsFromUIDT( ...(await getColumnPropsFromUIDT(
{ {
uidt: UITypes.LastModifiedTime, uidt: UITypes.LastModifiedTime,
column_name: getUniqueColumnName(columns, 'updated_at'), column_name: getUniqueColumnName(
[...columns, ...dbColumns],
'updated_at',
),
title: getUniqueColumnAliasName(columns, 'UpdatedAt'), title: getUniqueColumnAliasName(columns, 'UpdatedAt'),
}, },
source, source,
@ -190,7 +199,11 @@ async function upgradeModels({
} }
} }
// alter table and add new columns if any
if (newColumns.length) { if (newColumns.length) {
logger.log(
`Altering table ${model.title} from source ${source.alias} for new columns`,
);
// update column in db // update column in db
const tableUpdateBody = { const tableUpdateBody = {
...model, ...model,
@ -246,10 +259,15 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
const source = new Source(_source); const source = new Source(_source);
// skip deleted project bases // 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}`); logger.log(`Upgrading source ${source.alias}`);
// update the meta props // update the meta props
await upgradeModels({ ncMeta, source }); await upgradeModels({ ncMeta, source });
logger.log(`Upgraded source ${source.alias}`);
} }
} }

Loading…
Cancel
Save