DFSOrange commented on a change in pull request #855:
URL: 
https://github.com/apache/servicecomb-service-center/pull/855#discussion_r572668939



##########
File path: datasource/mongo/mongo.go
##########
@@ -100,64 +97,127 @@ func (ds *DataSource) initClient() error {
        }
 }
 
-//{Key: StringBuilder([]string{ColumnService, ColumnAlias}), Value: 
bsonx.Int32(1)}
-func (ds *DataSource) createIndexes() (err error) {
-       err = client.GetMongoClient().CreateIndexes(context.TODO(), 
CollectionService, []mongo.IndexModel{{
-               Keys:    bsonx.Doc{{Key: StringBuilder([]string{ColumnService, 
ColumnServiceID}), Value: bsonx.Int32(1)}},
-               Options: options.Index().SetUnique(true),
-       }, {
-               Keys: bsonx.Doc{{Key: StringBuilder([]string{ColumnService, 
ColumnAppID}), Value: bsonx.Int32(1)},
-                       {Key: StringBuilder([]string{ColumnService, 
ColumnServiceName}), Value: bsonx.Int32(1)},
-                       {Key: StringBuilder([]string{ColumnService, 
ColumnEnv}), Value: bsonx.Int32(1)},
-                       {Key: StringBuilder([]string{ColumnService, 
ColumnVersion}), Value: bsonx.Int32(1)},
-                       {Key: ColumnDomain, Value: bsonx.Int32(1)},
-                       {Key: ColumnProject, Value: bsonx.Int32(1)},
-               },
-               Options: options.Index().SetUnique(true),
-       }})
+func EnsureDB() {
+       EnsureService()
+       EnsureInstance()
+       EnsureRule()
+       EnsureSchema()
+       EnsureDep()
+}
+
+func EnsureService() {
+       err := 
client.GetMongoClient().GetDB().CreateCollection(context.Background(), 
CollectionService, options.CreateCollection().SetValidator(nil))
+       wrapCreateCollectionError(err)
+
+       serviceIDIndex := BuildIndexDoc(
+               StringBuilder([]string{ColumnService, ColumnServiceID}))
+       serviceIDIndex.Options = options.Index().SetUnique(true)
+
+       serviceIndex := BuildIndexDoc(
+               StringBuilder([]string{ColumnService, ColumnAppID}),
+               StringBuilder([]string{ColumnService, ColumnServiceName}),
+               StringBuilder([]string{ColumnService, ColumnEnv}),
+               StringBuilder([]string{ColumnService, ColumnVersion}),
+               ColumnDomain,
+               ColumnProject)
+       serviceIndex.Options = options.Index().SetUnique(true)
+
+       var serviceIndexs []mongo.IndexModel
+       serviceIndexs = append(serviceIndexs, serviceIDIndex, serviceIndex)
+
+       err = client.GetMongoClient().CreateIndexes(context.Background(), 
CollectionService, serviceIndexs)
        if err != nil {
+               log.Fatal("failed to create service collection indexs", err)
                return
        }
-       err = client.GetMongoClient().CreateIndexes(context.TODO(), 
CollectionInstance, []mongo.IndexModel{{
-               Keys:    bsonx.Doc{{Key: StringBuilder([]string{ColumnInstance, 
ColumnInstanceID}), Value: bsonx.Int32(1)}},
-               Options: options.Index().SetUnique(true),
-       }, {
-               Keys: bsonx.Doc{{Key: StringBuilder([]string{ColumnInstanceID, 
ColumnServiceID}), Value: bsonx.Int32(1)}},
-       }, {
-               Keys:    bsonx.Doc{{Key: ColumnRefreshTime, Value: 
bsonx.Int32(1)}},
-               Options: options.Index().SetExpireAfterSeconds(60),
-       }})
+}
+
+func EnsureInstance() {
+       err := 
client.GetMongoClient().GetDB().CreateCollection(context.Background(), 
CollectionInstance, options.CreateCollection().SetValidator(nil))
+       wrapCreateCollectionError(err)
+
+       instanceIDIndex := BuildIndexDoc(StringBuilder([]string{ColumnInstance, 
ColumnInstanceID}))

Review comment:
       > 不建议对instanceid做唯一性检查,你是否确定etcd做了这个限制?
   
   已删除




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to