Description
What happened:
Getting the error:
{"error":"a collection 'litmus.chaosExperiments' already exists","file":"/gql-server/pkg/database/mongodb/init.go:154","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb.(*MongoClient).initAllCollection","level":"error","msg":"failed to create chaosExperiments collection","time":"2025-05-09T05:48:46Z"}
{"error":"a collection 'litmus.chaosExperimentRuns' already exists","file":"/gql-server/pkg/database/mongodb/init.go:178","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb.(*MongoClient).initAllCollection","level":"error","msg":"failed to create chaosExperimentRuns collection","time":"2025-05-09T05:48:46Z"}
{"error":"a collection 'litmus.chaosHubs' already exists","file":"/gql-server/pkg/database/mongodb/init.go:196","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb.(*MongoClient).initAllCollection","level":"error","msg":"failed to create chaosHubs collection","time":"2025-05-09T05:48:46Z"}
{"error":"a collection 'litmus.chaosProbes' already exists","file":"/gql-server/pkg/database/mongodb/init.go:275","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb.(*MongoClient).initAllCollection","level":"error","msg":"failed to create chaosProbes collection","time":"2025-05-09T05:48:46Z"}
What you expected to happen:
It should start the server without having this issue
Where can this issue be corrected? (optional)
After seeing the code, I found that it should be checked whether the Collection already exists or not, , in the below parts of the code:
litmus/chaoscenter/graphql/server/pkg/database/mongodb/init.go
Lines 127 to 131 in 4f0749c
litmus/chaoscenter/graphql/server/pkg/database/mongodb/init.go
Lines 152 to 156 in 4f0749c
litmus/chaoscenter/graphql/server/pkg/database/mongodb/init.go
Lines 176 to 180 in 4f0749c
litmus/chaoscenter/graphql/server/pkg/database/mongodb/init.go
Lines 194 to 198 in 4f0749c
How to reproduce it (as minimally and precisely as possible):
Start the graphql server, configuring it with a MongoDB server with retryWrites false
Anything else we need to know?:
As in the authentication server, while initiating the mongodb service, there is a check for existence of Collection, if its not present then only its creating the collection, can that be possible here also.
Reference:
litmus/chaoscenter/authentication/pkg/utils/mongo_database.go
Lines 65 to 81 in 4f0749c