From 2e2a66c640d1fc46e0849111209eac7d31a314d1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 15 Mar 2024 03:13:09 +0000 Subject: [PATCH] Support not drop index when sync (#2429) Reviewed-on: https://gitea.com/xorm/xorm/pulls/2429 --- sync.go | 5 ++++- tests/schema_test.go | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/sync.go b/sync.go index adc2d859..f1b5e59f 100644 --- a/sync.go +++ b/sync.go @@ -17,6 +17,8 @@ type SyncOptions struct { IgnoreConstrains bool // IgnoreIndices will not add or delete indices IgnoreIndices bool + // IgnoreDropIndices will not delete indices + IgnoreDropIndices bool } type SyncResult struct{} @@ -55,6 +57,7 @@ func (session *Session) Sync(beans ...interface{}) error { WarnIfDatabaseColumnMissed: false, IgnoreConstrains: false, IgnoreIndices: false, + IgnoreDropIndices: false, }, beans...) return err } @@ -244,7 +247,7 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{}) for name2, index2 := range oriTable.Indexes { if _, ok := foundIndexNames[name2]; !ok { // 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) { // make sure we do not add a index with same name later delete(addedNames, name2) diff --git a/tests/schema_test.go b/tests/schema_test.go index db9f9e8f..3657950e 100644 --- a/tests/schema_test.go +++ b/tests/schema_test.go @@ -698,7 +698,7 @@ func TestSyncWithOptions(t *testing.T) { assert.NotNil(t, result) assert.Len(t, getIndicesOfBeanFromDB(t, &SyncWithOpts1{}), 0) - // only ignore indices + // only ignore constrains result, err = testEngine.SyncWithOptions(xorm.SyncOptions{IgnoreConstrains: true}, &SyncWithOpts2{}) assert.NoError(t, err) assert.NotNil(t, result) @@ -706,7 +706,7 @@ func TestSyncWithOptions(t *testing.T) { assert.Len(t, indices, 2) assert.ElementsMatch(t, []string{"ttt", "index"}, getKeysFromMap(indices)) - // only ignore constrains + // only ignore indices result, err = testEngine.SyncWithOptions(xorm.SyncOptions{IgnoreIndices: true}, &SyncWithOpts3{}) assert.NoError(t, err) assert.NotNil(t, result) @@ -714,9 +714,16 @@ func TestSyncWithOptions(t *testing.T) { assert.Len(t, indices, 4) assert.ElementsMatch(t, []string{"ttt", "index", "unique", "lll"}, getKeysFromMap(indices)) + // only ignore drop indices + result, err = testEngine.SyncWithOptions(xorm.SyncOptions{IgnoreDropIndices: true}, &SyncWithOpts3{}) + assert.NoError(t, err) + assert.NotNil(t, result) + indices = getIndicesOfBeanFromDB(t, &SyncWithOpts1{}) + assert.Len(t, indices, 4) + assert.ElementsMatch(t, []string{"ttt", "index", "unique", "lll"}, getKeysFromMap(indices)) + tableInfoFromStruct, _ := testEngine.TableInfo(&SyncWithOpts1{}) assert.ElementsMatch(t, getKeysFromMap(tableInfoFromStruct.Indexes), getKeysFromMap(getIndicesOfBeanFromDB(t, &SyncWithOpts1{}))) - } func getIndicesOfBeanFromDB(t *testing.T, bean interface{}) map[string]*schemas.Index {