Skip to content

refactor: make gateway health non-nullable and unify challenge types#4428

Merged
Flo4604 merged 1 commit intomainfrom
small-db-stuff
Nov 27, 2025
Merged

refactor: make gateway health non-nullable and unify challenge types#4428
Flo4604 merged 1 commit intomainfrom
small-db-stuff

Conversation

@chronark
Copy link
Collaborator

No description provided.

@vercel
Copy link

vercel bot commented Nov 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
dashboard Ready Ready Preview Comment Nov 26, 2025 4:49pm
engineering Ready Ready Preview Comment Nov 26, 2025 4:49pm

@changeset-bot
Copy link

changeset-bot bot commented Nov 26, 2025

⚠️ No Changeset found

Latest commit: 493a7db

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 26, 2025

📝 Walkthrough

Walkthrough

This pull request refactors database models and schema by renaming challenge type fields, reorganizing ACME challenge enum types, simplifying gateway health logic, and updating type signatures across generated Go code and TypeScript schemas to reflect these structural changes.

Changes

Cohort / File(s) Summary
Gateway Health Type Simplification
go/apps/ingress/services/router/service.go, go/cmd/dev/seed/ingress.go, go/pkg/db/gateway_insert.sql_generated.go
Simplified health field from nullable NullGatewaysHealth struct to direct GatewaysHealth enum type; gateway selection logic updated to compare health value directly without explicit validity check.
ACME Challenge Type Restructuring
go/pkg/db/models_generated.go
Major type reorganization: AcmeChallengesStatus renamed to AcmeChallengesChallengeType, AcmeChallengesType to AcmeChallengesStatus; constants updated from status values (waiting/pending/verified/failed) to challenge types (HTTP-01/DNS-01); all Scan/Value methods and null wrappers updated accordingly; AcmeChallenge struct field renamed from Type to ChallengeType.
ACME Challenge SQL Generated Code
go/pkg/db/acme_challenge_find_by_token.sql_generated.go, go/pkg/db/acme_challenge_insert.sql_generated.go, go/pkg/db/acme_challenge_list_executable.sql_generated.go, go/pkg/db/bulk_acme_challenge_insert.sql_generated.go
Updated column reference from type to challenge_type, renamed parameter and field accessors from Type to ChallengeType, updated type signatures from AcmeChallengesType to AcmeChallengesChallengeType.
Generated Query Interface
go/pkg/db/querier_generated.go
Updated ListExecutableChallenges method signature to use []AcmeChallengesChallengeType parameter type instead of []AcmeChallengesType.
SQL Query Definitions
go/pkg/db/queries/acme_challenge_insert.sql, go/pkg/db/queries/acme_challenge_list_executable.sql
Updated column references from type to challenge_type in INSERT and SELECT statements, corresponding filter conditions updated in WHERE clauses.
Database Schema
go/pkg/db/schema.sql
acme_challenges column renamed from type to challenge_type with enum('HTTP-01','DNS-01') NOT NULL; custom_domains.challenge_type enum values changed from ('dns01','http01') to ('HTTP-01','DNS-01') with no default; gateways.health enum expanded to include 'unknown' value with default.
TypeScript Database Schema
internal/db/src/schema/acme_challenges.ts, internal/db/src/schema/custom_domains.ts, internal/db/src/schema/gateways.ts
Introduced new shared challengeType constant with HTTP-01/DNS-01 values in acme_challenges schema; custom_domains now imports and references challengeType instead of inline enum; gateways health column now includes 'unknown' state with notNull() and default('unknown').

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

  • Type renaming consistency: Verify all occurrences of renamed types (AcmeChallengesChallengeType, AcmeChallengesStatus, GatewaysHealth) across Go generated files are updated consistently and no stale references remain.
  • Gateway health logic change: The simplified health check in SelectGateway() removing explicit Valid field check—confirm this aligns with the new non-nullable GatewaysHealth type and does not alter intended filtering behavior.
  • Enum value semantic changes: ACME challenge constants changed from status-oriented (waiting/pending) to type-oriented (HTTP-01/DNS-01)—verify this semantic shift is intentional and all downstream code interprets these values correctly.
  • SQL schema migration implications: Verify that the custom_domains enum default removal and gateways health default addition are compatible with existing data and migration strategy.
  • Generated code regeneration: Confirm all SQL-generated files are consistently regenerated from new schema definitions.

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning The pull request description is empty, missing all required template sections including motivation, type of change, testing instructions, and the required checklist. Add a complete description following the template: explain the refactoring rationale, mark the change type, document how to test these database schema/type changes, and complete all required checklist items.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main refactoring changes: making gateway health non-nullable and unifying challenge type naming conventions across the codebase.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch small-db-stuff

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.

@vercel vercel bot temporarily deployed to Preview – engineering November 26, 2025 16:47 Inactive
@vercel vercel bot temporarily deployed to Preview – dashboard November 26, 2025 16:49 Inactive
Copy link
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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/db/src/schema/acme_challenges.ts (1)

3-26: Break the new circular dependency between acme_challenges.ts and custom_domains.ts.

acme_challenges.ts imports customDomains (for acmeChallengeRelations), and custom_domains.ts now imports challengeType from ./acme_challenges. This creates a runtime import cycle where:

  • challengeType is used at top level to build table schemas in custom_domains.ts, and
  • customDomains is used at top level for relations in acme_challenges.ts.

In both CJS and ESM, this pattern can leave one side uninitialized/undefined at module initialization time, leading to hard‑to‑debug runtime errors or incorrect schema wiring.

Consider one of these options to break the cycle:

  • Move challengeType into a small, dependency‑free module (e.g. ./challenge_type or ./util/challenge_type) imported by both acme_challenges.ts and custom_domains.ts, or
  • Keep challengeType here but move acmeChallengeRelations into a separate relations file (or into custom_domains.ts) that imports both tables, so acme_challenges.ts no longer needs to import customDomains.

Once the cycle is broken, the shared enum + .notNull() and the constraint array look good.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aae72c0 and 493a7db.

📒 Files selected for processing (15)
  • go/apps/ingress/services/router/service.go (1 hunks)
  • go/cmd/dev/seed/ingress.go (1 hunks)
  • go/pkg/db/acme_challenge_find_by_token.sql_generated.go (2 hunks)
  • go/pkg/db/acme_challenge_insert.sql_generated.go (4 hunks)
  • go/pkg/db/acme_challenge_list_executable.sql_generated.go (2 hunks)
  • go/pkg/db/bulk_acme_challenge_insert.sql_generated.go (2 hunks)
  • go/pkg/db/gateway_insert.sql_generated.go (1 hunks)
  • go/pkg/db/models_generated.go (5 hunks)
  • go/pkg/db/querier_generated.go (3 hunks)
  • go/pkg/db/queries/acme_challenge_insert.sql (1 hunks)
  • go/pkg/db/queries/acme_challenge_list_executable.sql (1 hunks)
  • go/pkg/db/schema.sql (3 hunks)
  • internal/db/src/schema/acme_challenges.ts (1 hunks)
  • internal/db/src/schema/custom_domains.ts (2 hunks)
  • internal/db/src/schema/gateways.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Flo4604
Repo: unkeyed/unkey PR: 4190
File: go/internal/services/keys/verifier.go:51-53
Timestamp: 2025-10-30T15:10:52.743Z
Learning: PR #4190 for unkeyed/unkey is focused solely on database schema and query changes for identity-based credits. It adds IdentityCredits and KeyCredits fields to structs and queries, but does not implement the priority enforcement logic in the usagelimiter. The logic implementation is intentionally deferred to a later PR in the stack.
📚 Learning: 2025-07-17T14:24:20.403Z
Learnt from: Flo4604
Repo: unkeyed/unkey PR: 3631
File: go/pkg/db/bulk_keyring_insert.sql.go:23-25
Timestamp: 2025-07-17T14:24:20.403Z
Learning: In go/pkg/db/bulk_keyring_insert.sql.go and similar bulk insert generated files, hardcoded zero values for fields like size_approx and size_last_updated_at are intentional and reflect the original SQL query structure, not missing parameters.

Applied to files:

  • go/pkg/db/gateway_insert.sql_generated.go
  • go/pkg/db/bulk_acme_challenge_insert.sql_generated.go
🧬 Code graph analysis (7)
go/apps/ingress/services/router/service.go (1)
go/pkg/db/models_generated.go (1)
  • GatewaysHealthHealthy (286-286)
go/pkg/db/gateway_insert.sql_generated.go (1)
go/pkg/db/models_generated.go (1)
  • GatewaysHealth (281-281)
go/pkg/db/acme_challenge_find_by_token.sql_generated.go (3)
go/pkg/db/queries.go (1)
  • Queries (3-3)
go/pkg/db/interface.go (1)
  • DBTX (29-34)
go/pkg/db/models_generated.go (1)
  • AcmeChallenge (627-637)
go/cmd/dev/seed/ingress.go (1)
go/pkg/db/models_generated.go (1)
  • GatewaysHealthHealthy (286-286)
go/pkg/db/acme_challenge_insert.sql_generated.go (1)
go/pkg/db/models_generated.go (2)
  • AcmeChallengesStatus (58-58)
  • AcmeChallengesChallengeType (16-16)
internal/db/src/schema/custom_domains.ts (1)
internal/db/src/schema/acme_challenges.ts (1)
  • challengeType (7-7)
go/pkg/db/acme_challenge_list_executable.sql_generated.go (3)
go/pkg/db/models_generated.go (1)
  • AcmeChallengesChallengeType (16-16)
go/pkg/db/queries.go (1)
  • Queries (3-3)
go/pkg/db/interface.go (1)
  • DBTX (29-34)
⏰ 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). (5)
  • GitHub Check: Test Dashboard / Test Dashboard
  • GitHub Check: Test Go API Local / Test
  • GitHub Check: Test API / API Test Local
  • GitHub Check: Test Packages / Test
  • GitHub Check: Build / Build
🔇 Additional comments (15)
internal/db/src/schema/custom_domains.ts (1)

1-19: Shared challengeType enum changes behavior for custom_domains.

Switching to the shared challengeType enum is good for consistency, but it has two notable behavior changes compared to the previous inline definition:

  • Enum values are now HTTP-01 / DNS-01 (different spelling/casing from the old http01/dns01 style mentioned in the summary).
  • The previous default is gone; challenge_type is now strictly NOT NULL with no default, so every insert must set it explicitly.

Please double‑check that:

  • Existing rows (if any) are migrated to the new enum values, and
  • All write paths that insert into custom_domains now explicitly set challenge_type, otherwise inserts will fail.

Also, this import contributes to a circular dependency with acme_challenges.ts; see the other comment suggesting how to break that cycle.

go/pkg/db/queries/acme_challenge_list_executable.sql (1)

2-6: Column rename to challenge_type is consistent and non‑breaking.

Updating both the SELECT list and IN (sqlc.slice(verification_types)) to use dc.challenge_type cleanly aligns this query with the new column and Go enum type, without changing the filter semantics.

go/pkg/db/schema.sql (1)

392-425: Schema enums now match the refactor; verify data/migration coverage.

The schema updates look internally consistent:

  • custom_domains.challenge_type and acme_challenges.challenge_type both use enum('HTTP-01','DNS-01') NOT NULL, matching the shared TS enum.
  • gateways.health is now a non‑nullable enum with an 'unknown' default, which works with the router logic that only routes when health = 'healthy'.

Please verify that:

  • Existing custom_domains / acme_challenges rows (and any migrations) are updated to the new HTTP-01 / DNS-01 values.
  • All insert/update code paths for gateways either set health explicitly or intentionally rely on 'unknown' being treated as non‑routable.

From a schema perspective this aligns with the refactor; the remaining risk is purely around data migration and callers.

go/pkg/db/queries/acme_challenge_insert.sql (1)

2-22: INSERT now targets challenge_type consistently.

Swapping type for challenge_type in the column list keeps the INSERT aligned with the new schema and generated InsertAcmeChallengeParams.ChallengeType field. Placeholders and argument order remain consistent.

go/apps/ingress/services/router/service.go (1)

100-108: Health check simplification matches the new non‑nullable enum.

Filtering with if gw.Health != db.GatewaysHealthHealthy { continue } correctly reflects the new non‑nullable GatewaysHealth enum: only 'healthy' gateways are considered, while 'paused', 'unhealthy', and the new 'unknown' default are excluded from routing as intended.

go/pkg/db/bulk_acme_challenge_insert.sql_generated.go (1)

12-41: Bulk insert updated cleanly to use challenge_type.

The bulk insert base query and argument mapping now include challenge_type and use arg.ChallengeType in the correct slot, keeping the placeholder count and argument order consistent with the schema and single‑row insert.

go/pkg/db/acme_challenge_find_by_token.sql_generated.go (1)

12-38: Find‑by‑token query now aligned with ChallengeType field.

Updating the SELECT list and row.Scan to use challenge_typei.ChallengeType matches the AcmeChallenge struct and the renamed column, with the column order preserved.

go/cmd/dev/seed/ingress.go (1)

130-130: LGTM!

The direct use of db.GatewaysHealthHealthy aligns correctly with the updated non-nullable Health field type in InsertGatewayParams.

go/pkg/db/gateway_insert.sql_generated.go (1)

36-45: LGTM!

The generated InsertGatewayParams struct correctly reflects the non-nullable Health field type change from NullGatewaysHealth to GatewaysHealth.

go/pkg/db/querier_generated.go (1)

1560-1567: LGTM!

The ListExecutableChallenges interface signature correctly reflects the column rename from type to challenge_type and the updated parameter type []AcmeChallengesChallengeType.

go/pkg/db/acme_challenge_insert.sql_generated.go (1)

37-47: LGTM!

The generated InsertAcmeChallengeParams struct correctly reflects the column rename from type to challenge_type with the appropriate type AcmeChallengesChallengeType and updated db tag.

go/pkg/db/acme_challenge_list_executable.sql_generated.go (1)

21-25: LGTM!

The ListExecutableChallengesRow struct and associated query logic correctly reflect the column rename to challenge_type with proper type and scanning.

go/pkg/db/models_generated.go (3)

281-288: LGTM!

The GatewaysHealth enum correctly includes the new unknown state, which serves as the default value for the non-nullable health column.


785-794: LGTM!

The Gateway struct correctly reflects the non-nullable Health field as GatewaysHealth instead of NullGatewaysHealth.


16-21: I'll help you verify the review comment about the ACME challenge type enum values. Let me start by exploring the codebase to confirm the database schema definitions and usage patterns.
<function_calls>

#!/bin/bash

Search for database schema files and migrations

find . -type f ( -name "*.sql" -o -name "migration" -o -name "schema" ) | head -20


</function_calls>

Now let me search for references to the challenge type values in the codebase:
<function_calls>

#!/bin/bash

Search for challenge type references and enum values

rg -n "HTTP-01|DNS-01|http01|dns01" -t go -t sql -C 2 | head -100


</function_calls>

Let me also check the models_generated.go file directly to understand the complete context:
<function_calls>

#!/bin/bash

Check the models_generated.go file to see the challenge type definitions

head -100 go/pkg/db/models_generated.go | cat -n


</function_calls>

Let me search more specifically for database table definitions and create statements:
<function_calls>

#!/bin/bash

Search for acme_challenges table definition

rg -n "acme_challenges|AcmeChallenge" -t go -t sql -B 5 -A 5 | head -150


</function_calls>

Let me also check if there are test files that show expected values:
<function_calls>

#!/bin/bash

Search for test files and example usage

find . -type f -name "test" | grep -E ".(go|sql)" | head -10


</function_calls>

<function_calls>

#!/bin/bash

Search for any hardcoded challenge type values in the codebase

rg -n '"HTTP-01"|"DNS-01"|"dns01"|"http01"' --type go -C 3 | head -80


</function_calls>

@graphite-app
Copy link

graphite-app bot commented Nov 26, 2025

Illustrated gif. A hand appears and holds up an oversized thumb, giving us a thumbs up. (Added via Giphy)

@graphite-app
Copy link

graphite-app bot commented Nov 26, 2025

Graphite Automations

"Post a GIF when PR approved" took an action on this PR • (11/26/25)

1 gif was posted to this PR based on Andreas Thomas's automation.

@Flo4604 Flo4604 merged commit 2b2079e into main Nov 27, 2025
27 checks passed
@Flo4604 Flo4604 deleted the small-db-stuff branch November 27, 2025 11:28
@coderabbitai coderabbitai bot mentioned this pull request Nov 28, 2025
19 tasks
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.

3 participants