From 554ef9c50d1acb1769da7d41df84b9cd948a8f2a Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Tue, 10 Jan 2023 20:21:48 +0800 Subject: [PATCH] feat(nocodb): ncFilterUpgrader --- packages/nocodb/src/lib/Noco.ts | 2 +- .../src/lib/version-upgrader/NcUpgrader.ts | 2 ++ .../lib/version-upgrader/ncFilterUpgrader.ts | 34 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader.ts diff --git a/packages/nocodb/src/lib/Noco.ts b/packages/nocodb/src/lib/Noco.ts index cbcfbee986..8a3397053d 100644 --- a/packages/nocodb/src/lib/Noco.ts +++ b/packages/nocodb/src/lib/Noco.ts @@ -104,7 +104,7 @@ export default class Noco { constructor() { process.env.PORT = process.env.PORT || '8080'; // todo: move - process.env.NC_VERSION = '0098005'; + process.env.NC_VERSION = '0100002'; // 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 a51d04910b..9f7ae6d07f 100644 --- a/packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts +++ b/packages/nocodb/src/lib/version-upgrader/NcUpgrader.ts @@ -8,6 +8,7 @@ import ncProjectEnvUpgrader0011045 from './ncProjectEnvUpgrader0011045'; import ncProjectUpgraderV2_0090000 from './ncProjectUpgraderV2_0090000'; import ncDataTypesUpgrader from './ncDataTypesUpgrader'; import ncProjectRolesUpgrader from './ncProjectRolesUpgrader'; +import ncFilterUpgrader from './ncFilterUpgrader'; const log = debug('nc:version-upgrader'); import boxen from 'boxen'; @@ -35,6 +36,7 @@ export default class NcUpgrader { { name: '0090000', handler: ncProjectUpgraderV2_0090000 }, { name: '0098004', handler: ncDataTypesUpgrader }, { name: '0098005', handler: ncProjectRolesUpgrader }, + { name: '0100002', handler: ncFilterUpgrader }, ]; if (!(await ctx.ncMeta.knexConnection?.schema?.hasTable?.('nc_store'))) { return; diff --git a/packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader.ts b/packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader.ts new file mode 100644 index 0000000000..09eb667432 --- /dev/null +++ b/packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader.ts @@ -0,0 +1,34 @@ +import { NcUpgraderCtx } from './NcUpgrader'; +import { MetaTable } from '../utils/globals'; +import View from '../models/View'; +import Hook from '../models/Hook'; +import Column from '../models/Column'; + +// before 0.101.0, an incorrect project_id was inserted when +// a filter is created without specifying the column +// this upgrader is to retrieve the correct project id from either view, hook, or column +// and update the project id +export default async function ({ ncMeta }: NcUpgraderCtx) { + const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP); + for (const filter of filters) { + let model: { project_id?: string; base_id?: string }; + if (filter.fk_view_id) { + model = await View.get(filter.fk_view_id, ncMeta); + } else if (filter.fk_hook_id) { + model = await Hook.get(filter.fk_hook_id, ncMeta); + } else if (filter.fk_column_id) { + model = await Column.get({ colId: filter.fk_column_id }, ncMeta); + } else { + continue; + } + if (filter.project_id != model.project_id) { + await ncMeta.metaUpdate( + null, + null, + MetaTable.FILTER_EXP, + { project_id: model.project_id }, + filter.id + ); + } + } +}