Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (c *Compiler) parseCatalog(schemas []string) error {
continue
}
contents := migrations.RemoveRollbackStatements(string(blob))
contents, err = migrations.TransformStatements(filepath.Dir(filename), contents)
if err != nil {
merr.Add(filename, contents, 0, err)
continue
}
c.schema = append(c.schema, contents)
stmts, err := c.parser.Parse(strings.NewReader(contents))
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions internal/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package migrations

import (
"bufio"
"errors"
"fmt"
"os"
"path"
"regexp"
"strings"
)

Expand Down Expand Up @@ -37,3 +42,38 @@ func IsDown(filename string) bool {
// Remove golang-migrate rollback files.
return strings.HasSuffix(filename, ".down.sql")
}

var ternTemplateRegex *regexp.Regexp

// tern: {{ template "filepath" . }}
func TransformStatements(pwd, content string) (string, error) {
if !strings.Contains(content, "{{ template \"") {
return content, nil
}

var err error
var processed string

if ternTemplateRegex == nil {
defer func() {
if r := recover(); r != nil {
fmt.Printf("failed to compile regexp: %v\n", r)
}
// It is tested, just recovering for it's technically possible to panic
}()
ternTemplateRegex = regexp.MustCompile(`\{\{ template \"(.+)\" \. \}\}`)
}

processed = ternTemplateRegex.ReplaceAllStringFunc(content, func(match string) string {
filePath := ternTemplateRegex.FindStringSubmatch(match)[1]
filePath = path.Join(pwd, filePath)
read, err := os.ReadFile(filePath)
if err != nil {
err = errors.Join(err, fmt.Errorf("error reading file %s: %w", filePath, err))
return match
}
return string(read)
})

return processed, err
}
Loading