From 2011a20f58f07b4f56254cdeaed8f3d437646fe6 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 20 Sep 2023 13:52:39 -0700 Subject: [PATCH 1/6] test(endtoend): Pull region from environment --- .github/workflows/ci.yml | 4 +++- internal/endtoend/ddl_test.go | 5 ++++- internal/sqltest/hosted/db.go | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75661b7662..c0ea403b5f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,7 +55,9 @@ jobs: with: go-version: '1.21.1' - - run: curl -I https://debug.fly.dev + - name: set sqlc region + run: | + curl -I -s -o foo.txt -w 'FLY_REGION=%header{fly-region}' https://debug.fly.dev >> "$GITHUB_ENV" - name: install gotestsum run: go install gotest.tools/gotestsum@latest diff --git a/internal/endtoend/ddl_test.go b/internal/endtoend/ddl_test.go index 33085926aa..e4a574eaad 100644 --- a/internal/endtoend/ddl_test.go +++ b/internal/endtoend/ddl_test.go @@ -23,6 +23,9 @@ func TestValidSchema(t *testing.T) { projectID := os.Getenv("CI_SQLC_PROJECT_ID") authToken := os.Getenv("CI_SQLC_AUTH_TOKEN") + + fmt.Println("region", os.Getenv("SQLC_REGION")) + if projectID == "" || authToken == "" { if os.Getenv("CI") == "" { t.Skip("skiping ddl tests outside of CI") @@ -106,7 +109,7 @@ func TestValidSchema(t *testing.T) { resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ Engine: "postgresql", - Region: "iad", + Region: os.Getenv("SQLC_REGION"), Migrations: sqls, }) if err != nil { diff --git a/internal/sqltest/hosted/db.go b/internal/sqltest/hosted/db.go index c7829a607f..9a380204e1 100644 --- a/internal/sqltest/hosted/db.go +++ b/internal/sqltest/hosted/db.go @@ -56,9 +56,10 @@ func PostgreSQL(t *testing.T, migrations []string) string { seed = append(seed, string(blob)) } + fmt.Println("region", os.Getenv("SQLC_REGION")) resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ Engine: "postgresql", - Region: "iad", + Region: os.Getenv("SQLC_REGION"), Migrations: seed, }) if err != nil { From 6e16b9f7d52d176ee36343399196c517d7f740da Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 20 Sep 2023 13:54:44 -0700 Subject: [PATCH 2/6] fix ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c0ea403b5f..ac0c2be3de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,7 +57,7 @@ jobs: - name: set sqlc region run: | - curl -I -s -o foo.txt -w 'FLY_REGION=%header{fly-region}' https://debug.fly.dev >> "$GITHUB_ENV" + curl -I -s -o foo.txt -w 'SQLC_REGION=%header{fly-region}' https://debug.fly.dev >> "$GITHUB_ENV" - name: install gotestsum run: go install gotest.tools/gotestsum@latest From d72ac19a89f8f37855d39380069937a89e5b5ac3 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 20 Sep 2023 15:05:22 -0700 Subject: [PATCH 3/6] Get region in code, not CI --- .github/workflows/ci.yml | 4 ---- internal/endtoend/ddl_test.go | 4 ++-- internal/quickdb/region.go | 19 +++++++++++++++++++ internal/sqltest/hosted/db.go | 4 ++-- 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 internal/quickdb/region.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac0c2be3de..6fe3470972 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,10 +55,6 @@ jobs: with: go-version: '1.21.1' - - name: set sqlc region - run: | - curl -I -s -o foo.txt -w 'SQLC_REGION=%header{fly-region}' https://debug.fly.dev >> "$GITHUB_ENV" - - name: install gotestsum run: go install gotest.tools/gotestsum@latest diff --git a/internal/endtoend/ddl_test.go b/internal/endtoend/ddl_test.go index e4a574eaad..875e3baca0 100644 --- a/internal/endtoend/ddl_test.go +++ b/internal/endtoend/ddl_test.go @@ -24,7 +24,7 @@ func TestValidSchema(t *testing.T) { projectID := os.Getenv("CI_SQLC_PROJECT_ID") authToken := os.Getenv("CI_SQLC_AUTH_TOKEN") - fmt.Println("region", os.Getenv("SQLC_REGION")) + fmt.Println("region", quickdb.GetClosestRegion()) if projectID == "" || authToken == "" { if os.Getenv("CI") == "" { @@ -109,7 +109,7 @@ func TestValidSchema(t *testing.T) { resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ Engine: "postgresql", - Region: os.Getenv("SQLC_REGION"), + Region: quickdb.GetClosestRegion(), Migrations: sqls, }) if err != nil { diff --git a/internal/quickdb/region.go b/internal/quickdb/region.go new file mode 100644 index 0000000000..bc03e3b3f7 --- /dev/null +++ b/internal/quickdb/region.go @@ -0,0 +1,19 @@ +package quickdb + +import ( + "net/http" + "sync" +) + +var region string +var once sync.Once + +func GetClosestRegion() string { + once.Do(func() { + resp, err := http.Get("https://debug.fly.dev") + if err == nil { + region = resp.Header.Get("Fly-Region") + } + }) + return region +} diff --git a/internal/sqltest/hosted/db.go b/internal/sqltest/hosted/db.go index 9a380204e1..976c479360 100644 --- a/internal/sqltest/hosted/db.go +++ b/internal/sqltest/hosted/db.go @@ -56,10 +56,10 @@ func PostgreSQL(t *testing.T, migrations []string) string { seed = append(seed, string(blob)) } - fmt.Println("region", os.Getenv("SQLC_REGION")) + fmt.Println("region", quickdb.GetClosestRegion()) resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ Engine: "postgresql", - Region: os.Getenv("SQLC_REGION"), + Region: quickdb.GetClosestRegion(), Migrations: seed, }) if err != nil { From 75e9daab028563dc44b879fd889717efbc32759a Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 20 Sep 2023 15:18:29 -0700 Subject: [PATCH 4/6] remove logging --- internal/endtoend/ddl_test.go | 2 -- internal/sqltest/hosted/db.go | 1 - 2 files changed, 3 deletions(-) diff --git a/internal/endtoend/ddl_test.go b/internal/endtoend/ddl_test.go index 875e3baca0..72be72c5c1 100644 --- a/internal/endtoend/ddl_test.go +++ b/internal/endtoend/ddl_test.go @@ -24,8 +24,6 @@ func TestValidSchema(t *testing.T) { projectID := os.Getenv("CI_SQLC_PROJECT_ID") authToken := os.Getenv("CI_SQLC_AUTH_TOKEN") - fmt.Println("region", quickdb.GetClosestRegion()) - if projectID == "" || authToken == "" { if os.Getenv("CI") == "" { t.Skip("skiping ddl tests outside of CI") diff --git a/internal/sqltest/hosted/db.go b/internal/sqltest/hosted/db.go index 976c479360..2d79c2b1ea 100644 --- a/internal/sqltest/hosted/db.go +++ b/internal/sqltest/hosted/db.go @@ -56,7 +56,6 @@ func PostgreSQL(t *testing.T, migrations []string) string { seed = append(seed, string(blob)) } - fmt.Println("region", quickdb.GetClosestRegion()) resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ Engine: "postgresql", Region: quickdb.GetClosestRegion(), From f11b9ad628476264c664060df26157201304aaf4 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 20 Sep 2023 15:34:16 -0700 Subject: [PATCH 5/6] include region in error --- internal/sqltest/hosted/db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/sqltest/hosted/db.go b/internal/sqltest/hosted/db.go index 2d79c2b1ea..4d2fe8dc84 100644 --- a/internal/sqltest/hosted/db.go +++ b/internal/sqltest/hosted/db.go @@ -62,7 +62,7 @@ func PostgreSQL(t *testing.T, migrations []string) string { Migrations: seed, }) if err != nil { - t.Fatal(err) + t.Fatalf("region %s: %s", quickdb.GetClosestRegion(), err) } t.Cleanup(func() { From f6fadb58c81cedf213b2e4a1b80457ca4a926733 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 20 Sep 2023 15:40:56 -0700 Subject: [PATCH 6/6] log other spot --- internal/endtoend/ddl_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/endtoend/ddl_test.go b/internal/endtoend/ddl_test.go index 72be72c5c1..123a503ce1 100644 --- a/internal/endtoend/ddl_test.go +++ b/internal/endtoend/ddl_test.go @@ -111,7 +111,7 @@ func TestValidSchema(t *testing.T) { Migrations: sqls, }) if err != nil { - t.Fatal(err) + t.Fatalf("region %s: %s", quickdb.GetClosestRegion(), err) } t.Cleanup(func() {