Skip to content

feat: Add support for renaming workspaces #3409

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

Merged
merged 16 commits into from
Aug 26, 2022
Merged

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Aug 8, 2022

WIP: This PR adds support for renaming workspaces.

For now, only the docker template has been updated, more to follow.

TODO

Fixes #3000

@mafredri mafredri self-assigned this Aug 8, 2022
@Emyrk
Copy link
Member

Emyrk commented Aug 8, 2022

Do you think we'll need to reuse the pg error checks? Should we have database pkg functions like:

if database.IsUniqueConstraint(err, "column_name") {
	// ...
}

Just spitballing ideas. I think you also need this check on the post workspace route too right?

@mafredri
Copy link
Member Author

mafredri commented Aug 9, 2022

@Emyrk I like that suggestion. What do you think about also pulling out the constraint names from dump.sql and creating constants for them in a generated file?

Edit: Example in 85eb0cf.

@Emyrk
Copy link
Member

Emyrk commented Aug 9, 2022

@Emyrk I like that suggestion. What do you think about also pulling out the constraint names from dump.sql and creating constants for them in a generated file?

Edit: Example in 85eb0cf.

I've been wondering if we should generate some Golang code like we do typescript code from our database schema. It might be a bit preemptive to go that far now, unless you think this error checking will be useful outside of just workspace and user names.


If we do generate though, I'd like to expand our enum generation in Go. Currently sqlc generates Enums to models.go. I'd like to expand that to copy the enums to codersdk somehow and add some functions for validating enums. Right now you can see we handle some enum validation manually

DestinationScheme ParameterDestinationScheme `json:"destination_scheme" validate:"oneof=environment_variable provisioner_variable,required"`
. But this is annoying to maintain in json tags, it would be great to handle this with some autogen "validate" function imo.

@mafredri
Copy link
Member Author

mafredri commented Aug 9, 2022

@Emyrk

I've been wondering if we should generate some Golang code like we do typescript code from our database schema. It might be a bit preemptive to go that far now, unless you think this error checking will be useful outside of just workspace and user names.

My thinking is that the value is in ensuring our code is in sync with the schema. For instance, the unique index we're relying on here could be recreated with a different name at some point, and in that case the code would silently no longer catch the case. By generating the enums we ensure that the project fails to build unless names are in sync.

Ultimately it'd be great if we strive for having all relevant parts generated into Go code (to ensure synchronization), but I don't think it needs to be an all or nothing approach. I.e. it'd be fine to generate a subset now, and more as the need arises.

It didn't look like sqlc supports generation of unique constraint enums, so I was thinking of doing some manual lifting in this case (based on dump.sql).

But this is annoying to maintain in json tags, it would be great to handle this with some autogen "validate" function imo.

(Not relevant for this PR, but) That looks annoying indeed. I think one of the libraries I mentioned in #3390 (comment) would support the custom Validate() error function, which sounds like what we'd want.

@mafredri mafredri force-pushed the mafredri/rename-workspaces branch from 952b5d3 to f8dcf51 Compare August 24, 2022 12:47
@mafredri mafredri marked this pull request as ready for review August 24, 2022 12:47
@mafredri mafredri requested a review from a team as a code owner August 24, 2022 12:47
@mafredri mafredri requested review from presleyp and removed request for a team August 24, 2022 12:47
@mafredri
Copy link
Member Author

While #3386 is not fixed yet, I opted to add a large disclaimer to the rename command:

❯ ./coder rename test byebye
> WARNING: A rename can result in loss of home volume if the template references the workspace name. Continue? (yes/no) no

I think this should be sufficient for us to merge this functionality.

@mafredri mafredri requested a review from a team August 24, 2022 12:49
Copy link
Contributor

@presleyp presleyp left a comment

Choose a reason for hiding this comment

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

Frontend ✅

mafredri added a commit that referenced this pull request Aug 24, 2022
continue
}
if other.Name == arg.Name {
return database.Workspace{}, &pq.Error{Code: "23505", Message: "duplicate key value violates unique constraint"}
Copy link
Member

Choose a reason for hiding this comment

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

TIL you can just return the correct SQL error codes 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a bit janky 😅.. but at least the codes will never change.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

I'm OK with this right now, bearing in mind that there is a really big open question about how to do this completely 100% safely. We should hide the command until this is 100% safe to do.

@mafredri
Copy link
Member Author

image

@mafredri mafredri merged commit c8f8c95 into main Aug 26, 2022
@mafredri mafredri deleted the mafredri/rename-workspaces branch August 26, 2022 09:28
mafredri added a commit that referenced this pull request Aug 26, 2022
mafredri added a commit that referenced this pull request Aug 29, 2022
* feat: Generate DB unique constraints as enums

This fixes a TODO from #3409.
mafredri added a commit that referenced this pull request Nov 8, 2022
mafredri added a commit that referenced this pull request Nov 9, 2022
* fix: Use immutable names for volumes in example templates

This contributes towards #3000, #3386

Related #3409

* Add lifecycle and labels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow workspace names to be editable
4 participants