Skip to content

Commit

Permalink
fix(api): Project hard sync existing entities deleted (#660)
Browse files Browse the repository at this point in the history
Co-authored-by: Rajdip Bhattacharya <agentR47@gmail.com>
  • Loading branch information
csehatt741 and rajdip-b authored Feb 4, 2025
1 parent cf34066 commit 3632217
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 35 additions & 1 deletion apps/api/src/project/service/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,11 @@ export class ProjectService {
// hardCopy = false: Only add those items in the toProject that are not already present in it
hardCopy: boolean = false
) {
// This field will be populated if hardCopy is true
// When we are doing a hard copy, we need to delete all the
// items in the toProject that are already present in it
const deleteOps = []

// Get all the environments that belongs to the parent project
// and replicate them for the new project
const createEnvironmentOps = []
Expand Down Expand Up @@ -964,6 +969,30 @@ export class ProjectService {
variables.forEach((variable) => {
toProjectVariables.add(variable.name)
})
} else {
deleteOps.push(
this.prisma.environment.deleteMany({
where: {
projectId: toProject.id
}
})
)

deleteOps.push(
this.prisma.secret.deleteMany({
where: {
projectId: toProject.id
}
})
)

deleteOps.push(
this.prisma.variable.deleteMany({
where: {
projectId: toProject.id
}
})
)
}

// We want to find all such environments in the fromProject that
Expand Down Expand Up @@ -1117,7 +1146,12 @@ export class ProjectService {
)
}

return [...createEnvironmentOps, ...createSecretOps, ...createVariableOps]
return [
...deleteOps,
...createEnvironmentOps,
...createSecretOps,
...createVariableOps
]
}

/**
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ These are the options supported by the `sync` command:

#### `-h, --hard-sync`

Upserts a new copy of the parent onto the child. Defaults to soft sync.
Force the child to match the parent by discarding all changes in the child. Defaults to soft sync.

## `unlink`

Expand Down

0 comments on commit 3632217

Please sign in to comment.