From e1bee8a068f7c04eea88894db5c3b4b87f6ddd43 Mon Sep 17 00:00:00 2001 From: mertmit Date: Fri, 17 Feb 2023 15:10:27 +0300 Subject: [PATCH 1/4] fix: map first column as display value if no pk Signed-off-by: mertmit --- .../src/lib/meta/api/helpers/populateMeta.ts | 2 ++ .../meta/helpers/mapDefaultDisplayValue.ts | 24 ++++++++----------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/nocodb/src/lib/meta/api/helpers/populateMeta.ts b/packages/nocodb/src/lib/meta/api/helpers/populateMeta.ts index ff7861a529..4fe72f70ac 100644 --- a/packages/nocodb/src/lib/meta/api/helpers/populateMeta.ts +++ b/packages/nocodb/src/lib/meta/api/helpers/populateMeta.ts @@ -228,6 +228,8 @@ export async function populateMeta(base: Base, project: Project): Promise { return async () => { const columns = (await sqlClient.columnList({ tn: table.table_name })) ?.data?.list; + + mapDefaultDisplayValue(columns); /* create nc_models and its rows if it doesn't exists */ models2[table.table_name] = await Model.insert(project.id, base.id, { diff --git a/packages/nocodb/src/lib/meta/helpers/mapDefaultDisplayValue.ts b/packages/nocodb/src/lib/meta/helpers/mapDefaultDisplayValue.ts index 8f12d40d13..3fe911f447 100644 --- a/packages/nocodb/src/lib/meta/helpers/mapDefaultDisplayValue.ts +++ b/packages/nocodb/src/lib/meta/helpers/mapDefaultDisplayValue.ts @@ -4,31 +4,27 @@ export default function mapDefaultDisplayValue( columnsArr: Array ): void | T { if (!columnsArr.some((column) => column.pv)) { - let len = columnsArr.length; - let pkIndex = -1; - - while (len--) { - if (columnsArr[len].pk) { - pkIndex = len; - break; - } - } + const pkIndex = columnsArr.findIndex((column) => column.pk) // if PK is at the end of table if (pkIndex === columnsArr.length - 1) { if (pkIndex > 0) { columnsArr[pkIndex - 1].pv = true; return columnsArr[pkIndex - 1]; + } else if (columnsArr.length > 0) { + columnsArr[0].pv = true; + return columnsArr[0]; } - } // pk is not at the end of table - else if (pkIndex > -1) { + } else if (pkIndex > -1) { columnsArr[pkIndex + 1].pv = true; return columnsArr[pkIndex + 1]; - } // no pk at all - else { - // todo: + } else { + if (columnsArr.length > 0) { + columnsArr[0].pv = true; + return columnsArr[0]; + } } } } From 500c017d752dac143979f2f35fd7c3ac54b6b868 Mon Sep 17 00:00:00 2001 From: mertmit Date: Fri, 17 Feb 2023 15:12:10 +0300 Subject: [PATCH 2/4] fix: keep single display value per model Signed-off-by: mertmit --- packages/nocodb/src/lib/models/Model.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/nocodb/src/lib/models/Model.ts b/packages/nocodb/src/lib/models/Model.ts index c22c115d02..e9070d6d76 100644 --- a/packages/nocodb/src/lib/models/Model.ts +++ b/packages/nocodb/src/lib/models/Model.ts @@ -561,14 +561,14 @@ export default class Model implements TableType { ncMeta = Noco.ncMeta ) { const model = await this.getWithInfo({ id: tableId }); - const currentPvCol = model.displayValue; const newPvCol = model.columns.find((c) => c.id === columnId); if (!newPvCol) NcError.badRequest('Column not found'); - if (currentPvCol) { + // drop existing primary column/s + for (const col of model.columns?.filter((c) => c.pv) || []) { // get existing cache - const key = `${CacheScope.COLUMN}:${currentPvCol.id}`; + const key = `${CacheScope.COLUMN}:${col.id}`; const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT); if (o) { o.pv = false; @@ -583,7 +583,7 @@ export default class Model implements TableType { { pv: false, }, - currentPvCol.id + col.id ); } From 95f357d2bf68456721ea3065d74d1a7bffa26813 Mon Sep 17 00:00:00 2001 From: mertmit Date: Fri, 17 Feb 2023 15:48:23 +0300 Subject: [PATCH 3/4] fix: handle missing display value in upgrader Signed-off-by: mertmit --- .../ncStickyColumnUpgrader.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/nocodb/src/lib/version-upgrader/ncStickyColumnUpgrader.ts b/packages/nocodb/src/lib/version-upgrader/ncStickyColumnUpgrader.ts index 67b02ce842..0a6e16c21e 100644 --- a/packages/nocodb/src/lib/version-upgrader/ncStickyColumnUpgrader.ts +++ b/packages/nocodb/src/lib/version-upgrader/ncStickyColumnUpgrader.ts @@ -38,6 +38,50 @@ export default async function ({ ncMeta }: NcUpgraderCtx) { view_columns_meta.push(col_meta); } + // if no display value column is set + if (!view_columns_meta.some((column) => column.pv)) { + const pkIndex = view_columns_meta.findIndex((column) => column.pk) + + // if PK is at the end of table + if (pkIndex === view_columns_meta.length - 1) { + if (pkIndex > 0) { + await ncMeta.metaUpdate( + null, + null, + MetaTable.COLUMNS, + { pv: true }, + view_columns_meta[pkIndex - 1].id + ); + } else if (view_columns_meta.length > 0) { + await ncMeta.metaUpdate( + null, + null, + MetaTable.COLUMNS, + { pv: true }, + view_columns_meta[0].id + ); + } + // pk is not at the end of table + } else if (pkIndex > -1) { + await ncMeta.metaUpdate( + null, + null, + MetaTable.COLUMNS, + { pv: true }, + view_columns_meta[pkIndex + 1].id + ); + // no pk at all + } else if (view_columns_meta.length > 0) { + await ncMeta.metaUpdate( + null, + null, + MetaTable.COLUMNS, + { pv: true }, + view_columns_meta[0].id + ); + } + } + const primary_value_column_meta = view_columns_meta.find((col) => col.pv); if (primary_value_column_meta) { From a2bfae7e47ef795c0d8c37be758452b204ec6077 Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 20 Feb 2023 15:22:16 +0300 Subject: [PATCH 4/4] fix: update version of sticky column upgrader Signed-off-by: mertmit --- packages/nocodb/src/lib/Noco.ts | 2 +- packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/lib/Noco.ts b/packages/nocodb/src/lib/Noco.ts index 6c7ead8acd..b66287a446 100644 --- a/packages/nocodb/src/lib/Noco.ts +++ b/packages/nocodb/src/lib/Noco.ts @@ -105,7 +105,7 @@ export default class Noco { constructor() { process.env.PORT = process.env.PORT || '8080'; // todo: move - process.env.NC_VERSION = '0104004'; + process.env.NC_VERSION = '0105002'; // if env variable NC_MINIMAL_DBS is set, then disable project creation with external sources if (process.env.NC_MINIMAL_DBS) { diff --git a/packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts b/packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts index cd84032692..c125701a4c 100644 --- a/packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts +++ b/packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts @@ -43,8 +43,8 @@ export default class NcUpgrader { { name: '0100002', handler: ncFilterUpgrader }, { name: '0101002', handler: ncAttachmentUpgrader }, { name: '0104002', handler: ncAttachmentUpgrader_0104002 }, - { name: '0104003', handler: ncStickyColumnUpgrader }, { name: '0104004', handler: ncFilterUpgrader_0104004 }, + { name: '0105002', handler: ncStickyColumnUpgrader }, ]; if (!(await ctx.ncMeta.knexConnection?.schema?.hasTable?.('nc_store'))) { return;