diff --git a/packages/nocodb/src/lib/Noco.ts b/packages/nocodb/src/lib/Noco.ts index bd2fed4b61..d144579e41 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 = '0104002'; + process.env.NC_VERSION = '0104003'; // 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 06890a86c3..1fea08ebed 100644 --- a/packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts +++ b/packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts @@ -11,6 +11,7 @@ import ncProjectRolesUpgrader from './ncProjectRolesUpgrader'; import ncFilterUpgrader from './ncFilterUpgrader'; import ncAttachmentUpgrader from './ncAttachmentUpgrader'; import ncAttachmentUpgrader_0104002 from './ncAttachmentUpgrader_0104002'; +import ncStickyColumnUpgrader from './ncStickyColumnUpgrader'; const log = debug('nc:version-upgrader'); import boxen from 'boxen'; @@ -41,6 +42,7 @@ export default class NcUpgrader { { name: '0100002', handler: ncFilterUpgrader }, { name: '0101002', handler: ncAttachmentUpgrader }, { name: '0104002', handler: ncAttachmentUpgrader_0104002 }, + { name: '0104003', handler: ncStickyColumnUpgrader }, ]; if (!(await ctx.ncMeta.knexConnection?.schema?.hasTable?.('nc_store'))) { return; diff --git a/packages/nocodb/src/lib/version-upgrader/ncStickyColumnUpgrader.ts b/packages/nocodb/src/lib/version-upgrader/ncStickyColumnUpgrader.ts new file mode 100644 index 0000000000..7168c27fe9 --- /dev/null +++ b/packages/nocodb/src/lib/version-upgrader/ncStickyColumnUpgrader.ts @@ -0,0 +1,72 @@ +import { NcUpgraderCtx } from './NcUpgrader'; +import { MetaTable } from '../utils/globals'; + +// before 0.104.3, primary value column can be in any position in table +// with this upgrade we introduced sticky primary column feature +// this upgrader will make primary value column first column in grid views + +export default async function ({ ncMeta }: NcUpgraderCtx) { + const grid_columns = await ncMeta.metaList2(null, null, MetaTable.GRID_VIEW_COLUMNS); + const grid_views = [...new Set(grid_columns.map((col) => col.fk_view_id))] + + for (const view_id of grid_views) { + // get a list of view columns sorted by order + const view_columns = await ncMeta.metaList2(null, null, MetaTable.GRID_VIEW_COLUMNS, { + condition: { + fk_view_id: view_id, + }, + orderBy: { + order: 'asc', + }, + }); + const view_columns_meta = [] + + // get column meta for each view column + for (const col of view_columns) { + const col_meta = await ncMeta.metaGet(null, null, MetaTable.COLUMNS, { id: col.fk_column_id }); + view_columns_meta.push(col_meta); + } + + const primary_value_column_meta = view_columns_meta.find((col) => col.pv); + + if (primary_value_column_meta) { + const primary_value_column = view_columns.find((col) => col.fk_column_id === primary_value_column_meta.id); + const primary_value_column_index = view_columns.findIndex((col) => col.fk_column_id === primary_value_column_meta.id); + const view_orders = view_columns.map((col) => col.order); + const view_min_order = Math.min(...view_orders); + + // if primary_value_column is not visible, make it visible + if (!primary_value_column.show) { + await ncMeta.metaUpdate( + null, + null, + MetaTable.GRID_VIEW_COLUMNS, + { show: true }, + primary_value_column.id, + ); + } + + if (primary_value_column.order === view_min_order && view_orders.filter((o) => o === view_min_order).length === 1) { + // if primary_value_column is in first order do nothing + continue; + } else { + // if primary_value_column not in first order, move it to the start of array + if (primary_value_column_index !== 0) { + const temp_pv = view_columns.splice(primary_value_column_index, 1); + view_columns.unshift(...temp_pv); + } + + // update order of all columns in view to match the order in array + for (let i = 0; i < view_columns.length; i++) { + await ncMeta.metaUpdate( + null, + null, + MetaTable.GRID_VIEW_COLUMNS, + { order: i + 1 }, + view_columns[i].id + ); + } + } + } + } +}