-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added migration script for deleting mongodb environment records…
… 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.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
...grations/delete-environment-for-deleted-project/delete-environment-for-deleted-project.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |