Skip to content

Commit

Permalink
feat: Added migration script for deleting mongodb environment records…
Browse files Browse the repository at this point in the history
… for projects already deleted
  • Loading branch information
Mayur committed Nov 25, 2024
1 parent f674568 commit f490280
Showing 1 changed file with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Migration script for deleting environments associated with deleted projects.
*
* This migration is designed to clean up environment documents that are still
* associated with projects that have already been deleted. In older versions
* of the system, if a project was deleted, the corresponding environment
* documents were not automatically deleted. This issue has been fixed in
* newer versions, but for older users, we need to run this migration to
* ensure that any environments associated with deleted projects are properly removed.
*/

import '../../config';
import { AppModule } from '../../app.module';

import { NestFactory } from '@nestjs/core';
import { EnvironmentRepository } from '@impler/dal';
export async function run() {
const app = await NestFactory.create(AppModule, {
logger: false,
});

const environmentRepository = new EnvironmentRepository();
const deletedProjectsInEnvironments = await environmentRepository.aggregate([
{
$lookup: {
from: 'project',
localField: '_projectId',
foreignField: '_id',
as: 'project',
},
},
{
$match: {
project: {
$size: 0,
},
},
},
{
$unwind: {
path: '$apiKeys',
preserveNullAndEmptyArrays: false,
},
},
{
$lookup: {
from: 'users',
localField: 'apiKeys._userId',
foreignField: '_id',
as: 'userInfo',
},
},
{
$unwind: {
path: '$userInfo',
preserveNullAndEmptyArrays: true,
},
},
{
$project: {
_id: 1,
_projectId: 1,
apiKeys: {
_userId: '$apiKeys._userId',
// key: "$apiKeys.key",
user: {
firstName: '$userInfo.firstName',
lastName: '$userInfo.lastName',
email: '$userInfo.email',
},
},
},
},
{
$group: {
_id: '$_id',
_projectId: {
$first: '$_projectId',
},
apiKeys: {
$push: '$apiKeys',
},
},
},
]);

const deletedProjectIdInEnvironment = deletedProjectsInEnvironments.map((deletedProjects) => deletedProjects._id);

if (deletedProjectIdInEnvironment.length > 0) {
await environmentRepository.deleteMany({
_id: { $in: deletedProjectIdInEnvironment },
});
console.log(`end migration - deleted ${deletedProjectIdInEnvironment.length} environments.`);
} else {
console.log('end migration - No environments found to delete.');
}

app.close();
process.exit(0);
}
run();

0 comments on commit f490280

Please sign in to comment.