[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>
This commit is contained in:
kim 2025-06-30 15:19:09 +02:00 committed by kim
commit 8b0ea56027
294 changed files with 139999 additions and 21873 deletions

View file

@ -17,17 +17,21 @@ import (
type AutoMigratorOption func(m *AutoMigrator)
// WithModel adds a bun.Model to the scope of migrations.
// WithModel adds a bun.Model to the migration scope.
func WithModel(models ...interface{}) AutoMigratorOption {
return func(m *AutoMigrator) {
m.includeModels = append(m.includeModels, models...)
}
}
// WithExcludeTable tells the AutoMigrator to ignore a table in the database.
// WithExcludeTable tells AutoMigrator to exclude database tables from the migration scope.
// This prevents AutoMigrator from dropping tables which may exist in the schema
// but which are not used by the application.
//
// Expressions may make use of the wildcards supported by the SQL LIKE operator:
// - % as a wildcard
// - _ as a single character
//
// Do not exclude tables included via WithModel, as BunModelInspector ignores this setting.
func WithExcludeTable(tables ...string) AutoMigratorOption {
return func(m *AutoMigrator) {
@ -35,7 +39,17 @@ func WithExcludeTable(tables ...string) AutoMigratorOption {
}
}
// WithSchemaName changes the default database schema to migrate objects in.
// WithExcludeForeignKeys tells AutoMigrator to exclude a foreign key constaint
// from the migration scope. This prevents AutoMigrator from dropping foreign keys
// that are defined manually via CreateTableQuery.ForeignKey().
func WithExcludeForeignKeys(fks ...sqlschema.ForeignKey) AutoMigratorOption {
return func(m *AutoMigrator) {
m.excludeForeignKeys = append(m.excludeForeignKeys, fks...)
}
}
// WithSchemaName sets the database schema to migrate objects in.
// By default, dialects' default schema is used.
func WithSchemaName(schemaName string) AutoMigratorOption {
return func(m *AutoMigrator) {
m.schemaName = schemaName
@ -82,7 +96,7 @@ func WithMigrationsDirectoryAuto(directory string) AutoMigratorOption {
// database schema automatically.
//
// Usage:
// 1. Generate migrations and apply them au once with AutoMigrator.Migrate().
// 1. Generate migrations and apply them at once with AutoMigrator.Migrate().
// 2. Create up- and down-SQL migration files and apply migrations using Migrator.Migrate().
//
// While both methods produce complete, reversible migrations (with entries in the database
@ -124,8 +138,8 @@ type AutoMigrator struct {
// includeModels define the migration scope.
includeModels []interface{}
// excludeTables are excluded from database inspection.
excludeTables []string
excludeTables []string // excludeTables are excluded from database inspection.
excludeForeignKeys []sqlschema.ForeignKey // excludeForeignKeys are excluded from database inspection.
// diffOpts are passed to detector constructor.
diffOpts []diffOption
@ -150,7 +164,11 @@ func NewAutoMigrator(db *bun.DB, opts ...AutoMigratorOption) (*AutoMigrator, err
}
am.excludeTables = append(am.excludeTables, am.table, am.locksTable)
dbInspector, err := sqlschema.NewInspector(db, sqlschema.WithSchemaName(am.schemaName), sqlschema.WithExcludeTables(am.excludeTables...))
dbInspector, err := sqlschema.NewInspector(db,
sqlschema.WithSchemaName(am.schemaName),
sqlschema.WithExcludeTables(am.excludeTables...),
sqlschema.WithExcludeForeignKeys(am.excludeForeignKeys...),
)
if err != nil {
return nil, err
}
@ -252,12 +270,12 @@ func (am *AutoMigrator) createSQLMigrations(ctx context.Context, transactional b
migrations := NewMigrations(am.migrationsOpts...)
migrations.Add(Migration{
Name: name,
Up: changes.Up(am.dbMigrator),
Down: changes.Down(am.dbMigrator),
Up: wrapMigrationFunc(changes.Up(am.dbMigrator)),
Down: wrapMigrationFunc(changes.Down(am.dbMigrator)),
Comment: "Changes detected by bun.AutoMigrator",
})
// Append .tx.up.sql or .up.sql to migration name, dependin if it should be transactional.
// Append .tx.up.sql or .up.sql to migration name, depending if it should be transactional.
fname := func(direction string) string {
return name + map[bool]string{true: ".tx.", false: "."}[transactional] + direction + ".sql"
}
@ -336,7 +354,7 @@ func (c *changeset) apply(ctx context.Context, db *bun.DB, m sqlschema.Migrator)
}
for _, op := range c.operations {
if _, isComment := op.(*comment); isComment {
if _, skip := op.(*Unimplemented); skip {
continue
}
@ -359,17 +377,22 @@ func (c *changeset) WriteTo(w io.Writer, m sqlschema.Migrator) error {
b := internal.MakeQueryBytes()
for _, op := range c.operations {
if c, isComment := op.(*comment); isComment {
if comment, isComment := op.(*Unimplemented); isComment {
b = append(b, "/*\n"...)
b = append(b, *c...)
b = append(b, *comment...)
b = append(b, "\n*/"...)
continue
}
b, err = m.AppendSQL(b, op)
// Append each query separately, merge later.
// Dialects assume that the []byte only holds
// the contents of a single query and may be misled.
queryBytes := internal.MakeQueryBytes()
queryBytes, err = m.AppendSQL(queryBytes, op)
if err != nil {
return fmt.Errorf("write changeset: %w", err)
}
b = append(b, queryBytes...)
b = append(b, ";\n"...)
}
if _, err := w.Write(b); err != nil {
@ -409,7 +432,7 @@ func (c *changeset) ResolveDependencies() error {
}
// visit iterates over c.operations until it finds all operations that depend on the current one
// or runs into cirtular dependency, in which case it will return an error.
// or runs into circular dependency, in which case it will return an error.
visit = func(op Operation) error {
switch status[op] {
case visited:

View file

@ -1,6 +1,7 @@
package migrate
import (
"github.com/uptrace/bun/internal/ordered"
"github.com/uptrace/bun/migrate/sqlschema"
)
@ -22,8 +23,8 @@ func diff(got, want sqlschema.Database, opts ...diffOption) *changeset {
}
func (d *detector) detectChanges() *changeset {
currentTables := d.current.GetTables()
targetTables := d.target.GetTables()
currentTables := toOrderedMap(d.current.GetTables())
targetTables := toOrderedMap(d.target.GetTables())
RenameCreate:
for _, wantPair := range targetTables.Pairs() {
@ -99,10 +100,19 @@ RenameCreate:
return &d.changes
}
// detechColumnChanges finds renamed columns and, if checkType == true, columns with changed type.
// toOrderedMap transforms a slice of objects to an ordered map, using return of GetName() as key.
func toOrderedMap[V interface{ GetName() string }](named []V) *ordered.Map[string, V] {
m := ordered.NewMap[string, V]()
for _, v := range named {
m.Store(v.GetName(), v)
}
return m
}
// detectColumnChanges finds renamed columns and, if checkType == true, columns with changed type.
func (d *detector) detectColumnChanges(current, target sqlschema.Table, checkType bool) {
currentColumns := current.GetColumns()
targetColumns := target.GetColumns()
currentColumns := toOrderedMap(current.GetColumns())
targetColumns := toOrderedMap(target.GetColumns())
ChangeRename:
for _, tPair := range targetColumns.Pairs() {
@ -265,7 +275,7 @@ type detector struct {
// cmpType determines column type equivalence.
// Default is direct comparison with '==' operator, which is inaccurate
// due to the existence of dialect-specific type aliases. The caller
// should pass a concrete InspectorDialect.EquuivalentType for robust comparison.
// should pass a concrete InspectorDialect.EquivalentType for robust comparison.
cmpType CompareTypeFunc
}
@ -283,7 +293,7 @@ func (d detector) equalColumns(col1, col2 sqlschema.Column) bool {
}
func (d detector) makeTargetColDef(current, target sqlschema.Column) sqlschema.Column {
// Avoid unneccessary type-change migrations if the types are equivalent.
// Avoid unnecessary type-change migrations if the types are equivalent.
if d.cmpType(current, target) {
target = &sqlschema.BaseColumn{
Name: target.GetName(),
@ -311,8 +321,7 @@ func equalSignatures(t1, t2 sqlschema.Table, eq CompareTypeFunc) bool {
// signature is a set of column definitions, which allows "relation/name-agnostic" comparison between them;
// meaning that two columns are considered equal if their types are the same.
type signature struct {
// underlying stores the number of occurences for each unique column type.
// underlying stores the number of occurrences for each unique column type.
// It helps to account for the fact that a table might have multiple columns that have the same type.
underlying map[sqlschema.BaseColumn]int
@ -330,7 +339,7 @@ func newSignature(t sqlschema.Table, eq CompareTypeFunc) signature {
// scan iterates over table's field and counts occurrences of each unique column definition.
func (s *signature) scan(t sqlschema.Table) {
for _, icol := range t.GetColumns().Values() {
for _, icol := range t.GetColumns() {
scanCol := icol.(*sqlschema.BaseColumn)
// This is slightly more expensive than if the columns could be compared directly
// and we always did s.underlying[col]++, but we get type-equivalence in return.
@ -368,7 +377,7 @@ func (s *signature) Equals(other signature) bool {
}
// refMap is a utility for tracking superficial changes in foreign keys,
// which do not require any modificiation in the database.
// which do not require any modification in the database.
// Modern SQL dialects automatically updated foreign key constraints whenever
// a column or a table is renamed. Detector can use refMap to ignore any
// differences in foreign keys which were caused by renamed column/table.

View file

@ -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

View file

@ -58,8 +58,8 @@ func (m *Migrations) Register(up, down MigrationFunc) error {
m.Add(Migration{
Name: name,
Comment: comment,
Up: up,
Down: down,
Up: wrapMigrationFunc(up),
Down: wrapMigrationFunc(down),
})
return nil

View file

@ -41,6 +41,12 @@ func WithMarkAppliedOnSuccess(enabled bool) MigratorOption {
}
}
func WithTemplateData(data any) MigratorOption {
return func(m *Migrator) {
m.templateData = data
}
}
type Migrator struct {
db *bun.DB
migrations *Migrations
@ -50,6 +56,8 @@ type Migrator struct {
table string
locksTable string
markAppliedOnSuccess bool
templateData any
}
func NewMigrator(db *bun.DB, migrations *Migrations, opts ...MigratorOption) *Migrator {
@ -168,7 +176,7 @@ func (m *Migrator) Migrate(ctx context.Context, opts ...MigrationOption) (*Migra
group.Migrations = migrations[:i+1]
if !cfg.nop && migration.Up != nil {
if err := migration.Up(ctx, m.db); err != nil {
if err := migration.Up(ctx, m.db, m.templateData); err != nil {
return group, err
}
}
@ -207,7 +215,7 @@ func (m *Migrator) Rollback(ctx context.Context, opts ...MigrationOption) (*Migr
}
if !cfg.nop && migration.Down != nil {
if err := migration.Down(ctx, m.db); err != nil {
if err := migration.Down(ctx, m.db, m.templateData); err != nil {
return lastGroup, err
}
}

View file

@ -17,7 +17,7 @@ import (
// about the applied change.
//
// Some operations might be irreversible due to technical limitations. Returning
// a *comment from GetReverse() will add an explanatory note to the generate migation file.
// a *comment from GetReverse() will add an explanatory note to the generate migration file.
//
// To declare dependency on another Operation, operations should implement
// { DependsOn(Operation) bool } interface, which Changeset will use to resolve dependencies.
@ -56,7 +56,7 @@ func (op *DropTableOp) DependsOn(another Operation) bool {
// GetReverse for a DropTable returns a no-op migration. Logically, CreateTable is the reverse,
// but DropTable does not have the table's definition to create one.
func (op *DropTableOp) GetReverse() Operation {
c := comment(fmt.Sprintf("WARNING: \"DROP TABLE %s\" cannot be reversed automatically because table definition is not available", op.TableName))
c := Unimplemented(fmt.Sprintf("WARNING: \"DROP TABLE %s\" cannot be reversed automatically because table definition is not available", op.TableName))
return &c
}
@ -224,7 +224,6 @@ func (op *AddUniqueConstraintOp) DependsOn(another Operation) bool {
default:
return false
}
}
// DropUniqueConstraintOp drops a UNIQUE constraint.
@ -326,15 +325,15 @@ func (op *ChangePrimaryKeyOp) GetReverse() Operation {
}
}
// comment denotes an Operation that cannot be executed.
// Unimplemented denotes an Operation that cannot be executed.
//
// Operations, which cannot be reversed due to current technical limitations,
// may return &comment with a helpful message from their GetReverse() method.
// may have their GetReverse() return &Unimplemented with a helpful message.
//
// Chnagelog should skip it when applying operations or output as log message,
// and write it as an SQL comment when creating migration files.
type comment string
// When applying operations, changelog should skip it or output as a log message,
// and write it as an SQL Unimplemented when creating migration files.
type Unimplemented string
var _ Operation = (*comment)(nil)
var _ Operation = (*Unimplemented)(nil)
func (c *comment) GetReverse() Operation { return c }
func (reason *Unimplemented) GetReverse() Operation { return reason }

View file

@ -4,12 +4,11 @@ import (
"slices"
"strings"
"github.com/uptrace/bun/internal/ordered"
"github.com/uptrace/bun/schema"
)
type Database interface {
GetTables() *ordered.Map[string, Table]
GetTables() []Table
GetForeignKeys() map[ForeignKey]string
}
@ -20,11 +19,11 @@ var _ Database = (*BaseDatabase)(nil)
// Dialects and only dialects can use it to implement the Database interface.
// Other packages must use the Database interface.
type BaseDatabase struct {
Tables *ordered.Map[string, Table]
Tables []Table
ForeignKeys map[ForeignKey]string
}
func (ds BaseDatabase) GetTables() *ordered.Map[string, Table] {
func (ds BaseDatabase) GetTables() []Table {
return ds.Tables
}

View file

@ -7,7 +7,6 @@ import (
"strings"
"github.com/uptrace/bun"
"github.com/uptrace/bun/internal/ordered"
"github.com/uptrace/bun/schema"
)
@ -30,12 +29,23 @@ type InspectorDialect interface {
// InspectorConfig controls the scope of migration by limiting the objects Inspector should return.
// Inspectors SHOULD use the configuration directly instead of copying it, or MAY choose to embed it,
// to make sure options are always applied correctly.
//
// ExcludeTables and ExcludeForeignKeys are intended for database inspectors,
// to compensate for the fact that model structs may not wholly reflect the
// state of the database schema.
// Database inspectors MUST respect these exclusions to prevent relations
// from being dropped unintentionally.
type InspectorConfig struct {
// SchemaName limits inspection to tables in a particular schema.
SchemaName string
// ExcludeTables from inspection.
// ExcludeTables from inspection. Patterns MAY make use of wildcards
// like % and _ and dialects MUST acknowledge that by using them
// with the SQL LIKE operator.
ExcludeTables []string
// ExcludeForeignKeys from inspection.
ExcludeForeignKeys map[ForeignKey]string
}
// Inspector reads schema state.
@ -49,13 +59,26 @@ func WithSchemaName(schemaName string) InspectorOption {
}
}
// WithExcludeTables works in append-only mode, i.e. tables cannot be re-included.
// WithExcludeTables forces inspector to exclude tables from the reported schema state.
// It works in append-only mode, i.e. tables cannot be re-included.
//
// Patterns MAY make use of % and _ wildcards, as if writing a LIKE clause in SQL.
func WithExcludeTables(tables ...string) InspectorOption {
return func(cfg *InspectorConfig) {
cfg.ExcludeTables = append(cfg.ExcludeTables, tables...)
}
}
// WithExcludeForeignKeys forces inspector to exclude foreign keys
// from the reported schema state.
func WithExcludeForeignKeys(fks ...ForeignKey) InspectorOption {
return func(cfg *InspectorConfig) {
for _, fk := range fks {
cfg.ExcludeForeignKeys[fk] = ""
}
}
}
// NewInspector creates a new database inspector, if the dialect supports it.
func NewInspector(db *bun.DB, options ...InspectorOption) (Inspector, error) {
dialect, ok := (db.Dialect()).(InspectorDialect)
@ -78,6 +101,9 @@ func NewBunModelInspector(tables *schema.Tables, options ...InspectorOption) *Bu
type InspectorOption func(*InspectorConfig)
func ApplyInspectorOptions(cfg *InspectorConfig, options ...InspectorOption) {
if cfg.ExcludeForeignKeys == nil {
cfg.ExcludeForeignKeys = make(map[ForeignKey]string)
}
for _, opt := range options {
opt(cfg)
}
@ -90,6 +116,10 @@ type inspector struct {
// BunModelInspector creates the current project state from the passed bun.Models.
// Do not recycle BunModelInspector for different sets of models, as older models will not be de-registerred before the next run.
//
// BunModelInspector does not know which the database's dialect, so it does not
// assume any default schema name. Always specify the target schema name via
// WithSchemaName option to receive meaningful results.
type BunModelInspector struct {
InspectorConfig
tables *schema.Tables
@ -102,21 +132,21 @@ func (bmi *BunModelInspector) Inspect(ctx context.Context) (Database, error) {
BaseDatabase: BaseDatabase{
ForeignKeys: make(map[ForeignKey]string),
},
Tables: ordered.NewMap[string, Table](),
}
for _, t := range bmi.tables.All() {
if t.Schema != bmi.SchemaName {
continue
}
columns := ordered.NewMap[string, Column]()
var columns []Column
for _, f := range t.Fields {
sqlType, length, err := parseLen(f.CreateTableSQLType)
if err != nil {
return nil, fmt.Errorf("parse length in %q: %w", f.CreateTableSQLType, err)
}
columns.Store(f.Name, &BaseColumn{
columns = append(columns, &BaseColumn{
Name: f.Name,
SQLType: strings.ToLower(sqlType), // TODO(dyma): maybe this is not necessary after Column.Eq()
VarcharLen: length,
@ -162,7 +192,7 @@ func (bmi *BunModelInspector) Inspect(ctx context.Context) (Database, error) {
// produces
// schema.Table{ Schema: "favourite", Name: "favourite.books" }
tableName := strings.TrimPrefix(t.Name, t.Schema+".")
state.Tables.Store(tableName, &BunTable{
state.Tables = append(state.Tables, &BunTable{
BaseTable: BaseTable{
Schema: t.Schema,
Name: tableName,
@ -212,7 +242,7 @@ func parseLen(typ string) (string, int, error) {
}
// exprOrLiteral converts string to lowercase, if it does not contain a string literal 'lit'
// and trims the surrounding '' otherwise.
// and trims the surrounding otherwise.
// Use it to ensure that user-defined default values in the models are always comparable
// to those returned by the database inspector, regardless of the case convention in individual drivers.
func exprOrLiteral(s string) string {
@ -226,10 +256,10 @@ func exprOrLiteral(s string) string {
type BunModelSchema struct {
BaseDatabase
Tables *ordered.Map[string, Table]
Tables []Table
}
func (ms BunModelSchema) GetTables() *ordered.Map[string, Table] {
func (ms BunModelSchema) GetTables() []Table {
return ms.Tables
}

View file

@ -1,13 +1,9 @@
package sqlschema
import (
"github.com/uptrace/bun/internal/ordered"
)
type Table interface {
GetSchema() string
GetName() string
GetColumns() *ordered.Map[string, Column]
GetColumns() []Column
GetPrimaryKey() *PrimaryKey
GetUniqueConstraints() []Unique
}
@ -23,7 +19,7 @@ type BaseTable struct {
Name string
// ColumnDefinitions map each column name to the column definition.
Columns *ordered.Map[string, Column]
Columns []Column
// PrimaryKey holds the primary key definition.
// A nil value means that no primary key is defined for the table.
@ -47,7 +43,7 @@ func (td *BaseTable) GetName() string {
return td.Name
}
func (td *BaseTable) GetColumns() *ordered.Map[string, Column] {
func (td *BaseTable) GetColumns() []Column {
return td.Columns
}