diff --git a/apps/cli-go/cmd/gen.go b/apps/cli-go/cmd/gen.go index c9d2510f5e..24a632d6a9 100644 --- a/apps/cli-go/cmd/gen.go +++ b/apps/cli-go/cmd/gen.go @@ -96,7 +96,8 @@ var ( return err } } - return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, swiftAccessControl.Value, queryTimeout, afero.NewOsFs()) + useDirectDBURL := cmd.Flags().Changed("db-url") + return types.Run(ctx, flags.ProjectRef, flags.DbConfig, lang.Value, schema, postgrestV9Compat, swiftAccessControl.Value, queryTimeout, afero.NewOsFs(), useDirectDBURL) }, Example: ` supabase gen types --local supabase gen types --linked --lang=go diff --git a/apps/cli-go/internal/gen/types/types.go b/apps/cli-go/internal/gen/types/types.go index c12a7b58eb..624e66c179 100644 --- a/apps/cli-go/internal/gen/types/types.go +++ b/apps/cli-go/internal/gen/types/types.go @@ -33,7 +33,7 @@ const ( SwiftInternalAccessControl = "internal" ) -func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang string, schemas []string, postgrestV9Compat bool, swiftAccessControl string, queryTimeout time.Duration, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error { +func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang string, schemas []string, postgrestV9Compat bool, swiftAccessControl string, queryTimeout time.Duration, fsys afero.Fs, useDirectDBURL bool, options ...func(*pgx.ConnConfig)) error { originalURL := utils.ToPostgresURL(dbConfig) // Add default schemas if --schema flag is not specified if len(schemas) == 0 { @@ -61,7 +61,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang str } hostConfig := container.HostConfig{} - if utils.IsLocalDatabase(dbConfig) { + if !useDirectDBURL && utils.IsLocalDatabase(dbConfig) { if err := utils.AssertSupabaseDbIsRunning(); err != nil { return err } diff --git a/apps/cli-go/internal/gen/types/types_test.go b/apps/cli-go/internal/gen/types/types_test.go index e7134b1820..7d073ea3ae 100644 --- a/apps/cli-go/internal/gen/types/types_test.go +++ b/apps/cli-go/internal/gen/types/types_test.go @@ -31,7 +31,7 @@ func TestGenLocalCommand(t *testing.T) { Password: "password", } - t.Run("generates typescript types", func(t *testing.T) { + t.Run("generates typescript types", func(t *testing.T) { const containerId = "test-pgmeta" imageUrl := utils.GetRegistryImageUrl(utils.Config.Studio.PgmetaImage) // Setup in-memory fs @@ -49,11 +49,30 @@ func TestGenLocalCommand(t *testing.T) { conn := pgtest.NewConn() defer conn.Close(t) // Run test - assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys, conn.Intercept)) + assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys, false, conn.Intercept)) // Validate api assert.Empty(t, apitest.ListUnmatchedRequests()) }) + t.Run("skips local db check when using direct db url", func(t *testing.T) { + const containerId = "test-pgmeta" + imageUrl := utils.GetRegistryImageUrl(utils.Config.Studio.PgmetaImage) + // Setup in-memory fs + fsys := afero.NewMemMapFs() + // Setup mock docker + require.NoError(t, apitest.MockDocker(utils.Docker)) + defer gock.OffAll() + apitest.MockDockerStart(utils.Docker, imageUrl, containerId) + require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "hello world\n")) + // Setup mock postgres + conn := pgtest.NewConn() + defer conn.Close(t) + // Run test + assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys, true, conn.Intercept)) + // Validate api + assert.Empty(t, apitest.ListUnmatchedRequests()) + }) + t.Run("throws error when db is not started", func(t *testing.T) { // Setup in-memory fs fsys := afero.NewMemMapFs() @@ -64,7 +83,7 @@ func TestGenLocalCommand(t *testing.T) { Get("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId). Reply(http.StatusServiceUnavailable) // Run test - assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys)) + assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys, false)) // Validate api assert.Empty(t, apitest.ListUnmatchedRequests()) }) @@ -84,7 +103,7 @@ func TestGenLocalCommand(t *testing.T) { Get("/v" + utils.Docker.ClientVersion() + "/images"). Reply(http.StatusServiceUnavailable) // Run test - assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys)) + assert.Error(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{}, true, "", time.Second, fsys, false)) // Validate api assert.Empty(t, apitest.ListUnmatchedRequests()) }) @@ -107,7 +126,7 @@ func TestGenLocalCommand(t *testing.T) { conn := pgtest.NewConn() defer conn.Close(t) // Run test - assert.NoError(t, Run(context.Background(), "", dbConfig, LangSwift, []string{}, true, SwiftInternalAccessControl, time.Second, fsys, conn.Intercept)) + assert.NoError(t, Run(context.Background(), "", dbConfig, LangSwift, []string{}, true, SwiftInternalAccessControl, time.Second, fsys, false, conn.Intercept)) // Validate api assert.Empty(t, apitest.ListUnmatchedRequests()) }) @@ -130,7 +149,7 @@ func TestGenLinkedCommand(t *testing.T) { Reply(200). JSON(api.TypescriptResponse{Types: ""}) // Run test - assert.NoError(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys)) + assert.NoError(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys, false)) // Validate api assert.Empty(t, apitest.ListUnmatchedRequests()) }) @@ -145,7 +164,7 @@ func TestGenLinkedCommand(t *testing.T) { Get("/v1/projects/" + projectId + "/types/typescript"). ReplyError(errNetwork) // Run test - err := Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys) + err := Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys, false) // Validate api assert.ErrorIs(t, err, errNetwork) assert.Empty(t, apitest.ListUnmatchedRequests()) @@ -160,7 +179,7 @@ func TestGenLinkedCommand(t *testing.T) { Get("/v1/projects/" + projectId + "/types/typescript"). Reply(http.StatusServiceUnavailable) // Run test - assert.Error(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys)) + assert.Error(t, Run(context.Background(), projectId, pgconn.Config{}, LangTypescript, []string{}, true, "", time.Second, fsys, false)) }) } @@ -185,7 +204,7 @@ func TestGenRemoteCommand(t *testing.T) { conn := pgtest.NewConn() defer conn.Close(t) // Run test - assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{"public"}, true, "", time.Second, afero.NewMemMapFs(), conn.Intercept)) + assert.NoError(t, Run(context.Background(), "", dbConfig, LangTypescript, []string{"public"}, true, "", time.Second, afero.NewMemMapFs(), false, conn.Intercept)) // Validate api assert.Empty(t, apitest.ListUnmatchedRequests()) })