Browse Source

refactor(nocodb): filter upgrader

pull/5106/head
Wing-Kam Wong 2 years ago
parent
commit
21d769e896
  1. 202
      packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader_0104003.ts

202
packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader_0104003.ts

@ -9,91 +9,97 @@ import { UITypes } from 'nocodb-sdk';
// this upgrader is to remove those unsupported filters / migrate to the correct filter // this upgrader is to remove those unsupported filters / migrate to the correct filter
// changes: // changes:
// - remove `>`, `<`, `>=`, `<=` for text-based columns // - remove `>`, `<`, `>=`, `<=` for text-based columns
// - remove `like`, `null`, and `empty` for numeric-based / singleSelect columns - migrate to `blank` from `null` and `empty` // - remove `like`; migrate `null`, and `empty` for numeric-based / singleSelect columns to`blank`
// - remove `is null`, `is not null` for checkbox columns - migrate `equal` and `not equal` to `checked` and `not checked` // - remove `equal`; migrate `null` to `checked` for checkbox columns
// - remove `like`, `null`, `equal` and `empty` for multiSelect columns // - remove `like`; migrate `equal`, `null`, `empty` for multiSelect columns
// - remove `>`, `<`, `>=`, `<=`, `empty`, `equal` for attachment / LTAR columns // - remove `>`, `<`, `>=`, `<=`; migrate `empty`, `equal`, `null` for attachment
// - remove `empty`, `like`, `equal`, `null` for duration columns - migrate to blank if necessary // - remove `>`, `<`, `>=`, `<=`; migrate `empty`, `null` for LTAR columns
// - migrate `empty`, `null` for Lookup columns
// - remove `>`, `<`, `>=`, `<=`, `like`, `equal`; migrate `empty`, `null`
// - remove `empty`, `like`, `equal`, `null` for duration columns
const removeEqualFilters = async ( const removeEqualFilters = async (filter, actions: any[], ncMeta) => {
filter,
actions: any[],
ncMeta,
migrateFn = () => {}
) => {
// remove `is equal`, `is not equal` // remove `is equal`, `is not equal`
if (['eq', 'neq'].includes(filter.comparison_op)) { if (['eq', 'neq'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta)); actions.push(await Filter.delete(filter, ncMeta));
if (migrateFn) migrateFn();
} }
return actions; return actions;
}; };
const removeArithmeticFilters = async ( const removeArithmeticFilters = async (filter, actions: any[], ncMeta) => {
filter,
actions: any[],
ncMeta,
migrateFn = () => {}
) => {
// remove `>`, `<`, `>=`, `<=` // remove `>`, `<`, `>=`, `<=`
if (['gt', 'lt', 'gte', 'lte'].includes(filter.comparison_op)) { if (['gt', 'lt', 'gte', 'lte'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta)); actions.push(await Filter.delete(filter, ncMeta));
if (migrateFn) migrateFn();
} }
return actions; return actions;
}; };
const removeLikeFilters = async ( const removeLikeFilters = async (filter, actions: any[], ncMeta) => {
filter,
actions: any[],
ncMeta,
migrateFn = () => {}
) => {
// remove `is like`, `is not like` // remove `is like`, `is not like`
if (['like', 'nlike'].includes(filter.comparison_op)) { if (['like', 'nlike'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta)); actions.push(await Filter.delete(filter, ncMeta));
if (migrateFn) migrateFn();
} }
return actions; return actions;
}; };
const removeNullFilters = async ( const migrateToBlankFilter = async (filter, actions: any[], ncMeta) => {
filter, if (['empty', 'null'].includes(filter.comparision_op)) {
actions: any[], // migrate to blank
ncMeta, actions.push(
migrateFn = () => {} await Filter.update(
) => { filter.id,
// remove `is null`, `is not null` {
if (['null', 'notnull'].includes(filter.comparison_op)) { ...filter,
actions.push(await Filter.delete(filter, ncMeta)); comparision_op: 'blank',
if (migrateFn) migrateFn(); },
ncMeta
)
);
} else if (['notempty', 'notnull'].includes(filter.comparision_op)) {
// migrate to not blank
actions.push(
await Filter.update(
filter.id,
{
...filter,
comparision_op: 'notblank',
},
ncMeta
)
);
} }
return actions; return actions;
}; };
const removeEmptyFilters = async ( const migrateToCheckboxFilter = async (filter, actions: any[], ncMeta) => {
filter, if (['empty', 'null'].includes(filter.comparision_op)) {
actions: any[], // migrate to checked
ncMeta, actions.push(
migrateFn = () => {} await Filter.update(
) => { filter.id,
// remove `is empty`, `is not empty` {
if (['empty', 'notempty'].includes(filter.comparison_op)) ...filter,
if (migrateFn) migrateFn(); comparision_op: 'checked',
{ },
actions.push(await Filter.delete(filter, ncMeta)); ncMeta
)
);
} else if (['notempty', 'notnull'].includes(filter.comparision_op)) {
// migrate to not checked
actions.push(
await Filter.update(
filter.id,
{
...filter,
comparision_op: 'notchecked',
},
ncMeta
)
);
} }
return actions; return actions;
}; };
const migrateToBlankFilter = async () => {
// TODO:
};
const migrateToCheckboxFilter = async () => {
// TODO
};
export default async function ({ ncMeta }: NcUpgraderCtx) { 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);
let actions = []; let actions = [];
@ -128,94 +134,30 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
].includes(col.uidt) ].includes(col.uidt)
) { ) {
actions = await removeLikeFilters(filter, actions, ncMeta); actions = await removeLikeFilters(filter, actions, ncMeta);
actions = await removeNullFilters( actions = await migrateToBlankFilter(filter, actions, ncMeta);
filter,
actions,
ncMeta,
migrateToBlankFilter
);
actions = await removeEmptyFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
} else if (col.uidt === UITypes.Checkbox) { } else if (col.uidt === UITypes.Checkbox) {
actions = await removeEqualFilters( actions = await removeEqualFilters(filter, actions, ncMeta);
filter, actions = await migrateToCheckboxFilter(filter, actions, ncMeta);
actions,
ncMeta,
migrateToCheckboxFilter
);
actions = await removeNullFilters(filter, actions, ncMeta);
} else if (col.uidt === UITypes.MultiSelect) { } else if (col.uidt === UITypes.MultiSelect) {
// TODO: migrate to "contains any of" or "contains all of" // TODO: migrate to "contains any of" or "contains all of"
// TODO: migrate to "doesnt contain any of" or "doesnt contain all of" // TODO: migrate to "doesnt contain any of" or "doesnt contain all of"
actions = await removeEqualFilters(filter, actions, ncMeta); // actions = await removeEqualFilters(filter, actions, ncMeta);
actions = await removeLikeFilters(filter, actions, ncMeta); actions = await removeLikeFilters(filter, actions, ncMeta);
actions = await removeNullFilters( actions = await migrateToBlankFilter(filter, actions, ncMeta);
filter,
actions,
ncMeta,
migrateToBlankFilter
);
actions = await removeEmptyFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
} else if (col.uidt === UITypes.Attachment) { } else if (col.uidt === UITypes.Attachment) {
actions = await removeArithmeticFilters(filter, actions, ncMeta); actions = await removeArithmeticFilters(filter, actions, ncMeta);
actions = await removeEmptyFilters(filter, actions, ncMeta); actions = await removeEqualFilters(filter, actions, ncMeta);
actions = await removeEqualFilters( actions = await migrateToBlankFilter(filter, actions, ncMeta);
filter,
actions,
ncMeta,
migrateToCheckboxFilter
);
} else if (col.uidt === UITypes.LinkToAnotherRecord) { } else if (col.uidt === UITypes.LinkToAnotherRecord) {
actions = await removeArithmeticFilters(filter, actions, ncMeta); actions = await removeArithmeticFilters(filter, actions, ncMeta);
actions = await removeEmptyFilters( actions = await migrateToBlankFilter(filter, actions, ncMeta);
filter,
actions,
ncMeta,
migrateToBlankFilter
);
actions = await removeNullFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
} else if (col.uidt === UITypes.Lookup) { } else if (col.uidt === UITypes.Lookup) {
actions = await removeArithmeticFilters(filter, actions, ncMeta); actions = await removeArithmeticFilters(filter, actions, ncMeta);
actions = await removeEmptyFilters( actions = await migrateToBlankFilter(filter, actions, ncMeta);
filter,
actions,
ncMeta,
migrateToBlankFilter
);
actions = await removeNullFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
} else if (col.uidt === UITypes.Duration) { } else if (col.uidt === UITypes.Duration) {
actions = await removeLikeFilters(filter, actions, ncMeta); actions = await removeLikeFilters(filter, actions, ncMeta);
actions = await removeEqualFilters( actions = await removeEqualFilters(filter, actions, ncMeta);
filter, actions = await migrateToBlankFilter(filter, actions, ncMeta);
actions,
ncMeta,
migrateToBlankFilter
);
actions = await removeNullFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
} }
} }
await Promise.all(actions); await Promise.all(actions);

Loading…
Cancel
Save