From 3e4f59d11f6149d411ca50871f485ead0591a9ab Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 4 Jan 2024 13:19:20 +0000 Subject: [PATCH] feat: create column with different name if already column exist --- packages/nocodb/src/db/BaseModelSqlv2.ts | 31 ++++++++--- .../nocodb/src/services/columns.service.ts | 55 ++++++++++--------- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 25837c2008..04f9054947 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -101,12 +101,23 @@ function checkColumnRequired( return !fields || fields.includes(column.title); } -export function getColumnName(column: Column) { +export function getColumnName(column: Column, columns: Column[] = []) { switch (column.uidt) { - case UITypes.CreateTime: - return 'created_at'; - case UITypes.LastModifiedTime: - return 'updated_at'; + case UITypes.CreateTime: { + const createdTimeSystemCol = columns.find( + (col) => col.system && col.uidt === UITypes.CreateTime, + ); + if (createdTimeSystemCol) return createdTimeSystemCol.column_name; + return column.column_name || 'created_at'; + } + case UITypes.LastModifiedTime: { + const lastModifiedTimeSystemCol = columns.find( + (col) => col.system && col.uidt === UITypes.LastModifiedTime, + ); + if (lastModifiedTimeSystemCol) + return lastModifiedTimeSystemCol.column_name; + return column.column_name || 'created_at'; + } default: return column.column_name; } @@ -2128,7 +2139,10 @@ class BaseModelSqlv2 { case UITypes.LastModifiedTime: case UITypes.DateTime: { - const columnName = getColumnName(column); + const columnName = getColumnName( + column, + await this.model.getColumns(), + ); if (this.isMySQL) { // MySQL stores timestamp in UTC but display in timezone // To verify the timezone, run `SELECT @@global.time_zone, @@session.time_zone;` @@ -5601,10 +5615,11 @@ class BaseModelSqlv2 { knex?: XKnex; }) { const columnName = await model.getColumns().then((columns) => { - return columns.find((c) => c.uidt === UITypes.LastModifiedTime)?.column_name; + return columns.find((c) => c.uidt === UITypes.LastModifiedTime) + ?.column_name; }); - if(!columnName) return; + if (!columnName) return; const qb = knex(model.table_name).update({ [columnName]: Noco.ncMeta.now(), diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.ts index e1c45a84e3..4c735a1280 100644 --- a/packages/nocodb/src/services/columns.service.ts +++ b/packages/nocodb/src/services/columns.service.ts @@ -1688,34 +1688,36 @@ export class ColumnsService { case UITypes.CreateTime: case UITypes.LastModifiedTime: { + const columns = await table.getColumns(); // check if column already exists, then just create a new column in meta // else create a new column in meta and db - const existingColumn = await table.getColumns().then((col) => { - return col.find( - (c) => c.uidt === colBody.uidt && c.system, - // || - // c.column_name === - // (c.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'), - ); - }); + const existingColumn = columns.find( + (c) => c.uidt === colBody.uidt && c.system, + // || + // c.column_name === + // (c.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'), + ); if (!existingColumn) { - const sqlClient = await reuseOrSave('sqlClient', reuse, async () => - NcConnectionMgrv2.getSqlClient(source), - ); - const dbColumns = ( - await sqlClient.columnList({ - tn: table.table_name, - schema: source.getConfig()?.schema, - }) - )?.data?.list; + let columnName = + colBody.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'; + // const sqlClient = await reuseOrSave('sqlClient', reuse, async () => + // NcConnectionMgrv2.getSqlClient(source), + // ); + // const dbColumns = ( + // await sqlClient.columnList({ + // tn: table.table_name, + // schema: source.getConfig()?.schema, + // }) + // )?.data?.list; // todo: check type as well - const dbColumn = dbColumns.find( - (c) => - c.column_name === - (c.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'), - ); + const dbColumn = columns.find((c) => c.column_name === columnName); + + if (dbColumn) { + columnName = getUniqueColumnName(columns, columnName); + } + if (!dbColumn) { // create column in db @@ -1752,11 +1754,11 @@ export class ColumnsService { system: 1, fk_model_id: table.id, }); - await Column.insert({ - ...colBody, - fk_model_id: table.id, - }); } + await Column.insert({ + ...colBody, + fk_model_id: table.id, + }); } break; default: @@ -2775,6 +2777,7 @@ export class ColumnsService { await Column.update(column.id, colBody); } } + async columnsHash(tableId: string) { const table = await Model.getWithInfo({ id: tableId,