summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/migrate
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-11-28 11:19:39 +0100
committerLibravatar GitHub <noreply@github.com>2022-11-28 11:19:39 +0100
commitdaf44ac2b709922512cfee3cde686b84b4868775 (patch)
tree2967df466f8d4515d32c05cf520ba3e7a19d4fae /vendor/github.com/uptrace/bun/migrate
parent[chore]: Bump codeberg.org/gruf/go-store/v2 from 2.0.9 to 2.0.10 (#1160) (diff)
downloadgotosocial-daf44ac2b709922512cfee3cde686b84b4868775.tar.xz
[chore] Bump database dependencies (#1164)
github.com/uptrace/bun v1.1.8 -> v1.1.9 github.com/uptrace/bun/pgdialect v1.1.8 -> v1.1.9 github.com/uptrace/bun/sqlitedialect v1.1.8 -> v1.1.9 modernc.org/sqlite v1.18.2 -> v1.19.5
Diffstat (limited to 'vendor/github.com/uptrace/bun/migrate')
-rw-r--r--vendor/github.com/uptrace/bun/migrate/migration.go122
-rw-r--r--vendor/github.com/uptrace/bun/migrate/migrator.go12
2 files changed, 64 insertions, 70 deletions
diff --git a/vendor/github.com/uptrace/bun/migrate/migration.go b/vendor/github.com/uptrace/bun/migrate/migration.go
index a581098a3..6f395b7b4 100644
--- a/vendor/github.com/uptrace/bun/migrate/migration.go
+++ b/vendor/github.com/uptrace/bun/migrate/migration.go
@@ -5,6 +5,7 @@ import (
"bytes"
"context"
"fmt"
+ "io"
"io/fs"
"sort"
"strings"
@@ -38,83 +39,86 @@ 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 {
- isTx := strings.HasSuffix(name, ".tx.up.sql") || strings.HasSuffix(name, ".tx.down.sql")
-
f, err := fsys.Open(name)
if err != nil {
return err
}
- scanner := bufio.NewScanner(f)
- var queries []string
-
- var query []byte
- for scanner.Scan() {
- b := scanner.Bytes()
-
- const prefix = "--bun:"
- if bytes.HasPrefix(b, []byte(prefix)) {
- b = b[len(prefix):]
- if bytes.Equal(b, []byte("split")) {
- queries = append(queries, string(query))
- query = query[:0]
- continue
- }
- return fmt.Errorf("bun: unknown directive: %q", b)
- }
+ isTx := strings.HasSuffix(name, ".tx.up.sql") || strings.HasSuffix(name, ".tx.down.sql")
+ return Exec(ctx, db, f, isTx)
+ }
+}
- query = append(query, b...)
- query = append(query, '\n')
+// 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)
+ var queries []string
+
+ var query []byte
+ for scanner.Scan() {
+ b := scanner.Bytes()
+
+ const prefix = "--bun:"
+ if bytes.HasPrefix(b, []byte(prefix)) {
+ b = b[len(prefix):]
+ if bytes.Equal(b, []byte("split")) {
+ queries = append(queries, string(query))
+ query = query[:0]
+ continue
+ }
+ return fmt.Errorf("bun: unknown directive: %q", b)
}
- if len(query) > 0 {
- queries = append(queries, string(query))
+ query = append(query, b...)
+ query = append(query, '\n')
+ }
+
+ if len(query) > 0 {
+ queries = append(queries, string(query))
+ }
+ if err := scanner.Err(); err != nil {
+ return err
+ }
+
+ var idb bun.IConn
+
+ if isTx {
+ tx, err := db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
}
- if err := scanner.Err(); err != nil {
+ idb = tx
+ } else {
+ conn, err := db.Conn(ctx)
+ if err != nil {
return err
}
+ idb = conn
+ }
- var idb bun.IConn
+ var retErr error
- if isTx {
- tx, err := db.BeginTx(ctx, nil)
- if err != nil {
- return err
- }
- idb = tx
- } else {
- conn, err := db.Conn(ctx)
- if err != nil {
- return err
- }
- idb = conn
+ defer func() {
+ if tx, ok := idb.(bun.Tx); ok {
+ retErr = tx.Commit()
+ return
}
- var retErr error
-
- defer func() {
- if tx, ok := idb.(bun.Tx); ok {
- retErr = tx.Commit()
- return
- }
-
- if conn, ok := idb.(bun.Conn); ok {
- retErr = conn.Close()
- return
- }
+ if conn, ok := idb.(bun.Conn); ok {
+ retErr = conn.Close()
+ return
+ }
- panic("not reached")
- }()
+ panic("not reached")
+ }()
- for _, q := range queries {
- _, err = idb.ExecContext(ctx, q)
- if err != nil {
- return err
- }
+ for _, q := range queries {
+ if _, err := idb.ExecContext(ctx, q); err != nil {
+ return err
}
-
- return retErr
}
+
+ return retErr
}
const goTemplate = `package %s
@@ -167,7 +171,7 @@ func (ms MigrationSlice) String() string {
if i > 0 {
sb.WriteString(", ")
}
- sb.WriteString(ms[i].Name)
+ sb.WriteString(ms[i].String())
}
return sb.String()
diff --git a/vendor/github.com/uptrace/bun/migrate/migrator.go b/vendor/github.com/uptrace/bun/migrate/migrator.go
index 98d8f434e..e24dba808 100644
--- a/vendor/github.com/uptrace/bun/migrate/migrator.go
+++ b/vendor/github.com/uptrace/bun/migrate/migrator.go
@@ -136,11 +136,6 @@ func (m *Migrator) Migrate(ctx context.Context, opts ...MigrationOption) (*Migra
return nil, err
}
- if err := m.Lock(ctx); err != nil {
- return nil, err
- }
- defer m.Unlock(ctx) //nolint:errcheck
-
migrations, lastGroupID, err := m.migrationsWithStatus(ctx)
if err != nil {
return nil, err
@@ -188,11 +183,6 @@ func (m *Migrator) Rollback(ctx context.Context, opts ...MigrationOption) (*Migr
return nil, err
}
- if err := m.Lock(ctx); err != nil {
- return nil, err
- }
- defer m.Unlock(ctx) //nolint:errcheck
-
migrations, err := m.MigrationsWithStatus(ctx)
if err != nil {
return nil, err
@@ -380,7 +370,7 @@ func (m *Migrator) formattedTableName(db *bun.DB) string {
func (m *Migrator) validate() error {
if len(m.ms) == 0 {
- return errors.New("migrate: there are no any migrations")
+ return errors.New("migrate: there are no migrations")
}
return nil
}