Skip to content

Commit

Permalink
fix(api): migrate project_group for signature (#4973)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Feb 12, 2020
1 parent d079d39 commit 091e083
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions engine/api/migrate/refactor_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,63 @@ func refactorGroupMembership(ctx context.Context, db *gorp.DbMap) error {
return nil
}

func refactorLinkGroupProject(ctx context.Context, db *gorp.DbMap) error {
// Third step
// Migrate data from table user_group
tx, err := db.Begin()
if err != nil {
return sdk.WithStack(err)
}

defer tx.Rollback() // nolint

rows, err := tx.Query(`
SELECT id, group_id, project_id, role
FROM project_group
FOR UPDATE SKIP LOCKED
`)
if err == sql.ErrNoRows {
return nil
}
if err != nil {
return sdk.WithStack(err)
}
defer rows.Close()

var links []*group.LinkGroupProject

for rows.Next() {
var id int64
var groupID int64
var projectID int64
var role int
if err := rows.Scan(&id, &groupID, &projectID, &role); err != nil {
return sdk.WithStack(err)
}

var l = group.LinkGroupProject{
ID: id,
GroupID: groupID,
ProjectID: projectID,
Role: role,
}

links = append(links, &l)
}

for _, l := range links {
if err := group.UpdateLinkGroupProject(tx, l); err != nil {
return sdk.WithStack(err)
}
}

if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}

return nil
}

// RefactorGroupMembership .
func RefactorGroupMembership(ctx context.Context, db *gorp.DbMap) error {
log.Debug("migrate.RefactorGroupMembership> begin")
Expand All @@ -135,5 +192,9 @@ func RefactorGroupMembership(ctx context.Context, db *gorp.DbMap) error {
return err
}

if err := refactorLinkGroupProject(ctx, db); err != nil {
return err
}

return nil
}

0 comments on commit 091e083

Please sign in to comment.