Skip to content

fix: Allow template names to be re-used after deletion #2454

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 8 commits into from
Jun 17, 2022
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
48 changes: 48 additions & 0 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli_test

import (
"io"
"os"
"testing"

Expand Down Expand Up @@ -197,6 +198,53 @@ func TestTemplateCreate(t *testing.T) {
require.EqualError(t, <-execDone, "Parameter value absent in parameter file for \"region\"!")
removeTmpDirUntilSuccess(t, tempDir)
})

t.Run("Recreate template with same name (create, delete, create)", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
coderdtest.CreateFirstUser(t, client)

create := func() error {
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
Provision: provisionCompleteWithAgent,
})
args := []string{
"templates",
"create",
"my-template",
"--yes",
"--directory", source,
"--test.provisioner", string(database.ProvisionerTypeEcho),
}
cmd, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

return cmd.Execute()
}
del := func() error {
args := []string{
"templates",
"delete",
"my-template",
}
cmd, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

return cmd.Execute()
}

err := create()
require.NoError(t, err, "Template must be created without error")
err = del()
require.NoError(t, err, "Template must be deleted without error")
err = create()
require.NoError(t, err, "Template must be recreated without error")
})
}

func createTestParseResponse() []*proto.Parse_Response {
Expand Down
2 changes: 1 addition & 1 deletion cli/templatedelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func templateDelete() *cobra.Command {
return xerrors.Errorf("delete template %q: %w", template.Name, err)
}

_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Deleted template "+cliui.Styles.Code.Render(template.Name)+"!")
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Deleted template "+cliui.Styles.Code.Render(template.Name)+"!")
}

return nil
Expand Down
7 changes: 1 addition & 6 deletions coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP INDEX templates_organization_id_name_idx;
CREATE UNIQUE INDEX templates_organization_id_name_idx ON templates USING btree (organization_id, name) WHERE deleted = false;
CREATE UNIQUE INDEX idx_templates_name_lower ON templates USING btree (lower(name));

ALTER TABLE ONLY templates ADD CONSTRAINT templates_organization_id_name_key UNIQUE (organization_id, name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP INDEX idx_templates_name_lower;
DROP INDEX templates_organization_id_name_idx;
CREATE UNIQUE INDEX templates_organization_id_name_idx ON templates (organization_id, lower(name)) WHERE deleted = false;

ALTER TABLE ONLY templates DROP CONSTRAINT templates_organization_id_name_key;