Support not drop index when sync

This commit is contained in:
Lunny Xiao 2024-03-12 16:24:32 +08:00
parent 63222312b2
commit 870d07377d
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
1 changed files with 4 additions and 1 deletions

View File

@ -17,6 +17,8 @@ type SyncOptions struct {
IgnoreConstrains bool IgnoreConstrains bool
// IgnoreIndices will not add or delete indices // IgnoreIndices will not add or delete indices
IgnoreIndices bool IgnoreIndices bool
// IgnoreDropIndices will not delete indices
IgnoreDropIndices bool
} }
type SyncResult struct{} type SyncResult struct{}
@ -55,6 +57,7 @@ func (session *Session) Sync(beans ...interface{}) error {
WarnIfDatabaseColumnMissed: false, WarnIfDatabaseColumnMissed: false,
IgnoreConstrains: false, IgnoreConstrains: false,
IgnoreIndices: false, IgnoreIndices: false,
IgnoreDropIndices: false,
}, beans...) }, beans...)
return err return err
} }
@ -244,7 +247,7 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
for name2, index2 := range oriTable.Indexes { for name2, index2 := range oriTable.Indexes {
if _, ok := foundIndexNames[name2]; !ok { if _, ok := foundIndexNames[name2]; !ok {
// ignore based on there type // ignore based on there type
if (index2.Type == schemas.IndexType && opts.IgnoreIndices) || if (index2.Type == schemas.IndexType && (opts.IgnoreIndices || opts.IgnoreDropIndices)) ||
(index2.Type == schemas.UniqueType && opts.IgnoreConstrains) { (index2.Type == schemas.UniqueType && opts.IgnoreConstrains) {
// make sure we do not add a index with same name later // make sure we do not add a index with same name later
delete(addedNames, name2) delete(addedNames, name2)