diff --git a/apps/app/src/server/service/normalize-data/convert-null-to-empty-granted-arrays.ts b/apps/app/src/server/service/normalize-data/convert-null-to-empty-granted-arrays.ts new file mode 100644 index 00000000000..bcd6255b353 --- /dev/null +++ b/apps/app/src/server/service/normalize-data/convert-null-to-empty-granted-arrays.ts @@ -0,0 +1,31 @@ +import type { IPageHasId } from '@growi/core'; +import mongoose from 'mongoose'; + +import { type PageModel } from '~/server/models/page'; + +export const convertNullToEmptyGrantedArrays = async(): Promise => { + const Page = mongoose.model('Page'); + + const requests = [ + { + updateMany: { + // Matches documents where field is null or nonexistent + // https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields/#equality-filter + filter: { grantedUsers: null }, + update: { + $set: { grantedUsers: [] }, + }, + }, + }, + { + updateMany: { + filter: { grantedGroups: null }, + update: { + $set: { grantedGroups: [] }, + }, + }, + }, + ]; + + await Page.bulkWrite(requests); +}; diff --git a/apps/app/src/server/service/normalize-data/index.ts b/apps/app/src/server/service/normalize-data/index.ts index f4647773625..b7901e77356 100644 --- a/apps/app/src/server/service/normalize-data/index.ts +++ b/apps/app/src/server/service/normalize-data/index.ts @@ -1,6 +1,7 @@ import { normalizeExpiredAtForThreadRelations } from '~/features/openai/server/services/normalize-data'; import loggerFactory from '~/utils/logger'; +import { convertNullToEmptyGrantedArrays } from './convert-null-to-empty-granted-arrays'; import { convertRevisionPageIdToObjectId } from './convert-revision-page-id-to-objectid'; import { renameDuplicateRootPages } from './rename-duplicate-root-pages'; @@ -10,6 +11,7 @@ export const normalizeData = async(): Promise => { await renameDuplicateRootPages(); await convertRevisionPageIdToObjectId(); await normalizeExpiredAtForThreadRelations(); + await convertNullToEmptyGrantedArrays(); logger.info('normalizeData has been executed'); return;