Add TemplateFS for templated file handling in migrations #4637
+344
−2
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.
PMM-0
Link to the Feature Build: SUBMODULES-0
If this PR adds or removes or alters one or more API endpoints, please review and add or update the relevant API documents as well:
If this PR is related to some other PRs in this or other repositories, please provide links to those PRs:
TemplateFS - Embedded Filesystem with Go Template Support
Overview
TemplateFS
is a customfs.FS
implementation that wraps anembed.FS
and applies Go text/template processing to file content during reads. This allows you to embed template files and dynamically substitute variables at runtime.Key Features
embed.FS
(fs.FS
,fs.ReadDirFS
,fs.ReadFileFS
)ReadFile()
Open()
returns original files without templatingfs.Sub()
,fs.Glob()
and other standard functionsUsage
Basic Example
Integration with golang-migrate
Template Files
Template files use standard Go text/template syntax:
API Reference
TemplateFS
Methods
NewTemplateFS(embedFS embed.FS, data map[string]any) *TemplateFS
Open(name string) (fs.File, error)
ReadFile(name string) ([]byte, error)
ReadDir(name string) ([]fs.DirEntry, error)
Template Data Usage
The same template data is used for all files in the filesystem. This makes it simple to have consistent variables across all your template files:
Data
map for template variable substitution{{.DatabaseName}}
are available in all template filesData
is nil, template processing is skipped and original content is returnedError Handling
This ensures that TemplateFS is backwards compatible with regular embedded files.
Example Usage in PMM
In the PMM project, TemplateFS is used in the QAN API for database migrations:
This allows migration files to use template variables like
{{.DatabaseName}}
for dynamic database naming while maintaining full compatibility with existing non-templated migrations.