Skip to content

feat: add templating support to go-migrate - #1441

Open
sbordeyne wants to merge 3 commits into
golang-migrate:masterfrom
sbordeyne:feat-add-templating-support
Open

feat: add templating support to go-migrate#1441
sbordeyne wants to merge 3 commits into
golang-migrate:masterfrom
sbordeyne:feat-add-templating-support

Conversation

@sbordeyne

Copy link
Copy Markdown

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 sprout library (successor to sprig, for maintenance purposes). All of the functions are added, meaning that the behavior of PR #793 can be easily replicated with either the expandEnv or the env template functions.

Also, compared to PR #793 I chose to pass down the isTemplate argument down to the NewMigration function to avoid changing the caller code (in migrate.go), thus templating support is available for all commands that read migration files (goto, up, down etc).

Copilot AI lite review requested due to automatic review settings September 13, 2026 11:08
@sbordeyne
sbordeyne force-pushed the feat-add-templating-support branch from b0495af to a4a76b8 Compare September 13, 2026 11:11

Copilot AI 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.

🟡 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 NewMigration when templating is enabled (Sprout function map cached once).
  • Add EnableTemplating to Migrate and a -template CLI flag to toggle it.
  • Add Sprout dependencies and update examples/tests to use the new NewMigration signature.
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 thread migration.go
Comment thread migrate.go
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
Comment thread internal/cli/main.go Outdated
Comment thread migration.go
@coveralls

coveralls commented Sep 13, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 54.929%. first build — sbordeyne:feat-add-templating-support into golang-migrate:master

sbordeyne and others added 2 commits September 13, 2026 18:32
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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