diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b9417c0ae8..75661b7662 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,8 +78,8 @@ jobs: MYSQL_HOST: localhost MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }} MYSQL_ROOT_PASSWORD: mysecretpassword - DDL_SQLC_PROJECT_ID: ${{ secrets.DDL_SQLC_PROJECT_ID }} - DDL_SQLC_AUTH_TOKEN: ${{ secrets.DDL_SQLC_AUTH_TOKEN }} + CI_SQLC_PROJECT_ID: ${{ secrets.CI_SQLC_PROJECT_ID }} + CI_SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }} - name: build internal/endtoend run: go build ./... diff --git a/examples/authors/postgresql/db_test.go b/examples/authors/postgresql/db_test.go index 150c118614..d2df436ae3 100644 --- a/examples/authors/postgresql/db_test.go +++ b/examples/authors/postgresql/db_test.go @@ -8,25 +8,31 @@ import ( "database/sql" "testing" - "github.com/sqlc-dev/sqlc/internal/sqltest" + _ "github.com/lib/pq" + + "github.com/sqlc-dev/sqlc/internal/sqltest/hosted" ) func TestAuthors(t *testing.T) { - sdb, cleanup := sqltest.PostgreSQL(t, []string{"schema.sql"}) - defer cleanup() + uri := hosted.PostgreSQL(t, []string{"schema.sql"}) + db, err := sql.Open("postgres", uri) + if err != nil { + t.Fatal(err) + } + defer db.Close() ctx := context.Background() - db := New(sdb) + q := New(db) // list all authors - authors, err := db.ListAuthors(ctx) + authors, err := q.ListAuthors(ctx) if err != nil { t.Fatal(err) } t.Log(authors) // create an author - insertedAuthor, err := db.CreateAuthor(ctx, CreateAuthorParams{ + insertedAuthor, err := q.CreateAuthor(ctx, CreateAuthorParams{ Name: "Brian Kernighan", Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true}, }) @@ -36,7 +42,7 @@ func TestAuthors(t *testing.T) { t.Log(insertedAuthor) // get the author we just inserted - fetchedAuthor, err := db.GetAuthor(ctx, insertedAuthor.ID) + fetchedAuthor, err := q.GetAuthor(ctx, insertedAuthor.ID) if err != nil { t.Fatal(err) } diff --git a/examples/batch/postgresql/db_test.go b/examples/batch/postgresql/db_test.go index 9395e4c20c..fd3ce4e72b 100644 --- a/examples/batch/postgresql/db_test.go +++ b/examples/batch/postgresql/db_test.go @@ -8,14 +8,21 @@ import ( "testing" "time" - "github.com/sqlc-dev/sqlc/internal/sqltest" + "github.com/jackc/pgx/v4" + "github.com/sqlc-dev/sqlc/internal/sqltest/hosted" ) func TestBatchBooks(t *testing.T) { - db, cleanup := sqltest.PostgreSQLPgx(t, []string{"schema.sql"}) - defer cleanup() + uri := hosted.PostgreSQL(t, []string{"schema.sql"}) ctx := context.Background() + + db, err := pgx.Connect(ctx, uri) + if err != nil { + t.Fatal(err) + } + defer db.Close(ctx) + dq := New(db) // create an author diff --git a/examples/booktest/postgresql/db_test.go b/examples/booktest/postgresql/db_test.go index 440198d0e7..9716f56844 100644 --- a/examples/booktest/postgresql/db_test.go +++ b/examples/booktest/postgresql/db_test.go @@ -5,15 +5,22 @@ package booktest import ( "context" + "database/sql" "testing" "time" - "github.com/sqlc-dev/sqlc/internal/sqltest" + _ "github.com/lib/pq" + + "github.com/sqlc-dev/sqlc/internal/sqltest/hosted" ) func TestBooks(t *testing.T) { - db, cleanup := sqltest.PostgreSQL(t, []string{"schema.sql"}) - defer cleanup() + uri := hosted.PostgreSQL(t, []string{"schema.sql"}) + db, err := sql.Open("postgres", uri) + if err != nil { + t.Fatal(err) + } + defer db.Close() ctx := context.Background() dq := New(db) diff --git a/examples/ondeck/postgresql/db_test.go b/examples/ondeck/postgresql/db_test.go index fd16a49484..c4e4ce8bbf 100644 --- a/examples/ondeck/postgresql/db_test.go +++ b/examples/ondeck/postgresql/db_test.go @@ -5,11 +5,13 @@ package ondeck import ( "context" + "database/sql" "testing" - "github.com/sqlc-dev/sqlc/internal/sqltest" - "github.com/google/go-cmp/cmp" + _ "github.com/lib/pq" + + "github.com/sqlc-dev/sqlc/internal/sqltest/hosted" ) func runOnDeckQueries(t *testing.T, q *Queries) { @@ -124,10 +126,14 @@ func runOnDeckQueries(t *testing.T, q *Queries) { func TestPrepared(t *testing.T) { t.Parallel() - sdb, cleanup := sqltest.PostgreSQL(t, []string{"schema"}) - defer cleanup() + uri := hosted.PostgreSQL(t, []string{"schema"}) + db, err := sql.Open("postgres", uri) + if err != nil { + t.Fatal(err) + } + defer db.Close() - q, err := Prepare(context.Background(), sdb) + q, err := Prepare(context.Background(), db) if err != nil { t.Fatal(err) } @@ -138,8 +144,12 @@ func TestPrepared(t *testing.T) { func TestQueries(t *testing.T) { t.Parallel() - sdb, cleanup := sqltest.PostgreSQL(t, []string{"schema"}) - defer cleanup() + uri := hosted.PostgreSQL(t, []string{"schema"}) + db, err := sql.Open("postgres", uri) + if err != nil { + t.Fatal(err) + } + defer db.Close() - runOnDeckQueries(t, New(sdb)) + runOnDeckQueries(t, New(db)) } diff --git a/internal/endtoend/ddl_test.go b/internal/endtoend/ddl_test.go index 363ba8cbb5..33085926aa 100644 --- a/internal/endtoend/ddl_test.go +++ b/internal/endtoend/ddl_test.go @@ -8,7 +8,6 @@ import ( "path/filepath" "strings" "testing" - "time" "github.com/jackc/pgx/v5" @@ -22,9 +21,8 @@ import ( func TestValidSchema(t *testing.T) { ctx := context.Background() - projectID := os.Getenv("DDL_SQLC_PROJECT_ID") - authToken := os.Getenv("DDL_SQLC_AUTH_TOKEN") - + projectID := os.Getenv("CI_SQLC_PROJECT_ID") + authToken := os.Getenv("CI_SQLC_AUTH_TOKEN") if projectID == "" || authToken == "" { if os.Getenv("CI") == "" { t.Skip("skiping ddl tests outside of CI") @@ -78,6 +76,10 @@ func TestValidSchema(t *testing.T) { t.Run(fmt.Sprintf("endtoend-%s-%d", file, j), func(t *testing.T) { t.Parallel() + if strings.Contains(file, "pg_dump") { + t.Skip("loading pg_dump not supported") + } + var schema []string for _, path := range pkg.Schema { schema = append(schema, filepath.Join(filepath.Dir(file), path)) @@ -102,13 +104,11 @@ func TestValidSchema(t *testing.T) { sqls = append(sqls, migrations.RemoveRollbackStatements(before)) } - start := time.Now() resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ Engine: "postgresql", Region: "iad", Migrations: sqls, }) - t.Logf("%s", time.Since(start)) if err != nil { t.Fatal(err) } diff --git a/internal/sqltest/hosted/db.go b/internal/sqltest/hosted/db.go new file mode 100644 index 0000000000..c7829a607f --- /dev/null +++ b/internal/sqltest/hosted/db.go @@ -0,0 +1,78 @@ +package hosted + +import ( + "context" + "fmt" + "os" + "sync" + "testing" + + "github.com/sqlc-dev/sqlc/internal/quickdb" + pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1" + "github.com/sqlc-dev/sqlc/internal/sql/sqlpath" +) + +var client pb.QuickClient +var once sync.Once + +func initClient() error { + projectID := os.Getenv("CI_SQLC_PROJECT_ID") + authToken := os.Getenv("CI_SQLC_AUTH_TOKEN") + if projectID == "" || authToken == "" { + return fmt.Errorf("missing project id or auth token") + } + c, err := quickdb.NewClient(projectID, authToken) + if err != nil { + return err + } + client = c + return nil +} + +func PostgreSQL(t *testing.T, migrations []string) string { + ctx := context.Background() + t.Helper() + + once.Do(func() { + if err := initClient(); err != nil { + t.Fatal(err) + } + }) + + if client == nil { + t.Fatalf("client init failed") + } + + var seed []string + files, err := sqlpath.Glob(migrations) + if err != nil { + t.Fatal(err) + } + for _, f := range files { + blob, err := os.ReadFile(f) + if err != nil { + t.Fatal(err) + } + seed = append(seed, string(blob)) + } + + resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ + Engine: "postgresql", + Region: "iad", + Migrations: seed, + }) + if err != nil { + t.Fatal(err) + } + + t.Cleanup(func() { + _, err = client.DropEphemeralDatabase(ctx, &pb.DropEphemeralDatabaseRequest{ + DatabaseId: resp.DatabaseId, + }) + if err != nil { + t.Fatal(err) + } + }) + + return resp.Uri +}