From ec051e5481b14268a1199b40f557a9d592bc467e Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Sun, 29 Mar 2020 08:34:25 +0200 Subject: [PATCH] Backend: Improve file share and sync entities #225 Signed-off-by: Michael Mayer --- internal/entity/file_share.go | 23 +++++++++++------------ internal/entity/file_sync.go | 15 +++++++-------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/internal/entity/file_share.go b/internal/entity/file_share.go index 89b3df4cc..66605b0ed 100644 --- a/internal/entity/file_share.go +++ b/internal/entity/file_share.go @@ -10,18 +10,17 @@ import ( // FileShare represents a one-to-many relation between File and Account for pushing files to remote services. type FileShare struct { - FileID uint `gorm:"primary_key;auto_increment:false"` - AccountID uint `gorm:"primary_key;auto_increment:false"` - RemoteName string `gorm:"primary_key;auto_increment:false;type:varbinary(256)"` - ShareStatus string `gorm:"type:varbinary(16);"` - ShareError string `gorm:"type:varbinary(512);"` - RetryCount uint - File *File - Account *Account - RemoveAt sql.NullTime - PushedAt sql.NullTime - CreatedAt time.Time - UpdatedAt time.Time + FileID uint `gorm:"primary_key;auto_increment:false"` + AccountID uint `gorm:"primary_key;auto_increment:false"` + RemoteName string `gorm:"primary_key;auto_increment:false;type:varbinary(256)"` + Status string `gorm:"type:varbinary(16);"` + Error string `gorm:"type:varbinary(512);"` + File *File + Account *Account + SharedAt sql.NullTime + ExpiresAt sql.NullTime + CreatedAt time.Time + UpdatedAt time.Time } // TableName returns the entity database table name. diff --git a/internal/entity/file_sync.go b/internal/entity/file_sync.go index b2dec8ca5..4bb01b5de 100644 --- a/internal/entity/file_sync.go +++ b/internal/entity/file_sync.go @@ -10,12 +10,11 @@ import ( // FileSync represents a one-to-many relation between File and Account for syncing with remote services. type FileSync struct { - FileID uint `gorm:"primary_key;auto_increment:false"` + FileID uint `gorm:"index;"` AccountID uint `gorm:"primary_key;auto_increment:false"` - RemoteName string `gorm:"type:varbinary(256);"` - SyncStatus string `gorm:"type:varbinary(16);"` - SyncError string `gorm:"type:varbinary(512);"` - RetryCount uint + RemoteName string `gorm:"type:varbinary(256);primary_key;auto_increment:false"` + Status string `gorm:"type:varbinary(16);"` + Error string `gorm:"type:varbinary(512);"` File *File Account *Account SyncedAt sql.NullTime @@ -29,10 +28,10 @@ func (FileSync) TableName() string { } // NewFileSync creates a new entity. -func NewFileSync(fileID, accountID uint) *FileSync { +func NewFileSync(accountID uint, remoteName string) *FileSync { result := &FileSync{ - FileID: fileID, AccountID: accountID, + RemoteName: remoteName, } return result @@ -43,7 +42,7 @@ func (m *FileSync) FirstOrCreate(db *gorm.DB) *FileSync { mutex.Db.Lock() defer mutex.Db.Unlock() - if err := db.FirstOrCreate(m, "file_id = ? AND account_id = ?", m.FileID, m.AccountID).Error; err != nil { + if err := db.FirstOrCreate(m, "account_id = ? AND remote_name = ?", m.AccountID, m.RemoteName).Error; err != nil { log.Errorf("file sync: %s", err) }