Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): Project hard sync existing entities deleted #660

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that this wont work. This is because, we are not actually deleting the variables, secrets or environments. So the subsequent block wont work.

We need to refactor the missingEnvironments section aswell to make sure that we don't do that comparison when we are doing a hard sync. Hope it makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I`ve tested it and it does delete all existing items from forked project then copies everything from the parent.

To my understanding this is what hard sync is about.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes your understanding is correct. I was talking about the next block where in we are checking for existing environments. In case we are hard syncing a project, we would be deleting the environments. So there shouldn't be any environments left to check for. So, it would be the best to use that block optionally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess i pinned this in the wrong place. It's line 974 that im talking about.

...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
Loading