Created
January 31, 2022 10:19
-
-
Save subomi/eb270b9ac97bef2682655592f4059ca8 to your computer and use it in GitHub Desktop.
Migrate to Convoy.
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
package main | |
import ( | |
"context" | |
"database/sql" | |
"fmt" | |
"log" | |
"os" | |
convoy "github.com/frain-dev/convoy-go" | |
"github.com/frain-dev/convoy-go/models" | |
"github.com/jackc/pgx/v4/pgxpool" | |
) | |
const ( | |
orgID = "f38d5014-efb2-4766-9b49-a69eec6d86c3" | |
) | |
// This script depends on four environment variables | |
// - CONVOY_URL, CONVOY_API_USERNAME, CONVOY_API_PASSWORD & DATABASE_URL | |
func main() { | |
dbPool := getConnection() | |
defer dbPool.Close() | |
client := convoy.New() | |
rows, _ := dbPool.Query(context.Background(), "select id, name, web_hook_url, web_hook_token from merchants") | |
for rows.Next() { | |
var id, name, url, token sql.NullString | |
err := rows.Scan(&id, &name, &url, &token) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Name: %s, Url: %s, Token: %s\n", name.String, url.String, token.String) | |
// save to Convoy. | |
app, err := client.CreateApp(&models.ApplicationRequest{ | |
OrgID: orgID, | |
AppName: name.String, | |
Secret: token.String, | |
}) | |
// TODO: Save ConvoyID | |
_, err = dbPool.Exec(context.Background(), "update merchants set convoy_id=$1 where id=$2", app.UID, id) | |
if err != nil { | |
log.Fatal("Failed to update ConvoyID") | |
} | |
if err != nil { | |
log.Fatal("failed to create app \n", err) | |
} | |
log.Printf("\nApp created - %+v\n", app) | |
if url.Valid { | |
endpoint, err := client.CreateAppEndpoint(app.UID, &models.EndpointRequest{ | |
URL: url.String, | |
Description: name.String + "'s default endpoint", | |
}) | |
// TODO: Save EndpointID | |
_, err = dbPool.Exec(context.Background(), "update merchants set endpoint_id=$1 where id=$2", endpoint.UID, id) | |
if err != nil { | |
log.Fatal("failed to update EndpointID") | |
} | |
if err != nil { | |
log.Fatal("failed to create app endpoint \n", err) | |
} | |
log.Printf("\nApp endpoint created - %+v\n", endpoint) | |
} | |
} | |
} | |
func getConnection() *pgxpool.Pool { | |
pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL")) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err) | |
} | |
return pool | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment