|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "github.com/openimsdk/chat/internal/rpc/chat" |
| 8 | + "github.com/openimsdk/chat/pkg/common/cmd" |
| 9 | + "github.com/openimsdk/chat/pkg/common/config" |
| 10 | + "github.com/openimsdk/chat/pkg/common/constant" |
| 11 | + table "github.com/openimsdk/chat/pkg/common/db/table/chat" |
| 12 | + "github.com/openimsdk/chat/tools/dataversion" |
| 13 | + "github.com/openimsdk/protocol/sdkws" |
| 14 | + "github.com/openimsdk/tools/db/mongoutil" |
| 15 | + "github.com/openimsdk/tools/system/program" |
| 16 | + "go.mongodb.org/mongo-driver/bson" |
| 17 | + "go.mongodb.org/mongo-driver/mongo" |
| 18 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 19 | + "path/filepath" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + credentialKey = "credential" |
| 24 | + credentialVersion = 1 |
| 25 | + |
| 26 | + attributeCollection = "attribute" |
| 27 | + credentialCollection = "credential" |
| 28 | + pageNum = 1000 |
| 29 | +) |
| 30 | + |
| 31 | +func initConfig(configDir string) (*config.Mongo, error) { |
| 32 | + var ( |
| 33 | + mongoConfig = &config.Mongo{} |
| 34 | + ) |
| 35 | + err := config.LoadConfig(filepath.Join(configDir, cmd.MongodbConfigFileName), cmd.ConfigEnvPrefixMap[cmd.MongodbConfigFileName], mongoConfig) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + return mongoConfig, nil |
| 41 | +} |
| 42 | + |
| 43 | +func pageGetAttribute(ctx context.Context, coll *mongo.Collection, pagination *sdkws.RequestPagination) (int64, []*table.Attribute, error) { |
| 44 | + return mongoutil.FindPage[*table.Attribute](ctx, coll, bson.M{}, pagination) |
| 45 | +} |
| 46 | + |
| 47 | +func doAttributeToCredential() error { |
| 48 | + var index int |
| 49 | + var configDir string |
| 50 | + flag.IntVar(&index, "i", 0, "Index number") |
| 51 | + defaultConfigDir := filepath.Join("..", "..", "..", "..", "..", "config") |
| 52 | + flag.StringVar(&configDir, "c", defaultConfigDir, "Configuration dir") |
| 53 | + flag.Parse() |
| 54 | + |
| 55 | + fmt.Printf("Index: %d, Config Path: %s\n", index, configDir) |
| 56 | + |
| 57 | + mongoConfig, err := initConfig(configDir) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + ctx := context.Background() |
| 63 | + |
| 64 | + mgocli, err := mongoutil.NewMongoDB(ctx, mongoConfig.Build()) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + versionColl := mgocli.GetDB().Collection(dataversion.Collection) |
| 70 | + converted, err := dataversion.CheckVersion(versionColl, credentialKey, credentialVersion) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + if converted { |
| 75 | + fmt.Println("[credential] credential data has been converted") |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + attrColl := mgocli.GetDB().Collection(attributeCollection) |
| 80 | + credColl := mgocli.GetDB().Collection(credentialCollection) |
| 81 | + |
| 82 | + pagination := &sdkws.RequestPagination{ |
| 83 | + PageNumber: 1, |
| 84 | + ShowNumber: pageNum, |
| 85 | + } |
| 86 | + tx := mgocli.GetTx() |
| 87 | + if err = tx.Transaction(ctx, func(ctx context.Context) error { |
| 88 | + for { |
| 89 | + total, attrs, err := pageGetAttribute(ctx, attrColl, pagination) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + credentials := make([]*table.Credential, 0, pageNum*3) |
| 94 | + for _, attr := range attrs { |
| 95 | + if attr.Email != "" { |
| 96 | + credentials = append(credentials, &table.Credential{ |
| 97 | + UserID: attr.UserID, |
| 98 | + Account: attr.Email, |
| 99 | + Type: constant.CredentialEmail, |
| 100 | + AllowChange: true, |
| 101 | + }) |
| 102 | + } |
| 103 | + if attr.Account != "" { |
| 104 | + credentials = append(credentials, &table.Credential{ |
| 105 | + UserID: attr.UserID, |
| 106 | + Account: attr.Account, |
| 107 | + Type: constant.CredentialAccount, |
| 108 | + AllowChange: true, |
| 109 | + }) |
| 110 | + } |
| 111 | + if attr.PhoneNumber != "" && attr.AreaCode != "" { |
| 112 | + credentials = append(credentials, &table.Credential{ |
| 113 | + UserID: attr.UserID, |
| 114 | + Account: chat.BuildCredentialPhone(attr.AreaCode, attr.PhoneNumber), |
| 115 | + Type: constant.CredentialPhone, |
| 116 | + AllowChange: true, |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + for _, credential := range credentials { |
| 122 | + err = mongoutil.UpdateOne(ctx, credColl, bson.M{ |
| 123 | + "user_id": credential.UserID, |
| 124 | + "type": credential.Type, |
| 125 | + }, bson.M{ |
| 126 | + "$set": credential, |
| 127 | + }, false, options.Update().SetUpsert(true)) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + pagination.PageNumber++ |
| 134 | + if total < pageNum { |
| 135 | + break |
| 136 | + } |
| 137 | + } |
| 138 | + return nil |
| 139 | + }); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + if err := dataversion.SetVersion(versionColl, credentialKey, credentialVersion); err != nil { |
| 143 | + return fmt.Errorf("set mongodb credential version %w", err) |
| 144 | + } |
| 145 | + fmt.Println("[credential] update old data to credential success") |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func main() { |
| 150 | + if err := doAttributeToCredential(); err != nil { |
| 151 | + program.ExitWithError(err) |
| 152 | + } |
| 153 | +} |
0 commit comments