@@ -186,20 +186,45 @@ class MongoSchemaCollection {
186186 . then ( ( ) => mongoSchemaToParseSchema ( schema ) )
187187 . catch ( error => {
188188 if ( error . code === 11000 ) {
189- //Mongo's duplicate key error
190- throw new Parse . Error ( Parse . Error . DUPLICATE_VALUE , 'Class already exists.' ) ;
189+ // Duplicate key error - the schema was likely created by a concurrent operation
190+ // Fetch and return the existing schema instead of throwing an error
191+ return this . _collection
192+ . findOne ( { _id : schema . _id } )
193+ . then ( existingSchema => {
194+ if ( existingSchema ) {
195+ return mongoSchemaToParseSchema ( existingSchema ) ;
196+ }
197+ // If we can't find it, throw the original duplicate error
198+ throw new Parse . Error ( Parse . Error . DUPLICATE_VALUE , 'Class already exists.' ) ;
199+ } ) ;
191200 } else {
192201 throw error ;
193202 }
194203 } ) ;
195204 }
196205
197206 updateSchema ( name : string , update ) {
198- return this . _collection . updateOne ( _mongoSchemaQueryFromNameQuery ( name ) , update ) ;
207+ return this . _collection . updateOne ( _mongoSchemaQueryFromNameQuery ( name ) , update ) . catch ( error => {
208+ // Handle duplicate key errors that can occur during concurrent schema updates
209+ if ( error . code === 11000 ) {
210+ // Schema already exists/updated by another concurrent operation - safe to ignore
211+ return ;
212+ }
213+ throw error ;
214+ } ) ;
199215 }
200216
201217 upsertSchema ( name : string , query : string , update ) {
202- return this . _collection . upsertOne ( _mongoSchemaQueryFromNameQuery ( name , query ) , update ) ;
218+ return this . _collection
219+ . upsertOne ( _mongoSchemaQueryFromNameQuery ( name , query ) , update )
220+ . catch ( error => {
221+ // Handle duplicate key errors that can occur during concurrent schema upserts
222+ if ( error . code === 11000 ) {
223+ // Schema already exists - safe to ignore
224+ return ;
225+ }
226+ throw error ;
227+ } ) ;
203228 }
204229
205230 // Add a field to the schema. If database does not support the field
0 commit comments