feat: add templating support to go-migrate - #1441
Open
sbordeyne wants to merge 3 commits into
Open
Conversation
sbordeyne
force-pushed
the
feat-add-templating-support
branch
from
September 13, 2026 11:11
b0495af to
a4a76b8
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
It introduces a breaking change to the exported NewMigration API and the new -template help text is currently inaccurate for how templates are executed.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR adds optional Go text/template rendering for migration bodies, backed by the Sprout (sprig successor) function library, and wires it through the CLI so all commands that read migrations can opt into templating.
Changes:
- Add template rendering to
NewMigrationwhen templating is enabled (Sprout function map cached once). - Add
EnableTemplatingtoMigrateand a-templateCLI flag to toggle it. - Add Sprout dependencies and update examples/tests to use the new
NewMigrationsignature.
File summaries
| File | Description |
|---|---|
| migration.go | Adds Sprout func map + conditional template rendering in NewMigration (and updates the constructor signature). |
| migration_test.go | Updates examples for new NewMigration signature and adds an example covering templating. |
| migrate.go | Adds EnableTemplating field and passes it into NewMigration when reading up/down migrations. |
| migrate_test.go | Updates tests to match the new NewMigration signature. |
| internal/cli/main.go | Adds -template flag and help text; toggles EnableTemplating on the migrater. |
| go.mod / go.sum | Introduces Sprout dependency and related indirect dependency updates. |
Review details
- Files reviewed: 6/7 changed files
- Comments generated: 4
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
851
to
855
| } else { | ||
| // create migration from up source | ||
| migr, err = NewMigration(r, identifier, version, targetVersion) | ||
| migr, err = NewMigration(r, identifier, version, targetVersion, m.EnableTemplating) | ||
| if err != nil { | ||
| return nil, err |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Already somewhat implemented by #793 but with a different approach:
Instead of reading the environment and adding all of the variables to the data argument when executing the template, this approach just adds the
sproutlibrary (successor tosprig, for maintenance purposes). All of the functions are added, meaning that the behavior of PR #793 can be easily replicated with either theexpandEnvor theenvtemplate functions.Also, compared to PR #793 I chose to pass down the
isTemplateargument down to theNewMigrationfunction to avoid changing the caller code (inmigrate.go), thus templating support is available for all commands that read migration files (goto, up, down etc).