Skip to content

Commit c27f53e

Browse files
committed
Merge public_links into migrator
1 parent 8cb2428 commit c27f53e

File tree

2 files changed

+21
-93
lines changed

2 files changed

+21
-93
lines changed

share/sql/migrate.go

+14-89
Original file line numberDiff line numberDiff line change
@@ -31,89 +31,6 @@ type ShareOrLink struct {
3131
Link *model.PublicLink
3232
}
3333

34-
func RunMigration(username, password, host, name, gatewaysvc, token string, port int) {
35-
config := map[string]interface{}{
36-
"engine": "mysql",
37-
"db_username": username,
38-
"db_password": password,
39-
"db_host": host,
40-
"db_port": port,
41-
"db_name": name,
42-
"gatewaysvc": gatewaysvc,
43-
"dry_run": false,
44-
}
45-
tokenlessCtx, cancel := context.WithCancel(context.Background())
46-
ctx := appctx.ContextSetToken(tokenlessCtx, token)
47-
ctx = metadata.AppendToOutgoingContext(ctx, appctx.TokenHeader, token)
48-
defer cancel()
49-
50-
shareManager, err := NewShareManager(ctx, config)
51-
if err != nil {
52-
fmt.Println("Failed to create shareManager: " + err.Error())
53-
os.Exit(1)
54-
}
55-
sharemgr := shareManager.(*shareMgr)
56-
oldDb, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", username, password, host, port, name))
57-
if err != nil {
58-
fmt.Println("Failed to create db: " + err.Error())
59-
os.Exit(1)
60-
}
61-
migrator := Migrator{
62-
OldDb: oldDb,
63-
NewDb: sharemgr.db,
64-
ShareMgr: sharemgr,
65-
}
66-
67-
ch := make(chan *ShareOrLink, 100)
68-
go getAllShares(ctx, migrator, ch)
69-
for share := range ch {
70-
// TODO error handling
71-
if share.IsShare {
72-
fmt.Printf("Creating share %d\n", share.Share.ID)
73-
migrator.NewDb.Create(&share.Share)
74-
} else {
75-
fmt.Printf("Creating share %d\n", share.Link.ID)
76-
migrator.NewDb.Create(&share.Link)
77-
}
78-
}
79-
80-
}
81-
82-
func getAllShares(ctx context.Context, migrator Migrator, ch chan *ShareOrLink) {
83-
// First we find out what the highest ID is
84-
count, err := getCount(migrator)
85-
if err != nil {
86-
fmt.Println("Error getting highest id: " + err.Error())
87-
close(ch)
88-
return
89-
}
90-
fmt.Printf("Migrating %d shares\n", count)
91-
92-
query := "select id, coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, lower(coalesce(share_with, '')) as share_with, coalesce(fileid_prefix, '') as fileid_prefix, coalesce(item_source, '') as item_source, coalesce(item_type, '') as item_type, stime, permissions, share_type, orphan FROM oc_share order by id desc" // AND id=?"
93-
params := []interface{}{}
94-
95-
res, err := migrator.OldDb.Query(query, params...)
96-
97-
if err != nil {
98-
fmt.Printf("Fatal error: %s", err.Error())
99-
close(ch)
100-
return
101-
}
102-
103-
for res.Next() {
104-
var s OldShareEntry
105-
res.Scan(&s.ID, &s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.Prefix, &s.ItemSource, &s.ItemType, &s.STime, &s.Permissions, &s.ShareType, &s.Orphan)
106-
newShare, err := oldShareToNewShare(ctx, migrator, s)
107-
if err == nil {
108-
ch <- newShare
109-
} else {
110-
fmt.Printf("Error occured for share %s: %s\n", s.ID, err.Error())
111-
}
112-
}
113-
114-
close(ch)
115-
}
116-
11734
type OldShareEntry struct {
11835
ID int
11936
UIDOwner string
@@ -168,12 +85,20 @@ func RunMigration(username, password, host, name, gatewaysvc, token string, port
16885
defer cancel()
16986

17087
// Set up migrator
171-
shareManager, err := New(ctx, config)
88+
shareManager, err := NewShareManager(ctx, config)
17289
if err != nil {
17390
fmt.Println("Failed to create shareManager: " + err.Error())
17491
os.Exit(1)
17592
}
176-
sharemgr := shareManager.(*mgr)
93+
sharemgr := shareManager.(*shareMgr)
94+
95+
linkManager, err := NewPublicShareManager(ctx, config)
96+
if err != nil {
97+
fmt.Println("Failed to create shareManager: " + err.Error())
98+
os.Exit(1)
99+
}
100+
linkmgr := linkManager.(*publicShareMgr)
101+
177102
oldDb, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true", username, password, host, port, name))
178103
if err != nil {
179104
fmt.Println("Failed to create db: " + err.Error())
@@ -183,6 +108,7 @@ func RunMigration(username, password, host, name, gatewaysvc, token string, port
183108
OldDb: oldDb,
184109
NewDb: sharemgr.db,
185110
ShareMgr: sharemgr,
111+
LinkMgr: linkmgr,
186112
}
187113

188114
if dryRun {
@@ -371,10 +297,9 @@ func oldShareToNewShare(ctx context.Context, migrator Migrator, s *OldShareEntry
371297
} else if errors.Is(err, errtypes.NotFound(protoShare.Inode)) {
372298
fmt.Printf("Marked share %d as an orphan (%s, %s)\n", s.ID, protoShare.Instance, protoShare.Inode)
373299
protoShare.Orphan = true
374-
} else {
375-
// We do not set, because of a general error
376-
// fmt.Printf("An error occured for share %d while statting (%s, %s): %s\n", s.ID, protoShare.Instance, protoShare.Inode, err.Error())
377-
}
300+
} // else {
301+
// We do not set, because of a general error
302+
// }
378303
}
379304

380305
// ShareTypeUser = 0

share/sql/share.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ func NewShareManager(ctx context.Context, m map[string]interface{}) (revashare.M
8888
return mgr, nil
8989
}
9090

91-
func (m *mgr) getDb() *gorm.DB {
92-
return m.db
93-
}
94-
9591
func (m *shareMgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collaboration.ShareGrant) (*collaboration.Share, error) {
9692
user := appctx.ContextMustGetUser(ctx)
9793

@@ -105,7 +101,14 @@ func (m *shareMgr) Share(ctx context.Context, md *provider.ResourceInfo, g *coll
105101
// check if share already exists.
106102
key := &collaboration.ShareKey{
107103
Owner: md.Owner,
104+
ResourceId: md.Id,
108105
Grantee: g.Grantee,
106+
}
107+
_, err := m.getShareByKey(ctx, key, true)
108+
// share already exists
109+
if err == nil {
110+
return nil, errors.New(errtypes.AlreadyExists(key.String()).Error())
111+
}
109112

110113
var shareWith string
111114
if g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER {

0 commit comments

Comments
 (0)