Skip to content

chore: write release targets for db changeset compatibility#688

Merged
adityachoudhari26 merged 2 commits intomainfrom
write-release-targets-for-db-compatibility
Oct 17, 2025
Merged

chore: write release targets for db changeset compatibility#688
adityachoudhari26 merged 2 commits intomainfrom
write-release-targets-for-db-compatibility

Conversation

@adityachoudhari26
Copy link
Copy Markdown
Member

@adityachoudhari26 adityachoudhari26 commented Oct 17, 2025

Summary by CodeRabbit

  • New Features

    • Release targets can now be created and deleted via the changeset flow, supporting resource–environment–deployment associations with idempotent write behavior.
  • Tests

    • Added comprehensive tests for create/delete, repeated operations, idempotency, multi-target scenarios, and deletion of non-existent targets.
  • Chores

    • CI workflow permissions updated for test jobs.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 17, 2025

Walkthrough

Adds handling for ReleaseTarget entities to the changeset application flow, introduces transactional DB helpers to write/delete release_target rows (INSERT ... ON CONFLICT DO NOTHING / DELETE), provides tests covering CRUD and idempotency, and updates workflow job permissions for workspace-engine tests.

Changes

Cohort / File(s) Summary
Changeset Integration
apps/workspace-engine/pkg/db/changeset.go
Adds branch to handle oapi.ReleaseTarget entities: calls deleteReleaseTarget(ctx, e, conn) for deletes, otherwise writeReleaseTarget(ctx, e, conn); placed before the unknown-entity error.
Release Target Database Helpers
apps/workspace-engine/pkg/db/release_targets.go
New file with writeReleaseTarget(ctx, *oapi.ReleaseTarget, pgx.Tx) performing INSERT ... ON CONFLICT DO NOTHING and deleteReleaseTarget(ctx, *oapi.ReleaseTarget, pgx.Tx) performing DELETE by natural key (resource_id, environment_id, deployment_id).
Release Target Tests
apps/workspace-engine/pkg/db/release_targets_test.go
New tests: write/delete lifecycle, multiple cycles, idempotent writes, deleting nonexistent, and multiple different targets; use transactional helpers and assertions against DB counts.
CI Workflow Permissions
.github/workflows/golang-tests-with-db.yaml, .github/workflows/golang-tests.yaml
Add permissions block granting contents: read and pull-requests: write to the workspace-engine-tests job(s); no step/control-flow changes.

Sequence Diagram(s)

sequenceDiagram
    participant Changeset as Changeset Application
    participant Apply as applyChange
    participant DBH as ReleaseTarget Handler
    participant DB as Database

    Changeset->>Apply: apply change containing ReleaseTarget entity
    Apply->>Apply: detect entity type == ReleaseTarget
    alt ChangeType == Delete
        Apply->>DBH: deleteReleaseTarget(ctx, entity, tx)
        DBH->>DB: DELETE FROM release_target WHERE resource_id=?, environment_id=?, deployment_id=?
        DB-->>DBH: result / error
    else ChangeType != Delete
        Apply->>DBH: writeReleaseTarget(ctx, entity, tx)
        DBH->>DB: INSERT INTO release_target (...) ON CONFLICT DO NOTHING
        DB-->>DBH: result / error
    end
    DBH-->>Apply: return error or nil
    Apply-->>Changeset: complete
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested reviewers

  • jsbroks

Poem

🐰 In the changeset burrow I nudge and write,

ReleaseTargets hop into rows just right.
Delete or insert, conflicts bypassed,
Tests keep them tidy, each run and last.
A little rabbit clap — code landed fast! 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "chore: write release targets for db changeset compatibility" accurately describes the main change in the changeset. The primary modifications add support for ReleaseTarget entities in the changeset application path (changeset.go), introduce helper functions to write and delete release targets (release_targets.go), and add comprehensive tests for this functionality. The title is specific and clear—a developer reviewing the repository history would understand that this PR enables database changeset compatibility for release targets. The title is concise and avoids vague terms or unnecessary noise.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch write-release-targets-for-db-compatibility

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 018e51c and 5da2727.

📒 Files selected for processing (2)
  • .github/workflows/golang-tests-with-db.yaml (1 hunks)
  • .github/workflows/golang-tests.yaml (1 hunks)
🔇 Additional comments (2)
.github/workflows/golang-tests-with-db.yaml (1)

15-17: Verify PR comment permission scope.

The workflow adds pull-requests: write to support the "Comment coverage on PR" step (line 120–137), which uses github.rest.issues.createComment. However, GitHub's API model typically requires issues: write for the issues.createComment endpoint, since PRs are represented as issues under the hood. The pull-requests: write permission is primarily for PR lifecycle management (create/update PRs), not commenting on existing issues/PRs.

Please verify against GitHub Actions permissions documentation whether pull-requests: write is sufficient for issues.createComment, or if this should be changed to issues: write. If the step has been tested and confirmed to work, a comment here clarifying that would be helpful.

.github/workflows/golang-tests.yaml (1)

12-14: Verify PR comment permission scope (consistency with golang-tests-with-db.yaml).

Similar to the change in .github/workflows/golang-tests-with-db.yaml, this adds pull-requests: write for the "Comment coverage on PR" step (line 62–78). The same permission scope question applies: github.rest.issues.createComment may require issues: write rather than pull-requests: write.

If you've tested this and confirmed the permission works, please add a clarifying comment. Otherwise, consider verifying against GitHub Actions documentation and potentially changing both workflows to use issues: write if that is the correct permission for commenting on PRs.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 392e6eb and 018e51c.

📒 Files selected for processing (3)
  • apps/workspace-engine/pkg/db/changeset.go (1 hunks)
  • apps/workspace-engine/pkg/db/release_targets.go (1 hunks)
  • apps/workspace-engine/pkg/db/release_targets_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
apps/workspace-engine/**/*.go

📄 CodeRabbit inference engine (apps/workspace-engine/CLAUDE.md)

apps/workspace-engine/**/*.go: Do not add extraneous inline comments that state the obvious
Do not add comments that simply restate what the code does
Do not add comments for standard Go patterns (e.g., noting WaitGroup or semaphore usage)
Write comments that explain why, document complex logic/algorithms, provide non-obvious context, include TODO/FIXME, and document exported functions/types/methods

Files:

  • apps/workspace-engine/pkg/db/release_targets_test.go
  • apps/workspace-engine/pkg/db/changeset.go
  • apps/workspace-engine/pkg/db/release_targets.go
apps/workspace-engine/**/*_test.go

📄 CodeRabbit inference engine (apps/workspace-engine/CLAUDE.md)

Follow the existing test structure used in *_test.go files

Files:

  • apps/workspace-engine/pkg/db/release_targets_test.go
🧬 Code graph analysis (3)
apps/workspace-engine/pkg/db/release_targets_test.go (1)
apps/workspace-engine/pkg/oapi/oapi.gen.go (3)
  • ResourceId (466-466)
  • EnvironmentId (457-457)
  • DeploymentId (448-448)
apps/workspace-engine/pkg/db/changeset.go (2)
apps/workspace-engine/pkg/oapi/oapi.gen.go (1)
  • ReleaseTarget (351-355)
apps/workspace-engine/pkg/changeset/changeset.go (1)
  • ChangeTypeDelete (13-13)
apps/workspace-engine/pkg/db/release_targets.go (1)
apps/workspace-engine/pkg/oapi/oapi.gen.go (3)
  • ResourceId (466-466)
  • EnvironmentId (457-457)
  • DeploymentId (448-448)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: build (linux/amd64)
  • GitHub Check: Lint
  • GitHub Check: Typecheck
🔇 Additional comments (6)
apps/workspace-engine/pkg/db/changeset.go (1)

139-144: LGTM!

The ReleaseTarget handling follows the exact same pattern as the other entity types and integrates cleanly into the changeset application flow.

apps/workspace-engine/pkg/db/release_targets.go (1)

10-30: LGTM!

The implementation is clean and idempotent by design:

  • INSERT with ON CONFLICT DO NOTHING ensures safe repeated writes
  • DELETE with composite key filtering is correct
  • Parameter ordering matches query placeholders
apps/workspace-engine/pkg/db/release_targets_test.go (4)

12-70: LGTM!

The test correctly verifies the write (created by prerequisites) and delete operations using count queries before and after deletion.


74-169: LGTM!

The test properly validates multiple write/delete cycles by first cleaning up the prerequisite-created target, then iterating through 3 cycles with count verification after each operation.


173-244: LGTM!

The test correctly validates that ON CONFLICT DO NOTHING makes writes idempotent by writing the same target multiple times within a transaction and verifying only one record exists.


286-431: LGTM!

The test thoroughly validates handling of multiple different release targets, including creation, coexistence verification, selective deletion, and verification that the correct target remains.

Comment on lines +248 to +282
func TestReleaseTarget_DeleteNonexistent(t *testing.T) {
workspaceID, conn := setupTestWithWorkspace(t)
ctx := t.Context()

// Create prerequisites
systemID, deploymentID, _, resourceID, environmentID := createReleasePrerequisites(
t, workspaceID, conn)

releaseTarget := &oapi.ReleaseTarget{
ResourceId: resourceID,
EnvironmentId: environmentID,
DeploymentId: deploymentID,
}

// Delete a release target that was never created
tx, err := conn.Begin(ctx)
if err != nil {
t.Fatalf("failed to begin tx: %v", err)
}
defer tx.Rollback(ctx)

// This should not error, just delete 0 rows
err = deleteReleaseTarget(ctx, releaseTarget, tx)
if err != nil {
t.Fatalf("expected no error deleting non-existent release target, got: %v", err)
}

err = tx.Commit(ctx)
if err != nil {
t.Fatalf("failed to commit tx: %v", err)
}

// Keep systemID to avoid "declared but not used" error
_ = systemID
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Test is not validating deletion of non-existent target.

The test creates prerequisites (which includes a release_target), then immediately tries to delete it using the same IDs. This deletes the existing target rather than a non-existent one. To properly test idempotency of deleting a non-existent target, you should either:

  1. Delete the prerequisite-created target first, then delete it again, OR
  2. Use different IDs that don't exist in the database

Apply this diff to fix option 1 (delete twice):

 	releaseTarget := &oapi.ReleaseTarget{
 		ResourceId:    resourceID,
 		EnvironmentId: environmentID,
 		DeploymentId:  deploymentID,
 	}
 
+	// First delete the release target created by createReleasePrerequisites
+	tx, err := conn.Begin(ctx)
+	if err != nil {
+		t.Fatalf("failed to begin first delete tx: %v", err)
+	}
+
+	err = deleteReleaseTarget(ctx, releaseTarget, tx)
+	if err != nil {
+		tx.Rollback(ctx)
+		t.Fatalf("failed to delete prerequisite release target: %v", err)
+	}
+
+	err = tx.Commit(ctx)
+	if err != nil {
+		t.Fatalf("failed to commit first delete tx: %v", err)
+	}
+
-	// Delete a release target that was never created
+	// Now delete the same target again (non-existent)
-	tx, err := conn.Begin(ctx)
+	tx, err = conn.Begin(ctx)
 	if err != nil {
 		t.Fatalf("failed to begin tx: %v", err)
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func TestReleaseTarget_DeleteNonexistent(t *testing.T) {
workspaceID, conn := setupTestWithWorkspace(t)
ctx := t.Context()
// Create prerequisites
systemID, deploymentID, _, resourceID, environmentID := createReleasePrerequisites(
t, workspaceID, conn)
releaseTarget := &oapi.ReleaseTarget{
ResourceId: resourceID,
EnvironmentId: environmentID,
DeploymentId: deploymentID,
}
// Delete a release target that was never created
tx, err := conn.Begin(ctx)
if err != nil {
t.Fatalf("failed to begin tx: %v", err)
}
defer tx.Rollback(ctx)
// This should not error, just delete 0 rows
err = deleteReleaseTarget(ctx, releaseTarget, tx)
if err != nil {
t.Fatalf("expected no error deleting non-existent release target, got: %v", err)
}
err = tx.Commit(ctx)
if err != nil {
t.Fatalf("failed to commit tx: %v", err)
}
// Keep systemID to avoid "declared but not used" error
_ = systemID
}
func TestReleaseTarget_DeleteNonexistent(t *testing.T) {
workspaceID, conn := setupTestWithWorkspace(t)
ctx := t.Context()
// Create prerequisites
systemID, deploymentID, _, resourceID, environmentID := createReleasePrerequisites(
t, workspaceID, conn)
releaseTarget := &oapi.ReleaseTarget{
ResourceId: resourceID,
EnvironmentId: environmentID,
DeploymentId: deploymentID,
}
// First delete the release target created by createReleasePrerequisites
tx, err := conn.Begin(ctx)
if err != nil {
t.Fatalf("failed to begin first delete tx: %v", err)
}
err = deleteReleaseTarget(ctx, releaseTarget, tx)
if err != nil {
tx.Rollback(ctx)
t.Fatalf("failed to delete prerequisite release target: %v", err)
}
err = tx.Commit(ctx)
if err != nil {
t.Fatalf("failed to commit first delete tx: %v", err)
}
// Now delete the same target again (non-existent)
tx, err = conn.Begin(ctx)
if err != nil {
t.Fatalf("failed to begin tx: %v", err)
}
defer tx.Rollback(ctx)
// This should not error, just delete 0 rows
err = deleteReleaseTarget(ctx, releaseTarget, tx)
if err != nil {
t.Fatalf("expected no error deleting non-existent release target, got: %v", err)
}
err = tx.Commit(ctx)
if err != nil {
t.Fatalf("failed to commit tx: %v", err)
}
// Keep systemID to avoid "declared but not used" error
_ = systemID
}
🤖 Prompt for AI Agents
In apps/workspace-engine/pkg/db/release_targets_test.go around lines 248 to 282,
the test intends to verify deleting a non-existent release target but currently
deletes an actually existing target because prerequisites include a created
release_target; update the test to delete the created target once and then
attempt to delete it again to validate idempotency: after committing the first
successful delete (or before commit if using the same tx), perform a second call
to deleteReleaseTarget with the same releaseTarget and assert no error and that
nothing breaks (i.e., delete twice), ensuring the test cleans up and keeps
systemID unused to avoid warnings.

@github-actions
Copy link
Copy Markdown
Contributor

📊 Code Coverage Report

workspace-engine coverage: 48.5%

View detailed coverage report in artifacts

@github-actions
Copy link
Copy Markdown
Contributor

📊 DB Package Test Coverage

pkg/db coverage: 56.1%

View detailed coverage report in artifacts

@adityachoudhari26 adityachoudhari26 merged commit 1824ef0 into main Oct 17, 2025
7 of 8 checks passed
@adityachoudhari26 adityachoudhari26 deleted the write-release-targets-for-db-compatibility branch October 17, 2025 17:21
@coderabbitai coderabbitai bot mentioned this pull request Nov 6, 2025
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.

1 participant