diff options
Diffstat (limited to 'vendor/github.com/uptrace/bun/migrate')
5 files changed, 62 insertions, 35 deletions
diff --git a/vendor/github.com/uptrace/bun/migrate/auto.go b/vendor/github.com/uptrace/bun/migrate/auto.go index e56fa23a0..16804cd99 100644 --- a/vendor/github.com/uptrace/bun/migrate/auto.go +++ b/vendor/github.com/uptrace/bun/migrate/auto.go @@ -196,6 +196,9 @@ func (am *AutoMigrator) plan(ctx context.Context) (*changeset, error) { func (am *AutoMigrator) Migrate(ctx context.Context, opts ...MigrationOption) (*MigrationGroup, error) { migrations, _, err := am.createSQLMigrations(ctx, false) if err != nil { + if err == errNothingToMigrate { + return new(MigrationGroup), nil + } return nil, fmt.Errorf("auto migrate: %w", err) } @@ -214,23 +217,37 @@ func (am *AutoMigrator) Migrate(ctx context.Context, opts ...MigrationOption) (* // CreateSQLMigration writes required changes to a new migration file. // Use migrate.Migrator to apply the generated migrations. func (am *AutoMigrator) CreateSQLMigrations(ctx context.Context) ([]*MigrationFile, error) { - _, files, err := am.createSQLMigrations(ctx, true) + _, files, err := am.createSQLMigrations(ctx, false) + if err == errNothingToMigrate { + return files, nil + } return files, err } // CreateTxSQLMigration writes required changes to a new migration file making sure they will be executed // in a transaction when applied. Use migrate.Migrator to apply the generated migrations. func (am *AutoMigrator) CreateTxSQLMigrations(ctx context.Context) ([]*MigrationFile, error) { - _, files, err := am.createSQLMigrations(ctx, false) + _, files, err := am.createSQLMigrations(ctx, true) + if err == errNothingToMigrate { + return files, nil + } return files, err } +// errNothingToMigrate is a sentinel error which means the database is already in a desired state. +// Should not be returned to the user -- return a nil-error instead. +var errNothingToMigrate = errors.New("nothing to migrate") + func (am *AutoMigrator) createSQLMigrations(ctx context.Context, transactional bool) (*Migrations, []*MigrationFile, error) { changes, err := am.plan(ctx) if err != nil { return nil, nil, fmt.Errorf("create sql migrations: %w", err) } + if changes.Len() == 0 { + return nil, nil, errNothingToMigrate + } + name, _ := genMigrationName(am.schemaName + "_auto") migrations := NewMigrations(am.migrationsOpts...) migrations.Add(Migration{ @@ -282,6 +299,10 @@ func (am *AutoMigrator) createSQL(_ context.Context, migrations *Migrations, fna return mf, nil } +func (c *changeset) Len() int { + return len(c.operations) +} + // Func creates a MigrationFunc that applies all operations all the changeset. func (c *changeset) Func(m sqlschema.Migrator) MigrationFunc { return func(ctx context.Context, db *bun.DB) error { diff --git a/vendor/github.com/uptrace/bun/migrate/diff.go b/vendor/github.com/uptrace/bun/migrate/diff.go index 42e55dcde..e05d54b7d 100644 --- a/vendor/github.com/uptrace/bun/migrate/diff.go +++ b/vendor/github.com/uptrace/bun/migrate/diff.go @@ -26,20 +26,21 @@ func (d *detector) detectChanges() *changeset { targetTables := d.target.GetTables() RenameCreate: - for wantName, wantTable := range targetTables.FromOldest() { - + for _, wantPair := range targetTables.Pairs() { + wantName, wantTable := wantPair.Key, wantPair.Value // A table with this name exists in the database. We assume that schema objects won't // be renamed to an already existing name, nor do we support such cases. // Simply check if the table definition has changed. - if haveTable, ok := currentTables.Get(wantName); ok { + if haveTable, ok := currentTables.Load(wantName); ok { d.detectColumnChanges(haveTable, wantTable, true) d.detectConstraintChanges(haveTable, wantTable) continue } // Find all renamed tables. We assume that renamed tables have the same signature. - for haveName, haveTable := range currentTables.FromOldest() { - if _, exists := targetTables.Get(haveName); !exists && d.canRename(haveTable, wantTable) { + for _, havePair := range currentTables.Pairs() { + haveName, haveTable := havePair.Key, havePair.Value + if _, exists := targetTables.Load(haveName); !exists && d.canRename(haveTable, wantTable) { d.changes.Add(&RenameTableOp{ TableName: haveTable.GetName(), NewName: wantName, @@ -65,8 +66,9 @@ RenameCreate: } // Drop any remaining "current" tables which do not have a model. - for name, table := range currentTables.FromOldest() { - if _, keep := targetTables.Get(name); !keep { + for _, tPair := range currentTables.Pairs() { + name, table := tPair.Key, tPair.Value + if _, keep := targetTables.Load(name); !keep { d.changes.Add(&DropTableOp{ TableName: table.GetName(), }) @@ -103,12 +105,13 @@ func (d *detector) detectColumnChanges(current, target sqlschema.Table, checkTyp targetColumns := target.GetColumns() ChangeRename: - for tName, tCol := range targetColumns.FromOldest() { + for _, tPair := range targetColumns.Pairs() { + tName, tCol := tPair.Key, tPair.Value // This column exists in the database, so it hasn't been renamed, dropped, or added. // Still, we should not delete(columns, thisColumn), because later we will need to // check that we do not try to rename a column to an already a name that already exists. - if cCol, ok := currentColumns.Get(tName); ok { + if cCol, ok := currentColumns.Load(tName); ok { if checkType && !d.equalColumns(cCol, tCol) { d.changes.Add(&ChangeColumnTypeOp{ TableName: target.GetName(), @@ -122,9 +125,10 @@ ChangeRename: // Column tName does not exist in the database -- it's been either renamed or added. // Find renamed columns first. - for cName, cCol := range currentColumns.FromOldest() { + for _, cPair := range currentColumns.Pairs() { + cName, cCol := cPair.Key, cPair.Value // Cannot rename if a column with this name already exists or the types differ. - if _, exists := targetColumns.Get(cName); exists || !d.equalColumns(tCol, cCol) { + if _, exists := targetColumns.Load(cName); exists || !d.equalColumns(tCol, cCol) { continue } d.changes.Add(&RenameColumnOp{ @@ -149,8 +153,9 @@ ChangeRename: } // Drop columns which do not exist in the target schema and were not renamed. - for cName, cCol := range currentColumns.FromOldest() { - if _, keep := targetColumns.Get(cName); !keep { + for _, cPair := range currentColumns.Pairs() { + cName, cCol := cPair.Key, cPair.Value + if _, keep := targetColumns.Load(cName); !keep { d.changes.Add(&DropColumnOp{ TableName: target.GetName(), ColumnName: cName, @@ -325,7 +330,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().FromOldest() { + for _, icol := range t.GetColumns().Values() { 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. diff --git a/vendor/github.com/uptrace/bun/migrate/sqlschema/database.go b/vendor/github.com/uptrace/bun/migrate/sqlschema/database.go index cdc5b2d50..eb7476c54 100644 --- a/vendor/github.com/uptrace/bun/migrate/sqlschema/database.go +++ b/vendor/github.com/uptrace/bun/migrate/sqlschema/database.go @@ -4,12 +4,12 @@ import ( "slices" "strings" + "github.com/uptrace/bun/internal/ordered" "github.com/uptrace/bun/schema" - orderedmap "github.com/wk8/go-ordered-map/v2" ) type Database interface { - GetTables() *orderedmap.OrderedMap[string, Table] + GetTables() *ordered.Map[string, Table] GetForeignKeys() map[ForeignKey]string } @@ -20,11 +20,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 *orderedmap.OrderedMap[string, Table] + Tables *ordered.Map[string, Table] ForeignKeys map[ForeignKey]string } -func (ds BaseDatabase) GetTables() *orderedmap.OrderedMap[string, Table] { +func (ds BaseDatabase) GetTables() *ordered.Map[string, Table] { return ds.Tables } diff --git a/vendor/github.com/uptrace/bun/migrate/sqlschema/inspector.go b/vendor/github.com/uptrace/bun/migrate/sqlschema/inspector.go index fc9af06fc..19d1dc469 100644 --- a/vendor/github.com/uptrace/bun/migrate/sqlschema/inspector.go +++ b/vendor/github.com/uptrace/bun/migrate/sqlschema/inspector.go @@ -7,8 +7,8 @@ import ( "strings" "github.com/uptrace/bun" + "github.com/uptrace/bun/internal/ordered" "github.com/uptrace/bun/schema" - orderedmap "github.com/wk8/go-ordered-map/v2" ) type InspectorDialect interface { @@ -102,25 +102,25 @@ func (bmi *BunModelInspector) Inspect(ctx context.Context) (Database, error) { BaseDatabase: BaseDatabase{ ForeignKeys: make(map[ForeignKey]string), }, - Tables: orderedmap.New[string, Table](), + Tables: ordered.NewMap[string, Table](), } for _, t := range bmi.tables.All() { if t.Schema != bmi.SchemaName { continue } - columns := orderedmap.New[string, Column]() + columns := ordered.NewMap[string, 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.Set(f.Name, &BaseColumn{ + columns.Store(f.Name, &BaseColumn{ Name: f.Name, SQLType: strings.ToLower(sqlType), // TODO(dyma): maybe this is not necessary after Column.Eq() VarcharLen: length, - DefaultValue: exprToLower(f.SQLDefault), + DefaultValue: exprOrLiteral(f.SQLDefault), IsNullable: !f.NotNull, IsAutoIncrement: f.AutoIncrement, IsIdentity: f.Identity, @@ -162,7 +162,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.Set(tableName, &BunTable{ + state.Tables.Store(tableName, &BunTable{ BaseTable: BaseTable{ Schema: t.Schema, Name: tableName, @@ -211,12 +211,13 @@ func parseLen(typ string) (string, int, error) { return typ[:paren], length, nil } -// exprToLower converts string to lowercase, if it does not contain a string literal 'lit'. +// exprOrLiteral converts string to lowercase, if it does not contain a string literal 'lit' +// 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 exprToLower(s string) string { +func exprOrLiteral(s string) string { if strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'") { - return s + return strings.Trim(s, "'") } return strings.ToLower(s) } @@ -225,10 +226,10 @@ func exprToLower(s string) string { type BunModelSchema struct { BaseDatabase - Tables *orderedmap.OrderedMap[string, Table] + Tables *ordered.Map[string, Table] } -func (ms BunModelSchema) GetTables() *orderedmap.OrderedMap[string, Table] { +func (ms BunModelSchema) GetTables() *ordered.Map[string, Table] { return ms.Tables } diff --git a/vendor/github.com/uptrace/bun/migrate/sqlschema/table.go b/vendor/github.com/uptrace/bun/migrate/sqlschema/table.go index a805ba780..ec9b77f69 100644 --- a/vendor/github.com/uptrace/bun/migrate/sqlschema/table.go +++ b/vendor/github.com/uptrace/bun/migrate/sqlschema/table.go @@ -1,13 +1,13 @@ package sqlschema import ( - orderedmap "github.com/wk8/go-ordered-map/v2" + "github.com/uptrace/bun/internal/ordered" ) type Table interface { GetSchema() string GetName() string - GetColumns() *orderedmap.OrderedMap[string, Column] + GetColumns() *ordered.Map[string, Column] GetPrimaryKey() *PrimaryKey GetUniqueConstraints() []Unique } @@ -23,7 +23,7 @@ type BaseTable struct { Name string // ColumnDefinitions map each column name to the column definition. - Columns *orderedmap.OrderedMap[string, Column] + Columns *ordered.Map[string, Column] // PrimaryKey holds the primary key definition. // A nil value means that no primary key is defined for the table. @@ -47,7 +47,7 @@ func (td *BaseTable) GetName() string { return td.Name } -func (td *BaseTable) GetColumns() *orderedmap.OrderedMap[string, Column] { +func (td *BaseTable) GetColumns() *ordered.Map[string, Column] { return td.Columns } |