mirror of https://github.com/nocodb/nocodb
mertmit
2 years ago
3 changed files with 75 additions and 1 deletions
@ -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 |
||||
); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue