Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement db incr migrate #1

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ builds:
- amd64
- arm64
goarm:
- 6
- 7
- "6"
- "7"
- id: openIM.wasm
main: wasm/cmd/main.go # 指定 wasm 主文件路径
binary: openIM.wasm
Expand Down
34 changes: 34 additions & 0 deletions pkg/db/app_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package db

import (
"context"

"github.com/openimsdk/tools/errs"
"gorm.io/gorm"

"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
)

func (d *DataBase) GetAppSDKVersion(ctx context.Context) (*model_struct.LocalAppSDKVersion, error) {
var appVersion model_struct.LocalAppSDKVersion
return &appVersion, errs.Wrap(d.conn.WithContext(ctx).Take(&appVersion).Error)
}

func (d *DataBase) SetAppSDKVersion(ctx context.Context, appVersion *model_struct.LocalAppSDKVersion) error {
var exist model_struct.LocalAppSDKVersion
err := d.conn.WithContext(ctx).First(&exist).Error
if err == gorm.ErrRecordNotFound {
if createErr := d.conn.WithContext(ctx).Create(appVersion).Error; createErr != nil {
return errs.Wrap(createErr)
}
return nil
} else if err != nil {
return errs.Wrap(err)
}

if updateErr := d.conn.WithContext(ctx).Model(&exist).Updates(appVersion).Error; updateErr != nil {
return errs.Wrap(updateErr)
}

return nil
}
96 changes: 62 additions & 34 deletions pkg/db/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
"github.com/openimsdk/openim-sdk-core/v3/version"

"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/log"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
Expand All @@ -47,7 +49,7 @@ type DataBase struct {
}

func (d *DataBase) GetMultipleMessageReactionExtension(ctx context.Context, msgIDList []string) (result []*model_struct.LocalChatLogReactionExtensions, err error) {
//TODO implement me
// TODO implement me
panic("implement me")
}

Expand Down Expand Up @@ -128,46 +130,72 @@ func (d *DataBase) initDB(ctx context.Context, logLevel int) error {
sqlDB.SetConnMaxIdleTime(time.Minute * 10)
d.conn = db

err = db.AutoMigrate(
&model_struct.LocalFriend{},
&model_struct.LocalFriendRequest{},
&model_struct.LocalGroup{},
&model_struct.LocalGroupMember{},
&model_struct.LocalGroupRequest{},
&model_struct.LocalErrChatLog{},
&model_struct.LocalUser{},
&model_struct.LocalBlack{},
&model_struct.LocalConversation{},
&model_struct.NotificationSeqs{},
&model_struct.LocalChatLog{},
&model_struct.LocalAdminGroupRequest{},
&model_struct.LocalWorkMomentsNotification{},
&model_struct.LocalWorkMomentsNotificationUnreadCount{},
&model_struct.TempCacheLocalChatLog{},
&model_struct.LocalChatLogReactionExtensions{},
&model_struct.LocalUpload{},
&model_struct.LocalStranger{},
&model_struct.LocalSendingMessages{},
&model_struct.LocalUserCommand{},
&model_struct.LocalVersionSync{},
)
if err != nil {
// base
if err = db.AutoMigrate(&model_struct.LocalAppSDKVersion{}); err != nil {
return err
}

if err = d.versionDataMigrate(ctx); err != nil {
return err
}

//if err := db.Table(constant.SuperGroupTableName).AutoMigrate(superGroup); err != nil {
// return err
//}

return nil
}

func (d *DataBase) versionDataFix(ctx context.Context) {
//todo some model auto migrate data conversion
//conversationIDs, err := d.FindAllConversationConversationID(ctx)
//if err != nil {
// log.ZError(ctx, "FindAllConversationConversationID err", err)
//}
//for _, conversationID := range conversationIDs {
// d.conn.WithContext(ctx).Table(utils.GetTableName(conversationID)).AutoMigrate(&model_struct.LocalChatLog{})
//}
func (d *DataBase) versionDataMigrate(ctx context.Context) error {
verModel, err := d.GetAppSDKVersion(ctx)
log.ZDebug(ctx, "SDK err is", errs.Unwrap(err))
if errs.Unwrap(err) == gorm.ErrRecordNotFound {
err = d.conn.AutoMigrate(
&model_struct.LocalAppSDKVersion{},
&model_struct.LocalFriend{},
&model_struct.LocalFriendRequest{},
&model_struct.LocalGroup{},
&model_struct.LocalGroupMember{},
&model_struct.LocalGroupRequest{},
&model_struct.LocalErrChatLog{},
&model_struct.LocalUser{},
&model_struct.LocalBlack{},
&model_struct.LocalConversation{},
&model_struct.NotificationSeqs{},
&model_struct.LocalChatLog{},
&model_struct.LocalAdminGroupRequest{},
&model_struct.LocalWorkMomentsNotification{},
&model_struct.LocalWorkMomentsNotificationUnreadCount{},
&model_struct.TempCacheLocalChatLog{},
&model_struct.LocalChatLogReactionExtensions{},
&model_struct.LocalUpload{},
&model_struct.LocalStranger{},
&model_struct.LocalSendingMessages{},
&model_struct.LocalUserCommand{},
&model_struct.LocalVersionSync{},
)
if err != nil {
return err
}
err = d.SetAppSDKVersion(ctx, &model_struct.LocalAppSDKVersion{Version: version.Version})
if err != nil {
return err
}

return nil
} else if err != nil {
return err
}
if verModel.Version != version.Version {
switch version.Version {
case "3.8.0":
d.conn.AutoMigrate(&model_struct.LocalAppSDKVersion{})
}
err = d.SetAppSDKVersion(ctx, &model_struct.LocalAppSDKVersion{Version: version.Version})
if err != nil {
return err
}
}

return nil
}
43 changes: 24 additions & 19 deletions pkg/db/db_interface/databse.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,38 @@ type GroupModel interface {

type MessageModel interface {
BatchInsertMessageList(ctx context.Context, conversationID string, MessageList []*model_struct.LocalChatLog) error
//BatchInsertMessageListController(ctx context.Context, MessageList []*model_struct.LocalChatLog) error
// BatchInsertMessageListController(ctx context.Context, MessageList []*model_struct.LocalChatLog) error
InsertMessage(ctx context.Context, conversationID string, Message *model_struct.LocalChatLog) error
//InsertMessageController(ctx context.Context, message *model_struct.LocalChatLog) error
// InsertMessageController(ctx context.Context, message *model_struct.LocalChatLog) error
SearchMessageByKeyword(ctx context.Context, contentType []int, keywordList []string, keywordListMatchType int, conversationID string, startTime, endTime int64, offset, count int) (result []*model_struct.LocalChatLog, err error)
//SearchMessageByKeywordController(ctx context.Context, contentType []int, keywordList []string, keywordListMatchType int, sourceID string, startTime, endTime int64, sessionType, offset, count int) (result []*model_struct.LocalChatLog, err error)
// SearchMessageByKeywordController(ctx context.Context, contentType []int, keywordList []string, keywordListMatchType int, sourceID string, startTime, endTime int64, sessionType, offset, count int) (result []*model_struct.LocalChatLog, err error)
SearchMessageByContentType(ctx context.Context, contentType []int, conversationID string, startTime, endTime int64, offset, count int) (result []*model_struct.LocalChatLog, err error)
//SearchMessageByContentTypeController(ctx context.Context, contentType []int, sourceID string, startTime, endTime int64, sessionType, offset, count int) (result []*model_struct.LocalChatLog, err error)
// SearchMessageByContentTypeController(ctx context.Context, contentType []int, sourceID string, startTime, endTime int64, sessionType, offset, count int) (result []*model_struct.LocalChatLog, err error)
SearchMessageByContentTypeAndKeyword(ctx context.Context, contentType []int, conversationID string, keywordList []string, keywordListMatchType int, startTime, endTime int64) (result []*model_struct.LocalChatLog, err error)
//SearchMessageByContentTypeAndKeywordController(ctx context.Context, contentType []int, keywordList []string, keywordListMatchType int, startTime, endTime int64) (result []*model_struct.LocalChatLog, err error)
//BatchUpdateMessageList(ctx context.Context, MessageList []*model_struct.LocalChatLog) error
//BatchSpecialUpdateMessageList(ctx context.Context, MessageList []*model_struct.LocalChatLog) error
// SearchMessageByContentTypeAndKeywordController(ctx context.Context, contentType []int, keywordList []string, keywordListMatchType int, startTime, endTime int64) (result []*model_struct.LocalChatLog, err error)
// BatchUpdateMessageList(ctx context.Context, MessageList []*model_struct.LocalChatLog) error
// BatchSpecialUpdateMessageList(ctx context.Context, MessageList []*model_struct.LocalChatLog) error
MessageIfExists(ctx context.Context, ClientMsgID string) (bool, error)
IsExistsInErrChatLogBySeq(ctx context.Context, seq int64) bool
MessageIfExistsBySeq(ctx context.Context, seq int64) (bool, error)
GetMessage(ctx context.Context, conversationID, clientMsgID string) (*model_struct.LocalChatLog, error)
GetMessageBySeq(ctx context.Context, conversationID string, seq int64) (*model_struct.LocalChatLog, error)
//GetMessageController(ctx context.Context, conversationID, clientMsgID string) (*model_struct.LocalChatLog, error)
// GetMessageController(ctx context.Context, conversationID, clientMsgID string) (*model_struct.LocalChatLog, error)
UpdateColumnsMessageList(ctx context.Context, clientMsgIDList []string, args map[string]interface{}) error
UpdateColumnsMessage(ctx context.Context, conversationID string, ClientMsgID string, args map[string]interface{}) error
//UpdateColumnsMessageController(ctx context.Context, ClientMsgID string, groupID string, sessionType int32, args map[string]interface{}) error
// UpdateColumnsMessageController(ctx context.Context, ClientMsgID string, groupID string, sessionType int32, args map[string]interface{}) error
UpdateMessage(ctx context.Context, conversationID string, c *model_struct.LocalChatLog) error
UpdateMessageBySeq(ctx context.Context, conversationID string, c *model_struct.LocalChatLog) error
//UpdateMessageController(ctx context.Context, c *model_struct.LocalChatLog) error
// UpdateMessageController(ctx context.Context, c *model_struct.LocalChatLog) error
DeleteAllMessage(ctx context.Context) error
UpdateMessageStatusBySourceID(ctx context.Context, sourceID string, status, sessionType int32) error
//UpdateMessageStatusBySourceIDController(ctx context.Context, sourceID string, status, sessionType int32) error
// UpdateMessageStatusBySourceIDController(ctx context.Context, sourceID string, status, sessionType int32) error
UpdateMessageTimeAndStatus(ctx context.Context, conversationID, clientMsgID string, serverMsgID string, sendTime int64, status int32) error
//UpdateMessageTimeAndStatusController(ctx context.Context, msg *sdk_struct.MsgStruct) error
// UpdateMessageTimeAndStatusController(ctx context.Context, msg *sdk_struct.MsgStruct) error
GetMessageList(ctx context.Context, conversationID string, count int, startTime int64, isReverse bool) (result []*model_struct.LocalChatLog, err error)
//GetMessageListController(ctx context.Context, sourceID string, sessionType, count int, startTime int64, isReverse bool) (result []*model_struct.LocalChatLog, err error)
// GetMessageListController(ctx context.Context, sourceID string, sessionType, count int, startTime int64, isReverse bool) (result []*model_struct.LocalChatLog, err error)
GetMessageListNoTime(ctx context.Context, conversationID string, count int, isReverse bool) (result []*model_struct.LocalChatLog, err error)
//GetMessageListNoTimeController(ctx context.Context, sourceID string, sessionType, count int, isReverse bool) (result []*model_struct.LocalChatLog, err error)
// GetMessageListNoTimeController(ctx context.Context, sourceID string, sessionType, count int, isReverse bool) (result []*model_struct.LocalChatLog, err error)
MarkConversationMessageAsReadDB(ctx context.Context, conversationID string, msgIDs []string) (rowsAffected int64, err error)
MarkConversationMessageAsReadBySeqs(ctx context.Context, conversationID string, seqs []int64) (rowsAffected int64, err error)
GetUnreadMessage(ctx context.Context, conversationID string) (result []*model_struct.LocalChatLog, err error)
Expand All @@ -119,10 +119,10 @@ type MessageModel interface {
GetTestMessage(ctx context.Context, seq uint32) (*model_struct.LocalChatLog, error)
UpdateMsgSenderNickname(ctx context.Context, sendID, nickname string, sType int) error
UpdateMsgSenderFaceURL(ctx context.Context, sendID, faceURL string, sType int) error
//UpdateMsgSenderFaceURLAndSenderNicknameController(ctx context.Context, sendID, faceURL, nickname string, sessionType int) error
// UpdateMsgSenderFaceURLAndSenderNicknameController(ctx context.Context, sendID, faceURL, nickname string, sessionType int) error
UpdateMsgSenderFaceURLAndSenderNickname(ctx context.Context, conversationID, sendID, faceURL, nickname string) error
GetMsgSeqByClientMsgID(ctx context.Context, clientMsgID string) (uint32, error)
//GetMsgSeqByClientMsgIDController(ctx context.Context, m *sdk_struct.MsgStruct) (uint32, error)
// GetMsgSeqByClientMsgIDController(ctx context.Context, m *sdk_struct.MsgStruct) (uint32, error)
GetMsgSeqListByGroupID(ctx context.Context, groupID string) ([]uint32, error)
GetMsgSeqListByPeerUserID(ctx context.Context, userID string) ([]uint32, error)
GetMsgSeqListBySelfUserID(ctx context.Context, userID string) ([]uint32, error)
Expand All @@ -141,7 +141,7 @@ type MessageModel interface {
DeleteConversationAllMessages(ctx context.Context, conversationID string) error
MarkDeleteConversationAllMessages(ctx context.Context, conversationID string) error
SuperGroupSearchMessageByKeyword(ctx context.Context, contentType []int, keywordList []string, keywordListMatchType int, sourceID string, startTime, endTime int64, sessionType, offset, count int) (result []*model_struct.LocalChatLog, err error)
//SuperGroupSearchMessageByContentType(ctx context.Context, contentType []int, sourceID string, startTime, endTime int64, sessionType, offset, count int) (result []*model_struct.LocalChatLog, err error)
// SuperGroupSearchMessageByContentType(ctx context.Context, contentType []int, sourceID string, startTime, endTime int64, sessionType, offset, count int) (result []*model_struct.LocalChatLog, err error)
SuperGroupSearchMessageByContentTypeAndKeyword(ctx context.Context, contentType []int, keywordList []string, keywordListMatchType int, startTime, endTime int64, groupID string) (result []*model_struct.LocalChatLog, err error)

SuperGroupBatchUpdateMessageList(ctx context.Context, MessageList []*model_struct.LocalChatLog) error
Expand All @@ -158,7 +158,7 @@ type MessageModel interface {
SuperGroupGetMessageListNoTime(ctx context.Context, sourceID string, sessionType, count int, isReverse bool) (result []*model_struct.LocalChatLog, err error)
SuperGroupGetSendingMessageList(ctx context.Context, groupID string) (result []*model_struct.LocalChatLog, err error)
SuperGroupUpdateGroupMessageHasRead(ctx context.Context, msgIDList []string, groupID string) error
//SuperGroupUpdateGroupMessageFields(ctx context.Context, msgIDList []string, groupID string, args map[string]interface{}) error
// SuperGroupUpdateGroupMessageFields(ctx context.Context, msgIDList []string, groupID string, args map[string]interface{}) error

SuperGroupUpdateMsgSenderNickname(ctx context.Context, sendID, nickname string, sType int) error
SuperGroupUpdateMsgSenderFaceURL(ctx context.Context, sendID, faceURL string, sType int) error
Expand All @@ -172,7 +172,7 @@ type MessageModel interface {
BatchInsertConversationUnreadMessageList(ctx context.Context, messageList []*model_struct.LocalConversationUnreadMessage) error
DeleteConversationUnreadMessageList(ctx context.Context, conversationID string, sendTime int64) int64
DeleteConversationMsgs(ctx context.Context, conversationID string, msgIDs []string) error
//DeleteConversationMsgsBySeqs(ctx context.Context, conversationID string, seqs []int64) error
// DeleteConversationMsgsBySeqs(ctx context.Context, conversationID string, seqs []int64) error
SetNotificationSeq(ctx context.Context, conversationID string, seq int64) error
GetNotificationAllSeqs(ctx context.Context) ([]*model_struct.NotificationSeqs, error)
}
Expand Down Expand Up @@ -280,6 +280,10 @@ type VersionSyncModel interface {
GetVersionSync(ctx context.Context, tableName, entityID string) (*model_struct.LocalVersionSync, error)
SetVersionSync(ctx context.Context, version *model_struct.LocalVersionSync) error
}
type AppSDKVersion interface {
GetAppSDKVersion(ctx context.Context) (*model_struct.LocalAppSDKVersion, error)
SetAppSDKVersion(ctx context.Context, version *model_struct.LocalAppSDKVersion) error
}

type DataBase interface {
Close(ctx context.Context) error
Expand All @@ -293,4 +297,5 @@ type DataBase interface {
S3Model
SendingMessagesModel
VersionSyncModel
AppSDKVersion
}
1 change: 1 addition & 0 deletions version/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.7.0
6 changes: 6 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package version

import _ "embed"

//go:embed version
var Version string