@@ -136,7 +136,7 @@ func vectoredInsert(ctx context.Context) error {
136136 return nil
137137}
138138
139- func TestOplogDumpCollModPrepareUnique (t * testing.T ) {
139+ func TestOplogDumpCollModIndexUniqueness (t * testing.T ) {
140140 testtype .SkipUnlessTestType (t , testtype .IntegrationTestType )
141141 // Oplog is not available in a standalone topology.
142142 testtype .SkipUnlessTestType (t , testtype .ReplSetTestType )
@@ -157,6 +157,9 @@ func TestOplogDumpCollModPrepareUnique(t *testing.T) {
157157
158158 testCollName := testCollectionNames [0 ]
159159
160+ err = session .Database (testDB ).Collection (testCollName ).Drop (ctx )
161+ require .NoError (t , err )
162+
160163 err = session .Database (testDB ).CreateCollection (ctx , testCollName )
161164 require .NoError (t , err )
162165 //nolint:errcheck
@@ -167,7 +170,7 @@ func TestOplogDumpCollModPrepareUnique(t *testing.T) {
167170
168171 md .ToolOptions .DB = ""
169172 md .OutputOptions .Oplog = true
170- md .OutputOptions .Out = "collMod_prepareUnique "
173+ md .OutputOptions .Out = "collMod_indexUniqueness "
171174
172175 require .NoError (t , md .Init ())
173176
@@ -176,7 +179,7 @@ func TestOplogDumpCollModPrepareUnique(t *testing.T) {
176179 defer failpoint .Reset ()
177180
178181 go func () {
179- require .NoError (t , createIndexesAndRunCollModPrepareUnique (ctx ))
182+ require .NoError (t , createIndexesAndCollModIndexUniqueness (ctx ))
180183 }()
181184
182185 //nolint:errcheck
@@ -187,7 +190,7 @@ func TestOplogDumpCollModPrepareUnique(t *testing.T) {
187190 path , err := os .Getwd ()
188191 require .NoError (t , err )
189192
190- dumpDir := util .ToUniversalPath (filepath .Join (path , "collMod_prepareUnique " ))
193+ dumpDir := util .ToUniversalPath (filepath .Join (path , "collMod_indexUniqueness " ))
191194 dumpDBDir := util .ToUniversalPath (filepath .Join (dumpDir , testDB ))
192195 oplogFilePath := util .ToUniversalPath (filepath .Join (dumpDir , "oplog.bson" ))
193196 require .True (t , fileDirExists (dumpDir ))
@@ -203,28 +206,33 @@ func TestOplogDumpCollModPrepareUnique(t *testing.T) {
203206 bsonSrc := db .NewDecodedBSONSource (db .NewBufferlessBSONSource (oplogFile ))
204207 prepareUniqueTrueCount := 0
205208 prepareUniqueFalseCount := 0
209+ forceNonUniqueCount := 0
206210
207211 var oplog db.Oplog
208212 for bsonSrc .Next (& oplog ) {
209213 require .NoError (t , bsonSrc .Err ())
210214
211215 if oplog .Namespace == testDB + ".$cmd" {
212216 indexDoc , ok := bsonutil .ToMap (oplog .Object )["index" ].(bson.D )
217+ indexDocMap := bsonutil .ToMap (indexDoc )
213218 if ok {
214- if bsonutil . ToMap ( indexDoc ) ["prepareUnique" ] == true {
219+ if indexDocMap ["prepareUnique" ] == true {
215220 prepareUniqueTrueCount ++
216- } else {
221+ } else if indexDocMap [ "prepareUnique" ] == false {
217222 prepareUniqueFalseCount ++
223+ } else if indexDocMap ["forceNonUnique" ] == true {
224+ forceNonUniqueCount ++
218225 }
219226 }
220227 }
221228 }
222229 require .NoError (t , oplogFile .Close ())
223230 require .Equal (t , 8 , prepareUniqueTrueCount )
224231 require .Equal (t , 4 , prepareUniqueFalseCount )
232+ require .Equal (t , 4 , forceNonUniqueCount )
225233}
226234
227- func createIndexesAndRunCollModPrepareUnique (ctx context.Context ) error {
235+ func createIndexesAndCollModIndexUniqueness (ctx context.Context ) error {
228236 client , err := testutil .GetBareSession ()
229237 if err != nil {
230238 return err
@@ -274,6 +282,22 @@ func createIndexesAndRunCollModPrepareUnique(ctx context.Context) error {
274282 return res .Err ()
275283 }
276284 }
285+
286+ for _ , option := range []string {"unique" , "forceNonUnique" } {
287+ res := client .Database (testDB ).RunCommand (
288+ ctx ,
289+ bson.D {
290+ {"collMod" , testCollName },
291+ {"index" , bson.D {
292+ {"keyPattern" , index .Keys },
293+ {option , true },
294+ }},
295+ },
296+ )
297+ if res .Err () != nil {
298+ return res .Err ()
299+ }
300+ }
277301 }
278302
279303 return nil
0 commit comments