Skip to content

Conversation

@Mischulee
Copy link
Contributor

@Mischulee Mischulee commented Jan 8, 2026

https://issues.redhat.com/browse/HYPERFLEET-302

Summary by CodeRabbit

  • Tests
    • Improved database cleanup: dynamically discover public tables and compute a safe drop order respecting foreign-key dependencies.
    • Added cycle detection and robust error handling; drop operations now skip protected system/migrations tables and only remove tables if present.

✏️ Tip: You can customize this high-level summary in your review settings.

@openshift-ci openshift-ci bot requested review from 86254860 and rh-amarin January 8, 2026 12:55
@openshift-ci
Copy link

openshift-ci bot commented Jan 8, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign rafabene for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai
Copy link

coderabbitai bot commented Jan 8, 2026

Walkthrough

The test database cleanup was changed from a static list of tables to dynamic discovery and dependency-aware ordering. New helpers query PostgreSQL's information_schema to list public tables, fetch foreign-key dependencies for each table, detect cycles, and perform a DFS-based topological sort. CleanDB now drops tables in the computed safe order (skipping configured system tables and non-existent tables) and surfaces errors from discovery, ordering, or drop operations. GORM was added for DB interactions.

Sequence Diagram(s)

sequenceDiagram
  participant TestHelper
  participant GORM
  participant Postgres

  TestHelper->>GORM: Open connection
  TestHelper->>GORM: getAllTables()
  GORM->>Postgres: query pg_tables (exclude system)
  Postgres-->>GORM: list of tables
  GORM-->>TestHelper: tables

  loop for each table
    TestHelper->>GORM: getTableDependencies(table)
    GORM->>Postgres: query information_schema for FK refs
    Postgres-->>GORM: referenced tables
    GORM-->>TestHelper: dependencies
  end

  TestHelper->>TestHelper: orderTablesByDependencies(tables, deps)
  TestHelper->>GORM: drop table in safe order (IF EXISTS ... CASCADE)
  GORM->>Postgres: execute DROP TABLE
  Postgres-->>GORM: result
  GORM-->>TestHelper: success / error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: replacing hard-coded table deletion with dynamic table discovery and ordering by dependencies.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a9c2f0f and c960353.

📒 Files selected for processing (1)
  • test/helper.go
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Use Go 1.24.9 with FIPS-compliant crypto enabled (CGO_ENABLED=1, GOEXPERIMENT=boringcrypto)
Use structured logging via logger.NewOCMLogger(ctx) with context-aware log fields including operation ID, account ID, and transaction ID

Files:

  • test/helper.go
🧠 Learnings (8)
📓 Common learnings
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/db/**/*.go : Use PostgreSQL 14.2 with GORM ORM for database operations
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/db/**/*.go : Use PostgreSQL 14.2 with GORM ORM for database operations

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to cmd/hyperfleet/migrate/**/*.go : Use GORM AutoMigrate at startup via ./bin/hyperfleet-api migrate command; migrations are idempotent and track applied migrations

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/{dao,db}/**/*.go : Use polymorphic status tables with owner_type + owner_id to support multiple resource types without creating separate status tables

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/db/migrations/**/*.go : Store spec and conditions as JSONB in PostgreSQL to enable complex field queries

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to test/integration/**/*_test.go : Integration tests must use Testcontainers to create isolated PostgreSQL instances for each test suite to ensure no state leakage

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/{db/migrations,dao}/**/*.go : NodePool owner_references.id must enforce foreign key relationship to parent cluster to maintain referential integrity

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/api/**/*.go : Embed OpenAPI specification at compile time using Go 1.16+ //go:embed directive for self-contained binary deployment

Applied to files:

  • test/helper.go
🔇 Additional comments (5)
test/helper.go (5)

342-366: LGTM! Well-structured dynamic table cleanup.

The implementation correctly discovers tables, orders them by foreign key dependencies, and drops them safely. Error handling is comprehensive and appropriate for test cleanup.


368-378: System table protection is correctly implemented.

The migrations table is appropriately excluded from cleanup to preserve migration state across test runs.


380-394: Table discovery query is correct and secure.

The query appropriately uses PostgreSQL's system catalog, scopes to the public schema, and excludes system tables with proper parameterization.


396-453: Topological sort implementation is correct.

The DFS-based ordering correctly ensures child tables (with foreign keys) are dropped before parent tables (referenced tables). The circular dependency detection properly identifies cycles in the FK graph.

Note: If the schema contains circular foreign key dependencies (including self-references), the cleanup will fail with an error. This is acceptable for test cleanup, as such schemas are uncommon in test databases.


455-475: Foreign key dependency query is correct.

The query properly uses information_schema to identify parent tables (tables referenced by foreign keys) for dependency ordering. The use of DISTINCT prevents duplicate entries for tables with multiple FK relationships.


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

Copy link

@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: 2

🤖 Fix all issues with AI agents
In @test/helper.go:
- Around line 384-432: orderTablesByDependencies builds dependency edges from
getTableDependencies but does not ignore excluded tables (e.g., "migrations"),
so extraneous tables can be added to the DFS and returned; change the function
to first build a set of input tables and when populating dependencies for each
table (and when traversing deps in visit) only include dependency names that
exist in that input set (i.e., filter deps returned by getTableDependencies
against the original tables slice/set), ensuring only tables present in the
original input are considered and preventing excluded tables from being
ordered/dropped.
- Around line 400-418: The DFS closure visit currently silently returns nil when
a cycle is detected (when visiting[table] is true); change this to surface the
issue by returning an error (e.g., fmt.Errorf("cycle detected at table %s",
table)) so callers see circular foreign-key references and the drop order isn't
silently wrong; update the visit closure (and any callers that expect an error)
to propagate this error and consider including context such as the current
dependency path (using visiting or a stack) for clearer diagnostics while
leaving visited and ordered logic intact.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6b4c857 and a9c2f0f.

📒 Files selected for processing (1)
  • test/helper.go
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Use Go 1.24.9 with FIPS-compliant crypto enabled (CGO_ENABLED=1, GOEXPERIMENT=boringcrypto)
Use structured logging via logger.NewOCMLogger(ctx) with context-aware log fields including operation ID, account ID, and transaction ID

Files:

  • test/helper.go
🧠 Learnings (7)
📓 Common learnings
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/db/**/*.go : Use PostgreSQL 14.2 with GORM ORM for database operations
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to test/integration/**/*_test.go : Integration tests must use Testcontainers to create isolated PostgreSQL instances for each test suite to ensure no state leakage
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/db/**/*.go : Use PostgreSQL 14.2 with GORM ORM for database operations

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/{dao,db}/**/*.go : Use polymorphic status tables with owner_type + owner_id to support multiple resource types without creating separate status tables

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to test/integration/**/*_test.go : Integration tests must use Testcontainers to create isolated PostgreSQL instances for each test suite to ensure no state leakage

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/db/migrations/**/*.go : Store spec and conditions as JSONB in PostgreSQL to enable complex field queries

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to cmd/hyperfleet/migrate/**/*.go : Use GORM AutoMigrate at startup via ./bin/hyperfleet-api migrate command; migrations are idempotent and track applied migrations

Applied to files:

  • test/helper.go
📚 Learning: 2026-01-08T01:23:20.634Z
Learnt from: CR
Repo: openshift-hyperfleet/hyperfleet-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-01-08T01:23:20.634Z
Learning: Applies to pkg/api/**/*.go : Embed OpenAPI specification at compile time using Go 1.16+ //go:embed directive for self-contained binary deployment

Applied to files:

  • test/helper.go
🔇 Additional comments (4)
test/helper.go (4)

21-21: LGTM!

The GORM import is necessary for the new helper methods that accept *gorm.DB as a parameter. This is consistent with the project's established use of GORM ORM.


342-366: LGTM!

The refactored CleanDB function properly handles dynamic table discovery with dependency-aware ordering. The defensive HasTable check before DropTable and consistent error handling pattern are good practices.


368-382: LGTM!

The query correctly discovers user tables in the public schema while preserving the migrations table. This is PostgreSQL-specific but appropriate given the project uses PostgreSQL 14.2 per the coding guidelines.


434-454: LGTM!

The query correctly retrieves foreign key dependencies from information_schema. Using a parameterized query prevents SQL injection, and DISTINCT handles multi-column foreign keys appropriately.

return false
}

func (helper *Helper) getAllTables(g2 *gorm.DB) ([]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use helper.DBFactory creates sessions internally instead of pass the g2 variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yasun1 I think reusing same session for multiple related queries is more efficient than creating new one each time and it gives more control over the context

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.

2 participants