Skip to content

refactor(database): consolidate dialect mappers and ErrUnknownDialect - #1285

Merged
kalbasit merged 1 commit into
mainfrom
refactor/consolidate-dialect-mappers
May 29, 2026
Merged

kalbasit merged 1 commit into
mainfrom
refactor/consolidate-dialect-mappers

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Summary

Post-Ent-migration cleanup. The database.Type → ent dialect-string mapping was duplicated: database.EntDialectFor (documented as shared with the migrate package) had a byte-for-byte copy entDialectFor in pkg/database/migrate/fresh.go, plus a parallel ErrUnknownDialect sentinel. The two goose mappers (gooseStoreDialectFor, gooseDialectFor) duplicated the same switch.

  • Route the migrate package through the shared database.EntDialectFor; delete the duplicate (and the now-unused entgo.io/ent/dialect import).
  • Collapse the duplicated ErrUnknownDialect to the single database.ErrUnknownDialect across state.go, adopt.go, apply.go.
  • Merge the goose mappers: goose.Dialect is a type alias of goose-store's database.Dialect, so fresh.go uses the shared gooseDialectFor.
  • Behavior-preserving; records a database-orm spec requirement for the single-source-of-truth invariant.

Test plan

  • task fmt / task lint (0 issues) / task test (full suite green)
  • Existing pkg/database and pkg/database/migrate suites cover dialect mapping + fresh install

Post-Ent-migration, the database.Type -> ent dialect-string mapping was
duplicated: database.EntDialectFor (documented as shared with the
migrate package) had a byte-for-byte copy entDialectFor in
pkg/database/migrate/fresh.go, plus a parallel ErrUnknownDialect
sentinel. The two goose mappers (gooseStoreDialectFor, gooseDialectFor)
also duplicated the same switch.

Route the migrate package through the shared database.EntDialectFor and
delete the duplicate. Collapse the duplicated ErrUnknownDialect to the
single database.ErrUnknownDialect across state.go, adopt.go, and
apply.go. Merge the goose mappers: goose.Dialect is a type alias of
goose's database.Dialect, so fresh.go now uses the shared
gooseDialectFor. Update the client.go doc comments to match reality.

Behavior-preserving: no schema, migration, or runtime change. Existing
pkg/database and pkg/database/migrate suites stay green; a new
database-orm spec requirement records the single-source-of-truth
invariant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit

kalbasit commented May 29, 2026 •

Copy link
Copy Markdown
Owner Author

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. go Pull requests that update go code labels May 29, 2026
@coderabbitai

coderabbitai Bot commented May 29, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b498d6c9-dd8b-4883-b8ea-868e5ab7ffd1

📥 Commits

Reviewing files that changed from the base of the PR and between 0948dd3 and c62f7e3.

📒 Files selected for processing (11)
  • openspec/changes/archive/2026-05-29-consolidate-dialect-mappers/.openspec.yaml
  • openspec/changes/archive/2026-05-29-consolidate-dialect-mappers/design.md
  • openspec/changes/archive/2026-05-29-consolidate-dialect-mappers/proposal.md
  • openspec/changes/archive/2026-05-29-consolidate-dialect-mappers/specs/database-orm/spec.md
  • openspec/changes/archive/2026-05-29-consolidate-dialect-mappers/tasks.md
  • openspec/specs/database-orm/spec.md
  • pkg/database/client.go
  • pkg/database/migrate/adopt.go
  • pkg/database/migrate/apply.go
  • pkg/database/migrate/fresh.go
  • pkg/database/migrate/state.go

📝 Walkthrough

Summary by CodeRabbit

  • Chores

    • Consolidated database dialect mapping logic to eliminate code duplication across packages. Unified error handling for unknown dialects through a centralized sentinel value.
  • Breaking Changes

    • Removed the exported migrate.ErrUnknownDialect error. Use the shared database.ErrUnknownDialect instead.

Walkthrough

This PR consolidates dialect-mapping logic by centralizing it to database.EntDialectFor and unifying error handling to use a shared database.ErrUnknownDialect sentinel. Design docs, specifications, and implementation changes remove duplicate dialect-mapping code from the fresh-install path and update all error call sites across the migrate package for consistency.

Changes

Dialect Mapper Consolidation

Layer / File(s) Summary
Design and specification documentation
openspec/changes/archive/2026-05-29-consolidate-dialect-mappers/.openspec.yaml, .../design.md, .../proposal.md, .../specs/database-orm/spec.md, .../tasks.md, openspec/specs/database-orm/spec.md
Design documentation, proposal, requirements spec, and implementation checklist describing the consolidation plan for centralizing database.EntDialectFor as the single source of dialect mapping and unifying ErrUnknownDialect across database and migrate packages.
Central API contract and documentation
pkg/database/client.go
Updated comments for ErrUnknownDialect and EntDialectFor clarify that both are centralized and shared with the migrate package for consistent dialect resolution and error handling.
Fresh-install dialect consolidation
pkg/database/migrate/fresh.go
freshInstall now calls ncpsdb.EntDialectFor() instead of a duplicate local entDialectFor() function; gooseStoreDialectFor() is removed and calls route through gooseDialectFor(). The entgo.io/ent/dialect import is also removed.
Error sentinel unification across migrate package
pkg/database/migrate/state.go, pkg/database/migrate/adopt.go, pkg/database/migrate/apply.go
The package-local ErrUnknownDialect sentinel is removed from state.go; all error wrapping in Detect, tableExists, columnExists, adopt, adoptTransactional, validateOptions, and gooseDialectFor now consistently wraps database.ErrUnknownDialect for callers to match via errors.Is.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • kalbasit/ncps#1188: Modifies the fresh-install migration flow and goose dialect selection in apply.go-style functions that are directly affected by this PR's dialect-mapper consolidation and ErrUnknownDialect unification.

Suggested labels

go, size:M, merge-stack

Poem

🐰 Dialect paths now converge,
One truth for all to merge.
Errors unified, code deduped clean,
The freshest migration you've seen!
✨ Hop hop, refactoring wins!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main refactoring objective: consolidating duplicate dialect mappers and the ErrUnknownDialect sentinel across the database package.
Description check ✅ Passed The description is directly related to the changeset, providing clear context about the duplication post-Ent-migration and explaining the consolidation approach, error handling unification, and goose mapper merging.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request consolidates the database dialect mapping logic and error sentinels across the pkg/database and pkg/database/migrate packages. It removes the duplicate entDialectFor and gooseStoreDialectFor functions in pkg/database/migrate/fresh.go, routing calls to the shared database.EntDialectFor and gooseDialectFor instead. It also eliminates the duplicate ErrUnknownDialect sentinel in the migrate package in favor of a single source of truth (database.ErrUnknownDialect). No review comments were provided, so I have no feedback to evaluate.

@kalbasit
kalbasit enabled auto-merge (squash) May 29, 2026 17:43
@codecov

codecov Bot commented May 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (0948dd3) to head (c62f7e3).

Additional details and impacted files
@@     Coverage Diff      @@
##   main   #1285   +/-   ##
============================
============================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalbasit
kalbasit merged commit 8c17ebb into main May 29, 2026
36 of 39 checks passed
@kalbasit
kalbasit deleted the refactor/consolidate-dialect-mappers branch May 29, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update go code merge-stack size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant