-
-
Save zinvapel/65a6a0180b21f0c1bb3ecca49d6d8cf0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mux.Handle("/org/relink", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | |
kk, err := infra.CreatePgConnection(migrator.Config.KeycloakPgDsn) | |
if err != nil { | |
writer.WriteHeader(http.StatusOK) | |
writer.Write([]byte(err.Error())) | |
return | |
} | |
rows, err := kk.Query(context.Background(), | |
` | |
select ua.value | |
from user_attribute ua | |
left join user_attribute ua2 on ua.user_id = ua2.user_id and ua2.name = 'linkDone' | |
where ua.name = 'contragent_id' and ua2.id is null | |
`, | |
) | |
if err != nil { | |
writer.WriteHeader(http.StatusOK) | |
writer.Write([]byte(err.Error())) | |
return | |
} | |
uuidList := make([]string, 0) | |
for rows.Next() { | |
var uuid string | |
err = rows.Scan(&uuid) | |
if err != nil { | |
log.Printf("Unable to scan contragent attribute\n") | |
writer.WriteHeader(http.StatusOK) | |
writer.Write([]byte(err.Error())) | |
return | |
} | |
uuidList = append(uuidList, uuid) | |
} | |
if len(uuidList) > 0 { | |
if len(uuidList) > 10000 { | |
writer.WriteHeader(http.StatusOK) | |
writer.Write([]byte("Too much: " + strconv.Itoa(len(uuidList)))) | |
return | |
} | |
oh, err := infra.CreatePgConnection(migrator.Config.OldHubPgDsn) | |
if err != nil { | |
writer.WriteHeader(http.StatusOK) | |
writer.Write([]byte(err.Error())) | |
return | |
} | |
_, err = oh.Exec( | |
context.Background(), | |
`update contragents set migrated_at = null, confirmed_at = null where id = any($1)`, | |
pgx.QuerySimpleProtocol(true), | |
uuidList, | |
) | |
if err != nil { | |
writer.WriteHeader(http.StatusOK) | |
writer.Write([]byte(err.Error())) | |
return | |
} | |
} else { | |
writer.WriteHeader(http.StatusOK) | |
writer.Write([]byte("Nothing to remigrate")) | |
return | |
} | |
writer.WriteHeader(http.StatusOK) | |
writer.Write([]byte("Done: " + strconv.Itoa(len(uuidList)))) | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment