Skip to content

Commit 3f466f7

Browse files
committed
Removed BaseModel and all duplicated fields
1 parent 8d8e038 commit 3f466f7

File tree

5 files changed

+57
-67
lines changed

5 files changed

+57
-67
lines changed

pkg/share/manager/sql/conversions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func convertToCS3OCMShare(s *model.OcmShare, am []*ocm.AccessMethod) *ocm.Share
111111
}
112112
if s.Expiration.Valid {
113113
share.Expiration = &types.Timestamp{
114-
Seconds: uint64(s.Expiration.Int64),
114+
Seconds: uint64(s.Expiration.V.Unix()),
115115
}
116116
}
117117
return share
@@ -149,7 +149,7 @@ func convertToCS3OCMReceivedShare(s *model.OcmReceivedShare, p []*ocm.Protocol)
149149
}
150150
if s.Expiration.Valid {
151151
share.Expiration = &types.Timestamp{
152-
Seconds: uint64(s.Expiration.Int64),
152+
Seconds: uint64(s.Expiration.V.Unix()),
153153
}
154154
}
155155
return share

pkg/share/manager/sql/model/model.go

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package model
2020

2121
import (
22-
"database/sql"
2322
"strconv"
2423
"time"
2524

@@ -29,7 +28,7 @@ import (
2928
resourcespb "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
3029
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
3130
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
32-
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
31+
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
3332
conversions "github.com/cs3org/reva/v3/pkg/cbox/utils"
3433

3534
"gorm.io/datatypes"
@@ -95,37 +94,31 @@ type ShareID struct {
9594
ID uint `gorm:"primarykey"`
9695
}
9796

98-
// This is the base model for all share types. We cannot use gorm.Model, because we want our ID
99-
// to be a foreign key to ShareID, but we incorporate the date fields from gorm. This struct
100-
// does not declare any unique indexes as it is embedded by others.
101-
type BaseModel struct {
97+
// This is the base model for all share types and embeds parts of gorm.Model. We can't use it, because
98+
// we want our ID to be a foreign key to ShareID, but we incorporate the date fields from GORM.
99+
// The commented-out fields would logically belong here, but we define them in each specific type
100+
// to control the unique indexes to be enforced in the corresponding tables.
101+
type ProtoShare struct {
102102
// Id has to be called Id and not ID, otherwise the foreign key will not work
103103
// ID is a special field in GORM, which it uses as the default Primary Key
104104
Id uint `gorm:"primaryKey;not null;autoIncrement:false"`
105105
ShareId ShareID `gorm:"foreignKey:Id;references:ID;constraint:OnDelete:CASCADE"` //;references:ID
106106
CreatedAt time.Time
107107
UpdatedAt time.Time
108-
DeletedAt gorm.DeletedAt `gorm:"index"`
109-
}
110-
111-
// ProtoShare contains fields that are common between PublicLinks and Shares.
112-
// We also define some indexes for performance reasons.
113-
type ProtoShare struct {
114-
BaseModel
108+
//DeletedAt gorm.DeletedAt `gorm:"index"`
109+
//Inode string `gorm:"size:32;index"`
110+
//Instance string `gorm:"size:32;index"`
115111
UIDOwner string `gorm:"size:64"`
116112
UIDInitiator string `gorm:"size:64;index"`
117113
ItemType ItemType `gorm:"size:16;index"` // file | folder | reference | symlink
118114
InitialPath string
119-
Inode string `gorm:"size:32;index"`
120-
Instance string `gorm:"size:32;index"`
121115
Permissions uint8
122116
Orphan bool
123117
Expiration datatypes.NullTime `gorm:"index"`
124118
}
125119

126120
// Share is a regular share between users or groups. The unique index ensures that there
127-
// can only be one share per (inode, instance, permissions, recipient) tuple, unless the share is deleted:
128-
// for that, we redeclare `DeletedAt`, `Inode`, and `Instance` for GORM to define the unique index.
121+
// can only be one share per (inode, instance, permissions, recipient) tuple, unless the share is deleted.
129122
type Share struct {
130123
ProtoShare
131124
DeletedAt gorm.DeletedAt `gorm:"uniqueIndex:u_share"`
@@ -139,7 +132,10 @@ type Share struct {
139132
// PublicLink is a public link share. We only enforce a unique constraint on the token.
140133
type PublicLink struct {
141134
ProtoShare
142-
Token string `gorm:"uniqueIndex:u_link_token;size:32"` // Current tokens are only 16 chars long, but old tokens used to be 32 characters
135+
DeletedAt gorm.DeletedAt `gorm:"index"`
136+
Inode string `gorm:"size:32"`
137+
Instance string `gorm:"size:32"`
138+
Token string `gorm:"uniqueIndex:u_link_token;size:32"` // Current tokens are only 16 chars long, but old tokens used to be 32 characters
143139
Quicklink bool
144140
NotifyUploads bool
145141
NotifyUploadsExtraRecipients string
@@ -159,21 +155,21 @@ type ShareState struct {
159155
}
160156

161157
// OcmShare represents an OCM share for a remote user. The unique index ensures that there
162-
// can only be one share per (inode, instance, shareWith, owner) tuple, unless the share is deleted:
163-
// for that, we redeclare `DeletedAt`, `Inode`, and `Instance` as in Share. In addition, tokens must be unique.
158+
// can only be one share per (inode, instance, recipient) tuple, unless the share is deleted.
159+
// In addition, tokens must be unique.
160+
// TODO(lopresti) see if we can consolidate Owner and Initiator with UIDOwner and UIDInitiator in ProtoShare
164161
type OcmShare struct {
165-
BaseModel
162+
ProtoShare
166163
DeletedAt gorm.DeletedAt `gorm:"uniqueIndex:u_ocmshare"`
167164
Inode string `gorm:"size:64;not null;uniqueIndex:u_ocmshare"`
168165
Instance string `gorm:"size:64;not null;uniqueIndex:u_ocmshare"`
169166
Token string `gorm:"size:255;not null;uniqueIndex:u_ocmshare_token"`
170167
Name string `gorm:"type:text;not null"`
171168
ShareWith string `gorm:"size:255;not null;uniqueIndex:u_ocmshare"`
172-
Owner string `gorm:"size:255;not null;uniqueIndex:u_ocmshare"`
169+
Owner string `gorm:"size:255;not null"`
173170
Initiator string `gorm:"type:text;not null"`
174171
Ctime uint64 `gorm:"not null"`
175172
Mtime uint64 `gorm:"not null"`
176-
Expiration sql.NullInt64 `gorm:"default:null"`
177173
RecipientType OcmShareType `gorm:"not null"`
178174
Protocols []OcmShareProtocol `gorm:"constraint:OnDelete:CASCADE;"`
179175
}
@@ -189,18 +185,18 @@ type OcmShareProtocol struct {
189185
// OcmReceivedShare represents an OCM share received from a remote user.
190186
type OcmReceivedShare struct {
191187
gorm.Model
192-
RemoteShareID string `gorm:"index:i_ocmrecshare_remoteshareid;not null"`
193-
Name string `gorm:"size:255;not null"`
194-
ItemType ItemType `gorm:"size:16;not null"`
195-
ShareWith string `gorm:"size:255;not null"`
196-
Owner string `gorm:"index:i_ocmrecshare_owner;size:255;not null"`
197-
Initiator string `gorm:"index:i_ocmrecshare_initiator;size:255;not null"`
198-
Ctime uint64 `gorm:"not null"`
199-
Mtime uint64 `gorm:"not null"`
200-
Expiration sql.NullInt64 `gorm:"default:null"`
201-
RecipientType OcmShareType `gorm:"index:i_ocmrecshare_type;not null"`
202-
State OcmShareState `gorm:"index:i_ocmrecshare_state;not null"`
203-
Alias string `gorm:"size:64"`
188+
RemoteShareID string `gorm:"index:i_ocmrecshare_remoteshareid;not null"`
189+
Name string `gorm:"size:255;not null"`
190+
ItemType ItemType `gorm:"size:16;not null"`
191+
ShareWith string `gorm:"size:255;not null"`
192+
Owner string `gorm:"index:i_ocmrecshare_owner;size:255;not null"`
193+
Initiator string `gorm:"index:i_ocmrecshare_initiator;size:255;not null"`
194+
Ctime uint64 `gorm:"not null"`
195+
Mtime uint64 `gorm:"not null"`
196+
Expiration datatypes.NullTime `gorm:"index"`
197+
RecipientType OcmShareType `gorm:"index:i_ocmrecshare_type;not null"`
198+
State OcmShareState `gorm:"index:i_ocmrecshare_state;not null"`
199+
Alias string `gorm:"size:64"`
204200
Hidden bool
205201
}
206202

@@ -219,10 +215,10 @@ type OcmReceivedShareProtocol struct {
219215
}
220216

221217
func (s *Share) AsCS3Share(granteeType userpb.UserType) *collaboration.Share {
222-
creationTs := &typespb.Timestamp{
218+
creationTs := &types.Timestamp{
223219
Seconds: uint64(s.CreatedAt.Unix()),
224220
}
225-
updateTs := &typespb.Timestamp{
221+
updateTs := &types.Timestamp{
226222
Seconds: uint64(s.UpdatedAt.Unix()),
227223
}
228224
share := &collaboration.Share{
@@ -244,7 +240,7 @@ func (s *Share) AsCS3Share(granteeType userpb.UserType) *collaboration.Share {
244240
}
245241

246242
if s.Expiration.Valid {
247-
share.Expiration = &typespb.Timestamp{
243+
share.Expiration = &types.Timestamp{
248244
Seconds: uint64(s.Expiration.V.Unix()),
249245
}
250246
}
@@ -271,16 +267,16 @@ func (s *Share) AsCS3ReceivedShare(state *ShareState, granteeType userpb.UserTyp
271267
}
272268

273269
func (p *PublicLink) AsCS3PublicShare() *link.PublicShare {
274-
ts := &typespb.Timestamp{
270+
ts := &types.Timestamp{
275271
Seconds: uint64(p.CreatedAt.Unix()),
276272
}
277273

278-
var expires *typespb.Timestamp
274+
var expires *types.Timestamp
279275
if p.Expiration.Valid {
280276
exp, err := p.Expiration.Value()
281277
if err == nil {
282278
expiration := exp.(time.Time)
283-
expires = &typespb.Timestamp{
279+
expires = &types.Timestamp{
284280
Seconds: uint64(expiration.Unix()),
285281
}
286282
}

pkg/share/manager/sql/ocm_shares.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/pkg/errors"
3939
"google.golang.org/genproto/protobuf/field_mask"
4040
"google.golang.org/protobuf/proto"
41+
"gorm.io/datatypes"
4142
"gorm.io/gorm"
4243

4344
_ "github.com/go-sql-driver/mysql"
@@ -101,8 +102,10 @@ func (m *mgr) StoreShare(ctx context.Context, s *ocm.Share) (*ocm.Share, error)
101102
RecipientType: convertFromCS3OCMShareType(s.ShareType),
102103
}
103104
if s.Expiration != nil {
104-
share.Expiration.Int64 = int64(s.Expiration.Seconds)
105-
share.Expiration.Valid = true
105+
share.Expiration = datatypes.NullTime{
106+
V: time.Unix(int64(s.Expiration.Seconds), 0),
107+
Valid: true,
108+
}
106109
}
107110
share.Id = id
108111
share.ShareId = model.ShareID{ID: id}
@@ -274,10 +277,11 @@ func (m *mgr) StoreReceivedShare(ctx context.Context, s *ocm.ReceivedShare) (*oc
274277
RecipientType: convertFromCS3OCMShareType(s.ShareType),
275278
State: convertFromCS3OCMShareState(s.State),
276279
}
277-
278280
if s.Expiration != nil {
279-
receivedShare.Expiration.Int64 = int64(s.Expiration.Seconds)
280-
receivedShare.Expiration.Valid = true
281+
receivedShare.Expiration = datatypes.NullTime{
282+
V: time.Unix(int64(s.Expiration.Seconds), 0),
283+
Valid: true,
284+
}
281285
}
282286

283287
id := tx.Create(receivedShare)
@@ -480,7 +484,7 @@ func (m *mgr) getByID(ctx context.Context, user *userpb.User, id *ocm.ShareId) (
480484
func (m *mgr) getByKey(ctx context.Context, user *userpb.User, key *ocm.ShareKey) (*ocm.Share, error) {
481485
var shareModel model.OcmShare
482486
if err := m.db.WithContext(ctx).
483-
Where("owner = ? AND storage_id = ? AND file_id = ? AND share_with = ? AND (initiator = ? OR owner = ?)",
487+
Where("owner = ? AND instance = ? AND inode = ? AND share_with = ? AND (initiator = ? OR owner = ?)",
484488
key.Owner.OpaqueId, key.ResourceId.StorageId, key.ResourceId.OpaqueId, formatUserID(key.Grantee.GetUserId()), user.Id.OpaqueId, user.Id.OpaqueId).
485489
First(&shareModel).Error; err != nil {
486490
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -562,7 +566,7 @@ func (m *mgr) deleteByID(ctx context.Context, user *userpb.User, id *ocm.ShareId
562566

563567
func (m *mgr) deleteByKey(ctx context.Context, user *userpb.User, key *ocm.ShareKey) error {
564568
result := m.db.WithContext(ctx).
565-
Where("owner = ? AND storage_id = ? AND file_id = ? AND share_with = ? AND (initiator = ? OR owner = ?)",
569+
Where("owner = ? AND instance = ? AND inode = ? AND share_with = ? AND (initiator = ? OR owner = ?)",
566570
key.Owner.OpaqueId, key.ResourceId.StorageId, key.ResourceId.OpaqueId, formatUserID(key.Grantee.GetUserId()), user.Id.OpaqueId, user.Id.OpaqueId).
567571
Delete(&model.OcmShare{})
568572

@@ -675,7 +679,7 @@ func translateFilters(filters []*ocm.ListOCMSharesRequest_Filter) (string, []any
675679
for n, f := range lst {
676680
switch filter := f.Term.(type) {
677681
case *ocm.ListOCMSharesRequest_Filter_ResourceId:
678-
filterQuery.WriteString("storage_id = ? AND file_id = ?")
682+
filterQuery.WriteString("instance = ? AND inode = ?")
679683
params = append(params, filter.ResourceId.StorageId, filter.ResourceId.OpaqueId)
680684
case *ocm.ListOCMSharesRequest_Filter_Creator:
681685
filterQuery.WriteString("initiator = ?")

pkg/share/manager/sql/public_link.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,8 @@ func (m *PublicShareMgr) CreatePublicShare(ctx context.Context, u *user.User, md
109109
return nil, errors.Wrap(err, "failed to create id for PublicShare")
110110
}
111111

112-
publiclink.BaseModel = model.BaseModel{
113-
Id: id,
114-
ShareId: model.ShareID{ID: id},
115-
}
116-
112+
publiclink.Id = id
113+
publiclink.ShareId = model.ShareID{ID: id}
117114
publiclink.UIDOwner = conversions.FormatUserID(md.Owner)
118115
publiclink.UIDInitiator = conversions.FormatUserID(user.Id)
119116
publiclink.InitialPath = md.Path
@@ -512,9 +509,7 @@ func emptyLinkWithId(id string) (*model.PublicLink, error) {
512509
}
513510
link := &model.PublicLink{
514511
ProtoShare: model.ProtoShare{
515-
BaseModel: model.BaseModel{
516-
Id: uint(intId),
517-
},
512+
Id: uint(intId),
518513
},
519514
}
520515
return link, nil

pkg/share/manager/sql/share.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,8 @@ func (m *ShareMgr) Share(ctx context.Context, md *provider.ResourceInfo, g *coll
122122
return nil, errors.Wrap(err, "failed to create id for PublicShare")
123123
}
124124

125-
share.BaseModel = model.BaseModel{
126-
Id: id,
127-
ShareId: model.ShareID{ID: id},
128-
}
129-
125+
share.Id = id
126+
share.ShareId = model.ShareID{ID: id}
130127
share.UIDOwner = conversions.FormatUserID(md.Owner)
131128
share.UIDInitiator = conversions.FormatUserID(user.Id)
132129
share.InitialPath = md.Path
@@ -606,9 +603,7 @@ func emptyShareWithId(id string) (*model.Share, error) {
606603
}
607604
share := &model.Share{
608605
ProtoShare: model.ProtoShare{
609-
BaseModel: model.BaseModel{
610-
Id: uint(intId),
611-
},
606+
Id: uint(intId),
612607
},
613608
}
614609
return share, nil

0 commit comments

Comments
 (0)