Browse Source

refactor(nocodb): ncFilterUpgrader

pull/5185/head
Wing-Kam Wong 2 years ago
parent
commit
ea43a324ff
  1. 106
      packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader_0105003.ts

106
packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader_0105003.ts

@ -20,35 +20,19 @@ import { UITypes } from 'nocodb-sdk';
// - migrate `null` or `empty` filters to `blank` // - migrate `null` or `empty` filters to `blank`
// - add `exact date` in comparison_sub_op for existing filters `eq` and `neq` // - add `exact date` in comparison_sub_op for existing filters `eq` and `neq`
async function removeLikeAndNlikeFilters(ncMeta: NcMetaIO) { function removeLikeAndNlikeFilters(filter: Filter, ncMeta: NcMetaIO) {
let actions = []; let actions = [];
const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP); // remove `is like` and `is not like`
for (const filter of filters) { if (['like', 'nlike'].includes(filter.comparison_op)) {
if (!filter.fk_column_id || filter.is_group) { actions.push(Filter.delete(filter.id, ncMeta));
continue;
}
const col = await Column.get({ colId: filter.fk_column_id }, ncMeta);
if ([UITypes.Date, UITypes.DateTime].includes(col.uidt)) {
// remove `is like` and `is not like`
if (['like', 'nlike'].includes(filter.comparison_op)) {
actions.push(Filter.delete(filter.id, ncMeta));
}
}
} }
await Promise.all(actions); return actions;
} }
async function migrateEqAndNeqFilters(ncMeta: NcMetaIO) { function migrateEqAndNeqFilters(filter: Filter, ncMeta: NcMetaIO) {
let actions = []; let actions = [];
const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP); // remove `is like` and `is not like`
for (const filter of filters) { if (['eq', 'neq'].includes(filter.comparison_op)) {
if (
!filter.fk_column_id ||
filter.is_group ||
!['eq', 'neq'].includes(filter.comparison_op)
) {
continue;
}
actions.push( actions.push(
Filter.update( Filter.update(
filter.id, filter.id,
@ -59,49 +43,51 @@ async function migrateEqAndNeqFilters(ncMeta: NcMetaIO) {
) )
); );
} }
await Promise.all(actions); return actions;
} }
async function migrateEmptyAndNullFilters(ncMeta: NcMetaIO) { function migrateEmptyAndNullFilters(filter: Filter, ncMeta: NcMetaIO) {
let actions = []; let actions = [];
// remove `is like` and `is not like`
if (['empty', 'null'].includes(filter.comparison_op)) {
// migrate to blank
actions.push(
Filter.update(
filter.id,
{
comparison_op: 'blank',
},
ncMeta
)
);
} else if (['notempty', 'notnull'].includes(filter.comparison_op)) {
// migrate to not blank
actions.push(
Filter.update(
filter.id,
{
comparison_op: 'notblank',
},
ncMeta
)
);
}
return actions;
}
export default async function ({ ncMeta }: NcUpgraderCtx) {
const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP); const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP);
for (const filter of filters) { for (const filter of filters) {
if ( if (!filter.fk_column_id || filter.is_group) {
!filter.fk_column_id ||
filter.is_group ||
!['empty', 'notempty', 'null', 'notnull'].includes(filter.comparison_op)
) {
continue; continue;
} }
if (['empty', 'null'].includes(filter.comparison_op)) { const col = await Column.get({ colId: filter.fk_column_id }, ncMeta);
// migrate to blank if ([UITypes.Date, UITypes.DateTime].includes(col.uidt)) {
actions.push( await Promise.all([
Filter.update( ...removeLikeAndNlikeFilters(filter, ncMeta),
filter.id, ...migrateEmptyAndNullFilters(filter, ncMeta),
{ ...migrateEqAndNeqFilters(filter, ncMeta),
comparison_op: 'blank', ]);
},
ncMeta
)
);
} else if (['notempty', 'notnull'].includes(filter.comparison_op)) {
// migrate to not blank
actions.push(
Filter.update(
filter.id,
{
comparison_op: 'notblank',
},
ncMeta
)
);
} }
} }
await Promise.all(actions);
}
export default async function ({ ncMeta }: NcUpgraderCtx) {
await removeLikeAndNlikeFilters(ncMeta);
await migrateEmptyAndNullFilters(ncMeta);
await migrateEqAndNeqFilters(ncMeta);
} }

Loading…
Cancel
Save