MCPcopy Index your code
hub / github.com/rilldata/rill / InsertOrganizationMemberUser

Method InsertOrganizationMemberUser

admin/database/postgres/postgres.go:2095–2132  ·  view source on GitHub ↗
(ctx context.Context, orgID, userID, roleID string, attributes map[string]interface{}, ifNotExists bool)

Source from the content-addressed store, hash-verified

2093}
2094
2095func (c *connection) InsertOrganizationMemberUser(ctx context.Context, orgID, userID, roleID string, attributes map[string]interface{}, ifNotExists bool) (bool, error) {
2096 attrs, err := c.validateAttributes(attributes)
2097 if err != nil {
2098 return false, err
2099 }
2100
2101 if !ifNotExists {
2102 res, err := c.getDB(ctx).ExecContext(ctx, "INSERT INTO users_orgs_roles (user_id, org_id, org_role_id, attributes) VALUES ($1, $2, $3, $4)", userID, orgID, roleID, attrs)
2103 if err != nil {
2104 return false, parseErr("org member", err)
2105 }
2106 rows, err := res.RowsAffected()
2107 if err != nil {
2108 return false, err
2109 }
2110 if rows == 0 {
2111 return false, fmt.Errorf("no rows affected when adding user to organization")
2112 }
2113 return true, nil
2114 }
2115
2116 res, err := c.getDB(ctx).ExecContext(ctx, `
2117 INSERT INTO users_orgs_roles (user_id, org_id, org_role_id, attributes)
2118 VALUES ($1, $2, $3, $4)
2119 ON CONFLICT (user_id, org_id) DO NOTHING
2120 `, userID, orgID, roleID, attributes)
2121 if err != nil {
2122 return false, parseErr("org member", err)
2123 }
2124 rows, err := res.RowsAffected()
2125 if err != nil {
2126 return false, err
2127 }
2128 if rows > 1 {
2129 panic(fmt.Errorf("expected to update 0 or 1 row, but updated %d", rows))
2130 }
2131 return rows != 0, nil
2132}
2133
2134func (c *connection) DeleteOrganizationMemberUser(ctx context.Context, orgID, userID string) error {
2135 res, err := c.getDB(ctx).ExecContext(ctx, "DELETE FROM users_orgs_roles WHERE user_id = $1 AND org_id = $2", userID, orgID)

Callers

nothing calls this directly

Calls 5

validateAttributesMethod · 0.95
getDBMethod · 0.95
parseErrFunction · 0.85
ErrorfMethod · 0.65
ExecContextMethod · 0.45

Tested by

no test coverage detected