summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/migrate/migration.go
diff options
context:
space:
mode:
authorLibravatar kim <grufwub@gmail.com>2025-06-30 15:19:09 +0200
committerLibravatar kim <gruf@noreply.codeberg.org>2025-06-30 15:19:09 +0200
commit8b0ea560279a5bf4479555d3924c763ddeecfcad (patch)
tree005e26d4a658e565594fb259cc17948659195822 /vendor/github.com/uptrace/bun/migrate/migration.go
parent[chore] bumps ncruces/go-sqlite3 v0.26.1 => v0.26.3 (#4302) (diff)
downloadgotosocial-8b0ea560279a5bf4479555d3924c763ddeecfcad.tar.xz
[chore] update go dependencies (#4304)
- github.com/KimMachineGun/automemlimit v0.7.2 => v0.7.3 - github.com/gin-contrib/cors v1.7.5 => v1.7.6 - github.com/minio/minio-go/v7 v7.0.92 => v7.0.94 - github.com/spf13/cast v1.8.0 => v1.9.2 - github.com/uptrace/bun{,/*} v1.2.11 => v1.2.14 - golang.org/x/image v0.27.0 => v0.28.0 - golang.org/x/net v0.40.0 => v0.41.0 - code.superseriousbusiness.org/go-swagger v0.31.0-gts-go1.23-fix => v0.32.3-gts-go1.23-fix Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4304 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/github.com/uptrace/bun/migrate/migration.go')
-rw-r--r--vendor/github.com/uptrace/bun/migrate/migration.go49
1 files changed, 42 insertions, 7 deletions
diff --git a/vendor/github.com/uptrace/bun/migrate/migration.go b/vendor/github.com/uptrace/bun/migrate/migration.go
index 3f4076d2b..4d60a5858 100644
--- a/vendor/github.com/uptrace/bun/migrate/migration.go
+++ b/vendor/github.com/uptrace/bun/migrate/migration.go
@@ -9,6 +9,7 @@ import (
"io/fs"
"sort"
"strings"
+ "text/template"
"time"
"github.com/uptrace/bun"
@@ -23,8 +24,8 @@ type Migration struct {
GroupID int64
MigratedAt time.Time `bun:",notnull,nullzero,default:current_timestamp"`
- Up MigrationFunc `bun:"-"`
- Down MigrationFunc `bun:"-"`
+ Up internalMigrationFunc `bun:"-"`
+ Down internalMigrationFunc `bun:"-"`
}
func (m Migration) String() string {
@@ -35,23 +36,57 @@ func (m Migration) IsApplied() bool {
return m.ID > 0
}
+type internalMigrationFunc func(ctx context.Context, db *bun.DB, templateData any) error
+
type MigrationFunc func(ctx context.Context, db *bun.DB) error
-func NewSQLMigrationFunc(fsys fs.FS, name string) MigrationFunc {
- return func(ctx context.Context, db *bun.DB) error {
+func NewSQLMigrationFunc(fsys fs.FS, name string) internalMigrationFunc {
+ return func(ctx context.Context, db *bun.DB, templateData any) error {
f, err := fsys.Open(name)
if err != nil {
return err
}
isTx := strings.HasSuffix(name, ".tx.up.sql") || strings.HasSuffix(name, ".tx.down.sql")
- return Exec(ctx, db, f, isTx)
+ return Exec(ctx, db, f, templateData, isTx)
+ }
+}
+
+func wrapMigrationFunc(fn MigrationFunc) internalMigrationFunc {
+ return func(ctx context.Context, db *bun.DB, templateData any) error {
+ return fn(ctx, db)
+ }
+}
+
+func renderTemplate(contents []byte, templateData any) (*bytes.Buffer, error) {
+ tmpl, err := template.New("migration").Parse(string(contents))
+ if err != nil {
+ return nil, fmt.Errorf("failed to parse template: %w", err)
+ }
+
+ var rendered bytes.Buffer
+ if err := tmpl.Execute(&rendered, templateData); err != nil {
+ return nil, fmt.Errorf("failed to execute template: %w", err)
}
+
+ return &rendered, nil
}
// Exec reads and executes the SQL migration in the f.
-func Exec(ctx context.Context, db *bun.DB, f io.Reader, isTx bool) error {
- scanner := bufio.NewScanner(f)
+func Exec(ctx context.Context, db *bun.DB, f io.Reader, templateData any, isTx bool) error {
+ contents, err := io.ReadAll(f)
+ if err != nil {
+ return err
+ }
+ var reader io.Reader = bytes.NewReader(contents)
+ if templateData != nil {
+ buf, err := renderTemplate(contents, templateData)
+ if err != nil {
+ return err
+ }
+ reader = buf
+ }
+ scanner := bufio.NewScanner(reader)
var queries []string
var query []byte