-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate_organisations.go
146 lines (123 loc) · 3.82 KB
/
migrate_organisations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"context"
"fmt"
"github.com/frain-dev/newcloud-migrator/convoy-23.9.2/database/postgres"
"github.com/frain-dev/newcloud-migrator/convoy-23.9.2/datastore"
"github.com/frain-dev/newcloud-migrator/convoy-23.9.2/util"
)
func (m *Migrator) RunOrgMigration() error {
orgs, err := m.loadOrganisations(pagedResponse{
Data: data{
Pagination: datastore.PaginationData{PerPage: 1000},
},
})
if err != nil {
return err
}
// filter orgs owned by the user
userOrgs := []datastore.Organisation{}
for _, org := range orgs {
if org.OwnerID == m.user.UID {
userOrgs = append(userOrgs, org)
}
}
m.userOrgs = userOrgs
if len(userOrgs) == 0 {
return fmt.Errorf("user does not own any orgs")
}
orgMemberRepo := postgres.NewOrgMemberRepo(m)
userIDs := map[string]struct{}{}
members := []*datastore.OrganisationMember{}
for _, org := range userOrgs {
mm, err := m.loadOrgMembers(orgMemberRepo, org.UID, defaultPageable)
if err != nil {
return fmt.Errorf("failed to load org %s members: %v", org.UID, err)
}
for _, mr := range mm {
userIDs[mr.UserID] = struct{}{}
}
members = append(members, mm...)
}
users, err := m.loadUsers(userIDs)
if err != nil {
return fmt.Errorf("failed to load org member users: %v", err)
}
err = m.SaveUsers(context.Background(), users)
if err != nil {
return fmt.Errorf("failed to save org memeber users: %v", err)
}
err = m.SaveOrganisations(context.Background(), userOrgs)
if err != nil {
return fmt.Errorf("failed to save orgs: %v", err)
}
err = m.SaveOrganisationMembers(context.Background(), members)
if err != nil {
return fmt.Errorf("failed to save orgs: %v", err)
}
return nil
}
const (
saveOrganizations = `
INSERT INTO convoy.organisations (id, name, owner_id, custom_domain, assigned_domain, created_at, updated_at, deleted_at)
VALUES (
:id, :name, :owner_id, :custom_domain, :assigned_domain, :created_at, :updated_at, :deleted_at
)
`
)
func (m *Migrator) SaveOrganisations(ctx context.Context, orgs []datastore.Organisation) error {
values := make([]map[string]interface{}, 0, len(orgs))
for i := range orgs {
org := &orgs[i]
values = append(values, map[string]interface{}{
"id": org.UID,
"name": org.Name,
"owner_id": org.OwnerID,
"custom_domain": org.CustomDomain,
"assigned_domain": org.AssignedDomain,
"created_at": org.CreatedAt,
"updated_at": org.UpdatedAt,
"deleted_at": org.DeletedAt,
})
}
_, err := m.newDB.NamedExecContext(ctx, saveOrganizations, values)
if err != nil {
return fmt.Errorf("failed to save orgs: %v", err)
}
return nil
}
const (
saveOrgMembers = `
INSERT INTO convoy.organisation_members (id, organisation_id, user_id, role_type, role_project, role_endpoint, created_at, updated_at, deleted_at)
VALUES (
:id, :organisation_id, :user_id, :role_type, :role_project,
:role_endpoint, :created_at, :updated_at, :deleted_at
)
`
)
func (m *Migrator) SaveOrganisationMembers(ctx context.Context, members []*datastore.OrganisationMember) error {
values := make([]map[string]interface{}, 0, len(members))
for _, member := range members {
var endpointID *string
var projectID *string
if !util.IsStringEmpty(member.Role.Endpoint) {
endpointID = &member.Role.Endpoint
}
if !util.IsStringEmpty(member.Role.Project) {
projectID = &member.Role.Project
}
values = append(values, map[string]interface{}{
"id": member.UID,
"organisation_id": member.OrganisationID,
"user_id": member.UserID,
"role_type": member.Role.Type,
"role_project": projectID,
"role_endpoint": endpointID,
"created_at": member.CreatedAt,
"updated_at": member.UpdatedAt,
"deleted_at": member.DeletedAt,
})
}
_, err := m.newDB.NamedExecContext(ctx, saveOrgMembers, values)
return err
}