summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/migrate/diff.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/uptrace/bun/migrate/diff.go')
-rw-r--r--vendor/github.com/uptrace/bun/migrate/diff.go31
1 files changed, 20 insertions, 11 deletions
diff --git a/vendor/github.com/uptrace/bun/migrate/diff.go b/vendor/github.com/uptrace/bun/migrate/diff.go
index e05d54b7d..d12c11cb5 100644
--- a/vendor/github.com/uptrace/bun/migrate/diff.go
+++ b/vendor/github.com/uptrace/bun/migrate/diff.go
@@ -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.