Skip to content
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
142 changes: 142 additions & 0 deletions apps/workspace-engine/pkg/db/deployment_variables.sql.go

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

8 changes: 8 additions & 0 deletions apps/workspace-engine/pkg/db/models.go

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

26 changes: 26 additions & 0 deletions apps/workspace-engine/pkg/db/queries/deployment_variables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- name: GetDeploymentVariableByID :one
SELECT id, deployment_id, key, description, default_value
FROM deployment_variable
WHERE id = $1;

-- name: ListDeploymentVariablesByDeploymentID :many
SELECT id, deployment_id, key, description, default_value
FROM deployment_variable
WHERE deployment_id = $1;

-- name: UpsertDeploymentVariable :one
INSERT INTO deployment_variable (id, deployment_id, key, description, default_value)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (id) DO UPDATE
SET deployment_id = EXCLUDED.deployment_id, key = EXCLUDED.key,
description = EXCLUDED.description, default_value = EXCLUDED.default_value
RETURNING *;

-- name: DeleteDeploymentVariable :exec
DELETE FROM deployment_variable WHERE id = $1;

-- name: ListDeploymentVariablesByWorkspaceID :many
SELECT dv.id, dv.deployment_id, dv.key, dv.description, dv.default_value
FROM deployment_variable dv
INNER JOIN deployment d ON d.id = dv.deployment_id
WHERE d.workspace_id = $1;
8 changes: 8 additions & 0 deletions apps/workspace-engine/pkg/db/queries/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ CREATE TABLE user_approval_record (
PRIMARY KEY (version_id, user_id, environment_id)
);

CREATE TABLE deployment_variable (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
deployment_id UUID NOT NULL REFERENCES deployment(id) ON DELETE CASCADE,
key TEXT NOT NULL,
description TEXT,
default_value JSONB
);

CREATE TABLE resource_variable (
resource_id UUID NOT NULL REFERENCES resource(id) ON DELETE CASCADE,
key TEXT NOT NULL,
Expand Down
6 changes: 6 additions & 0 deletions apps/workspace-engine/pkg/db/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sql:
- queries/policies.sql
- queries/user_approval_records.sql
- queries/resource_variables.sql
- queries/deployment_variables.sql
database:
uri: "postgresql://ctrlplane:ctrlplane@127.0.0.1:5432/ctrlplane?sslmode=disable"
gen:
Expand Down Expand Up @@ -104,3 +105,8 @@ sql:
- column: "resource_variable.value"
go_type:
type: "[]byte"

# DeploymentVariable
- column: "deployment_variable.default_value"
go_type:
type: "[]byte"
2 changes: 1 addition & 1 deletion apps/workspace-engine/pkg/persistence/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func TestPersistence_AllEntityTypes(t *testing.T) {
_, ok = testStore.Repo().DeploymentVersions().Get(deploymentVersion.Id)
assert.True(t, ok, "DeploymentVersion should be restored")

_, ok = testStore.Repo().DeploymentVariables.Get(deploymentVariable.Id)
_, ok = testStore.Repo().DeploymentVariables().Get(deploymentVariable.Id)
assert.True(t, ok, "DeploymentVariable should be restored")

_, ok = testStore.Environments.Get(environment.Id)
Expand Down
30 changes: 21 additions & 9 deletions apps/workspace-engine/pkg/workspace/store/deployment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,58 @@ package store
import (
"context"
"workspace-engine/pkg/oapi"
"workspace-engine/pkg/workspace/store/repository/memory"
"workspace-engine/pkg/workspace/store/repository"

"github.com/charmbracelet/log"
)

func NewDeploymentVariables(store *Store) *DeploymentVariables {
return &DeploymentVariables{
repo: store.repo,
repo: store.repo.DeploymentVariables(),
store: store,
}
}

type DeploymentVariables struct {
repo *memory.InMemory
repo repository.DeploymentVariableRepo
store *Store
}

func (d *DeploymentVariables) SetRepo(repo repository.DeploymentVariableRepo) {
d.repo = repo
}

func (d *DeploymentVariables) Items() map[string]*oapi.DeploymentVariable {
return d.repo.DeploymentVariables.Items()
return d.repo.Items()
}

func (d *DeploymentVariables) Get(id string) (*oapi.DeploymentVariable, bool) {
return d.repo.DeploymentVariables.Get(id)
return d.repo.Get(id)
}

func (d *DeploymentVariables) Upsert(ctx context.Context, id string, deploymentVariable *oapi.DeploymentVariable) {
d.repo.DeploymentVariables.Set(id, deploymentVariable)
if err := d.repo.Set(deploymentVariable); err != nil {
log.Error("Failed to upsert deployment variable", "error", err)
return
}
d.store.changeset.RecordUpsert(deploymentVariable)
}

func (d *DeploymentVariables) Remove(ctx context.Context, id string) {
deploymentVariable, ok := d.repo.DeploymentVariables.Get(id)
deploymentVariable, ok := d.repo.Get(id)
if !ok || deploymentVariable == nil {
return
}
d.repo.DeploymentVariables.Remove(id)
if err := d.repo.Remove(id); err != nil {
log.Error("Failed to remove deployment variable", "error", err)
return
}
d.store.changeset.RecordDelete(deploymentVariable)
}

func (d *DeploymentVariables) Values(variableId string) map[string]*oapi.DeploymentVariableValue {
values := make(map[string]*oapi.DeploymentVariableValue)
for _, value := range d.repo.DeploymentVariableValues.Items() {
for _, value := range d.store.DeploymentVariableValues.Items() {
if value.DeploymentVariableId == variableId {
values[value.Id] = value
}
Expand Down
12 changes: 7 additions & 5 deletions apps/workspace-engine/pkg/workspace/store/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ func (e *Deployments) Remove(ctx context.Context, id string) {
}

func (e *Deployments) Variables(deploymentId string) map[string]*oapi.DeploymentVariable {
vars := make(map[string]*oapi.DeploymentVariable)
for _, variable := range e.store.repo.DeploymentVariables.Items() {
if variable.DeploymentId == deploymentId {
vars[variable.Key] = variable
}
dvs, err := e.store.DeploymentVariables.repo.GetByDeploymentID(deploymentId)
if err != nil {
return make(map[string]*oapi.DeploymentVariable)
}
vars := make(map[string]*oapi.DeploymentVariable, len(dvs))
for _, dv := range dvs {
vars[dv.Key] = dv
}
return vars
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package deploymentvariables

import (
"encoding/json"
"fmt"
"workspace-engine/pkg/db"
"workspace-engine/pkg/oapi"

"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)

func VariableToOapi(row db.DeploymentVariable) *oapi.DeploymentVariable {
var description *string
if row.Description.Valid {
description = &row.Description.String
}

var defaultValue *oapi.LiteralValue
if row.DefaultValue != nil {
lv := &oapi.LiteralValue{}
if err := json.Unmarshal(row.DefaultValue, lv); err == nil {
defaultValue = lv
}
}

return &oapi.DeploymentVariable{
Id: row.ID.String(),
DeploymentId: row.DeploymentID.String(),
Key: row.Key,
Description: description,
DefaultValue: defaultValue,
}
}

func ToVariableUpsertParams(e *oapi.DeploymentVariable) (db.UpsertDeploymentVariableParams, error) {
id, err := uuid.Parse(e.Id)
if err != nil {
return db.UpsertDeploymentVariableParams{}, fmt.Errorf("parse id: %w", err)
}

did, err := uuid.Parse(e.DeploymentId)
if err != nil {
return db.UpsertDeploymentVariableParams{}, fmt.Errorf("parse deployment_id: %w", err)
}

var description pgtype.Text
if e.Description != nil {
description = pgtype.Text{String: *e.Description, Valid: true}
}

var defaultValue []byte
if e.DefaultValue != nil {
defaultValue, err = json.Marshal(e.DefaultValue)
if err != nil {
return db.UpsertDeploymentVariableParams{}, fmt.Errorf("marshal default_value: %w", err)
}
}

return db.UpsertDeploymentVariableParams{
ID: id,
DeploymentID: did,
Key: e.Key,
Description: description,
DefaultValue: defaultValue,
}, nil
}
Loading
Loading