diff --git a/db/20200425172611_workspace_templates.sql b/db/20200425172611_workspace_templates.sql index ac035ae..9ac4823 100644 --- a/db/20200425172611_workspace_templates.sql +++ b/db/20200425172611_workspace_templates.sql @@ -1,15 +1,17 @@ -- +goose Up CREATE TABLE workspace_templates ( - id serial PRIMARY KEY, - uid varchar(36) UNIQUE NOT NULL CHECK(uid <> ''), - name text NOT NULL CHECK(name <> ''), - namespace varchar(36) NOT NULL, - is_archived boolean DEFAULT false, + id serial PRIMARY KEY, + uid varchar(36) UNIQUE NOT NULL CHECK(uid <> ''), + name text NOT NULL CHECK(name <> ''), + namespace varchar(36) NOT NULL, + is_archived boolean DEFAULT false, + + workflow_template_id integer NOT NULL REFERENCES workflow_templates ON DELETE CASCADE, -- auditing info - created_at timestamp NOT NULL DEFAULT (NOW() at time zone 'utc'), - modified_at timestamp + created_at timestamp NOT NULL DEFAULT (NOW() at time zone 'utc'), + modified_at timestamp ); CREATE UNIQUE INDEX workspace_templates_name_namespace_key ON workspace_templates (name, namespace) WHERE is_archived = false; diff --git a/db/20200425173049_workspace_template_versions.sql b/db/20200425173049_workspace_template_versions.sql index d99ab43..8310ed9 100644 --- a/db/20200425173049_workspace_template_versions.sql +++ b/db/20200425173049_workspace_template_versions.sql @@ -1,15 +1,15 @@ -- +goose Up CREATE TABLE workspace_template_versions ( - id serial PRIMARY KEY, - workspace_template_id integer NOT NULL REFERENCES workspace_templates ON DELETE CASCADE, - version integer NOT NULL, - manifest text NOT NULL, - is_latest boolean DEFAULT false, + id serial PRIMARY KEY, + workspace_template_id integer NOT NULL REFERENCES workspace_templates ON DELETE CASCADE, + version integer NOT NULL, + manifest text NOT NULL, + is_latest boolean DEFAULT false, -- auditing info - created_at timestamp NOT NULL DEFAULT (NOW() at time zone 'utc'), - modified_at timestamp + created_at timestamp NOT NULL DEFAULT (NOW() at time zone 'utc'), + modified_at timestamp ); -- +goose Down diff --git a/pkg/workflow_template.go b/pkg/workflow_template.go index 126a54b..d347efa 100644 --- a/pkg/workflow_template.go +++ b/pkg/workflow_template.go @@ -73,6 +73,8 @@ func (c *Client) createWorkflowTemplate(namespace string, workflowTemplate *Work return nil, err } + workflowTemplate.Version = versionUnix + return workflowTemplate, nil } diff --git a/pkg/workspace_template.go b/pkg/workspace_template.go index 6d8a853..84559f7 100644 --- a/pkg/workspace_template.go +++ b/pkg/workspace_template.go @@ -339,36 +339,39 @@ func (c *Client) createWorkspaceTemplate(namespace string, workspaceTemplate *Wo return nil, err } + workspaceTemplate.Version = workspaceTemplate.WorkflowTemplate.Version + err = sb.Insert("workspace_templates"). SetMap(sq.Eq{ - "uid": uid, - "name": workspaceTemplate.Name, - "namespace": namespace, + "uid": uid, + "name": workspaceTemplate.Name, + "namespace": namespace, + "workflow_template_id": workspaceTemplate.WorkflowTemplate.ID, }). Suffix("RETURNING id"). RunWith(tx). QueryRow().Scan(&workspaceTemplate.ID) if err != nil { + _, err := c.archiveWorkflowTemplate(namespace, workspaceTemplate.WorkflowTemplate.UID) return nil, err } _, err = sb.Insert("workspace_template_versions"). SetMap(sq.Eq{ - "workspace_template_id": workspaceTemplate.ID, - "version": workspaceTemplate.WorkflowTemplate.Version, + "version": workspaceTemplate.Version, "is_latest": true, "manifest": workspaceTemplate.Manifest, + "workspace_template_id": workspaceTemplate.ID, }). RunWith(tx). Exec() if err != nil { + _, err := c.archiveWorkflowTemplate(namespace, workspaceTemplate.WorkflowTemplate.UID) return nil, err } if err = tx.Commit(); err != nil { - //if err := c.ArgoprojV1alpha1().WorkflowTemplates(namespace).Delete(workspaceTemplate.WorkflowTemplate.Name, &metav1.DeleteOptions{}); err != nil { - // log.Printf("Unable to delete argo workflow template") - //} + _, err := c.archiveWorkflowTemplate(namespace, workspaceTemplate.WorkflowTemplate.UID) return nil, err }