Skip to content

test: share postgres container across suites - #1934

Open
RushikeshBhavsar3605 wants to merge 4 commits into
raystack:mainfrom
RushikeshBhavsar3605:fix/shared-postgres-test-container
Open

RushikeshBhavsar3605 wants to merge 4 commits into
raystack:mainfrom
RushikeshBhavsar3605:fix/shared-postgres-test-container

Conversation

@RushikeshBhavsar3605

Copy link
Copy Markdown
Contributor

Summary

Fixes #1932.

The Postgres repository tests were starting a separate Postgres container and running migrations for every test suite. This made the package slow and prone to hitting the test timeout.

Changes

  • Added package-level TestMain to start and tear down a single Postgres container.
  • Run database migrations once during package setup.
  • Create an isolated database for each test suite using the migrated test_db as a template.
  • Removed the 120-second container expiry from the per-suite setup.
  • Closed migration database connections so the migrated database can be used as a template.

Test Plan

  • go test . -count=1
  • go test -race -count=2 .
  • go vet .
  • git diff --check

@vercel

vercel Bot commented Sep 17, 2026

Copy link
Copy Markdown

@RushikeshBhavsar3605 is attempting to deploy a commit to the Raystack Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: raystack/frontier/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: a12fc143-efc1-4c88-84cd-667df997cc3d

📥 Commits

Reviewing files that changed from the base of the PR and between f1d2a5d and 4b0b269.

📒 Files selected for processing (3)
  • internal/store/postgres/domain_repository_test.go
  • internal/store/postgres/invitation_repository_test.go
  • internal/store/postgres/org_projects_repository_pg_test.go
💤 Files with no reviewable changes (2)
  • internal/store/postgres/domain_repository_test.go
  • internal/store/postgres/invitation_repository_test.go

Included review availability: Your plan provides up to 2 included reviews per hour; 0 remain after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes

    • Migration resources and database connections are now closed after use.
    • Database connections are closed if the initial health check fails, preventing resource leaks.
  • Tests

    • PostgreSQL integration tests share a test environment while using isolated databases for individual suites.
    • Test database clients are closed after each suite.
    • Test setup and cleanup are more consistent and efficient.

Walkthrough

Migration commands now defer resource closure, and database initialization closes a connection after a failed ping. PostgreSQL integration tests share one Docker container, migrate a template database once, and create unique databases for test clients.

Changes

Database resource lifecycle

Layer / File(s) Summary
Migration command cleanup
cmd/migrate.go, pkg/db/db.go
Migration commands defer closure of migration instances and database connections. New closes the connection when d.Ping() fails.

Shared PostgreSQL test fixture

Layer / File(s) Summary
Shared container and database lifecycle
internal/store/postgres/postgres_test.go
TestMain manages the shared PostgreSQL container and template database. newTestClient creates a unique database from the template and connects a client.
Suite client setup and teardown
internal/store/postgres/*_test.go
Test suites use the shared fixture and close their clients during teardown instead of purging Docker resources.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Other · Severity of issue fixed: Medium

Suggested reviewers: amangit07

Merge Risk: ⚪ Minimal · up to 4b0b2

The PostgreSQL suites now share a container while using isolated databases, and the previously reported stale test call is corrected. No concrete merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes satisfy the coding requirements in #1932. TestMain provides one package-level PostgreSQL container, runs migrations once, and cleans up after m.Run. newTestClient() creates an isolat…
Out of Scope Changes check ✅ Passed The changes stay within #1932. The test refactors implement shared container lifetime, one-time migrations, isolated suite databases, and client cleanup. Connection cleanup in cmd/migrate.go and `pk…
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR

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.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 65be7a0f-0552-41eb-85de-b59e606a3e67

📥 Commits

Reviewing files that changed from the base of the PR and between e17b050 and 5f78b45.

📒 Files selected for processing (2)
  • cmd/migrate.go
  • internal/store/postgres/postgres_test.go

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.

Comment thread internal/store/postgres/postgres_test.go Outdated

@rohilsurana rohilsurana left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Went through this properly and also ran it locally. The speedup is real, so thanks for picking it up. A few things I noticed while reading. None of these are hard blockers on their own, and I have left a separate note about the rebase.

Comment thread internal/store/postgres/postgres_test.go Outdated
Comment thread internal/store/postgres/postgres_test.go
Comment thread internal/store/postgres/postgres_test.go Outdated
Comment thread cmd/migrate.go
@rohilsurana

Copy link
Copy Markdown
Member

Could you rebase this on the latest main?

#1929 merged after you cut this branch, and it added internal/store/postgres/org_serviceuser_repository_pg_test.go. That file still calls purgeDocker, which this PR deletes. So the package stops compiling once the two are put together:

internal/store/postgres/org_serviceuser_repository_pg_test.go:50:12: undefined: purgeDocker

The checks on this PR are green, but that run is from 17 Sep and #1929 went in on 21 Sep. CI has not built the two together yet, so the green tick does not cover this.

The fix is the same edit you already made to the other 22 suites. Point that file's SetupSuite and TearDownSuite at closeTestClient(s.client).

On the change itself, I ran it locally and the numbers hold up well:

main this PR
-count 1 58.9s 21.9s
-count 2, what make test uses 125.4s 19.5s

The part I like most is that the -count 2 time is the same as -count 1. The container cost is paid once now, so adding new suites stays cheap. That was the whole point of #1932, so this does the job.

@RushikeshBhavsar3605
RushikeshBhavsar3605 force-pushed the fix/shared-postgres-test-container branch from 6a9d5de to f1d2a5d Compare September 24, 2026 12:44

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: raystack/frontier/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 471e596a-67c0-4cd3-9b60-1e9b1f6ce1bf

📥 Commits

Reviewing files that changed from the base of the PR and between 6a9d5de and f1d2a5d.

📒 Files selected for processing (29)
  • cmd/migrate.go
  • internal/store/postgres/audit_record_repository_test.go
  • internal/store/postgres/billing_customer_repository_test.go
  • internal/store/postgres/billing_product_repository_test.go
  • internal/store/postgres/billing_transactions_repository_pg_test.go
  • internal/store/postgres/domain_repository_test.go
  • internal/store/postgres/group_repository_test.go
  • internal/store/postgres/invitation_repository_test.go
  • internal/store/postgres/kyc_repository_test.go
  • internal/store/postgres/lock_test.go
  • internal/store/postgres/namespace_repository_test.go
  • internal/store/postgres/org_serviceuser_repository_pg_test.go
  • internal/store/postgres/org_users_repository_pg_test.go
  • internal/store/postgres/organization_repository_test.go
  • internal/store/postgres/permission_repository_test.go
  • internal/store/postgres/policy_repository_test.go
  • internal/store/postgres/postgres_test.go
  • internal/store/postgres/preference_repository_test.go
  • internal/store/postgres/project_repository_test.go
  • internal/store/postgres/prospect_repository_test.go
  • internal/store/postgres/relation_repository_test.go
  • internal/store/postgres/resource_repository_test.go
  • internal/store/postgres/role_repository_test.go
  • internal/store/postgres/serviceuser_repository_test.go
  • internal/store/postgres/user_consent_repository_test.go
  • internal/store/postgres/user_repository_test.go
  • internal/store/postgres/userpat_repository_test.go
  • internal/store/postgres/webhook_endpoint_repository_test.go
  • pkg/db/db.go

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.

Comment thread internal/store/postgres/postgres_test.go
@RushikeshBhavsar3605

Copy link
Copy Markdown
Contributor Author

Rebased onto the latest main and updated the newly introduced PostgreSQL test as well. The changes are pushed in the latest commit.

This branch has not been deployed

No deployments
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.

Share one postgres container across the internal/store/postgres test suites

2 participants