Support not drop index when sync #2429
5
sync.go
5
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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user