Skip to content

Commit

Permalink
chore: remove unnecessary config load
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Feb 18, 2025
1 parent 37b822a commit af5f928
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 72 deletions.
4 changes: 0 additions & 4 deletions internal/db/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/supabase/cli/internal/db/start"
"github.com/supabase/cli/internal/gen/keys"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
"github.com/supabase/cli/pkg/parser"
)
Expand All @@ -33,9 +32,6 @@ type DiffFunc func(context.Context, string, string, []string) (string, error)

func Run(ctx context.Context, schema []string, file string, config pgconn.Config, differ DiffFunc, fsys afero.Fs, options ...func(*pgx.ConnConfig)) (err error) {
// Sanity checks.
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if utils.IsLocalDatabase(config) {
if container, err := createShadowIfNotExists(ctx, fsys); err != nil {
return err
Expand Down
21 changes: 2 additions & 19 deletions internal/db/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/supabase/cli/internal/testing/fstest"
"github.com/supabase/cli/internal/testing/helper"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/config"
"github.com/supabase/cli/pkg/migration"
"github.com/supabase/cli/pkg/pgtest"
Expand All @@ -39,9 +40,7 @@ func TestRun(t *testing.T) {
t.Run("runs migra diff", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
project := apitest.RandomProjectRef()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644))
require.NoError(t, flags.LoadConfig(fsys))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
Expand Down Expand Up @@ -85,22 +84,9 @@ func TestRun(t *testing.T) {
assert.Equal(t, []byte(diff), contents)
})

t.Run("throws error on malformed config", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ConfigPath, []byte("malformed"), 0644))
// Run test
err := Run(context.Background(), []string{"public"}, "", pgconn.Config{}, DiffSchemaMigra, fsys)
// Check error
assert.ErrorContains(t, err, "toml: expected = after a key, but the document ends there")
})

t.Run("throws error on failure to load user schemas", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
project := apitest.RandomProjectRef()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand All @@ -115,9 +101,6 @@ func TestRun(t *testing.T) {
t.Run("throws error on failure to diff target", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
project := apitest.RandomProjectRef()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
Expand Down
10 changes: 2 additions & 8 deletions internal/db/diff/pgadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/supabase/cli/internal/db/start"
"github.com/supabase/cli/internal/migration/new"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/config"
)

Expand All @@ -35,13 +34,8 @@ func SaveDiff(out, file string, fsys afero.Fs) error {

func RunPgAdmin(ctx context.Context, schema []string, file string, config pgconn.Config, fsys afero.Fs) error {
// Sanity checks.
{
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
}

if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
Expand Down
11 changes: 3 additions & 8 deletions internal/db/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/supabase/cli/internal/migration/new"
"github.com/supabase/cli/internal/migration/repair"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

Expand All @@ -35,25 +34,21 @@ var (
)

func Run(ctx context.Context, schema []string, config pgconn.Config, name string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
// 1. Sanity checks.
if err := flags.LoadConfig(fsys); err != nil {
return err
}
// 2. Check postgres connection
// 1. Check postgres connection
conn, err := utils.ConnectByConfig(ctx, config, options...)
if err != nil {
return err
}
defer conn.Close(context.Background())
// 3. Pull schema
// 2. Pull schema
timestamp := utils.GetCurrentTimestamp()
path := new.GetMigrationPath(timestamp, name)
if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
return run(p, ctx, schema, path, conn, fsys)
}); err != nil {
return err
}
// 4. Insert a row to `schema_migrations`
// 3. Insert a row to `schema_migrations`
fmt.Fprintln(os.Stderr, "Schema written to "+utils.Bold(path))
if shouldUpdate, err := utils.NewConsole().PromptYesNo(ctx, "Update remote migration history table?", true); err != nil {
return err
Expand Down
12 changes: 0 additions & 12 deletions internal/db/pull/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,9 @@ var dbConfig = pgconn.Config{
}

func TestPullCommand(t *testing.T) {
t.Run("throws error on malformed config", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ConfigPath, []byte("malformed"), 0644))
// Run test
err := Run(context.Background(), nil, pgconn.Config{}, "", fsys)
// Check error
assert.ErrorContains(t, err, "toml: expected = after a key, but the document ends there")
})

t.Run("throws error on connect failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
// Run test
err := Run(context.Background(), nil, pgconn.Config{}, "", fsys)
// Check error
Expand All @@ -53,7 +42,6 @@ func TestPullCommand(t *testing.T) {
t.Run("throws error on sync failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand Down
7 changes: 0 additions & 7 deletions internal/db/remote/changes/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,17 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/db/diff"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

var output string

func Run(ctx context.Context, schema []string, config pgconn.Config, fsys afero.Fs) error {
// Sanity checks.
if err := flags.LoadConfig(fsys); err != nil {
return err
}

if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
return run(p, ctx, schema, config, fsys)
}); err != nil {
return err
}

return diff.SaveDiff(output, "", fsys)
}

Expand Down
7 changes: 0 additions & 7 deletions internal/db/remote/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@ import (
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/migration/repair"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

func Run(ctx context.Context, schema []string, config pgconn.Config, fsys afero.Fs) error {
// Sanity checks.
if err := flags.LoadConfig(fsys); err != nil {
return err
}

if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
return run(p, ctx, schema, config, fsys)
}); err != nil {
return err
}

fmt.Println("Finished " + utils.Aqua("supabase db remote commit") + ".")
return nil
}
Expand Down
4 changes: 0 additions & 4 deletions internal/migration/squash/squash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/migration/repair"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

Expand All @@ -35,9 +34,6 @@ func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.F
return err
}
}
if err := flags.LoadConfig(fsys); err != nil {
return err
}
// 1. Squash local migrations
if err := squashToVersion(ctx, version, fsys, options...); err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions internal/migration/squash/squash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/supabase/cli/internal/testing/fstest"
"github.com/supabase/cli/internal/testing/helper"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
"github.com/supabase/cli/pkg/pgtest"
)
Expand All @@ -43,7 +44,7 @@ func TestSquashCommand(t *testing.T) {
t.Run("squashes local migrations", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
require.NoError(t, flags.LoadConfig(fsys))
paths := []string{
filepath.Join(utils.MigrationsDir, "0_init.sql"),
filepath.Join(utils.MigrationsDir, "1_target.sql"),
Expand Down Expand Up @@ -108,7 +109,6 @@ func TestSquashCommand(t *testing.T) {
t.Run("baselines migration history", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
path := filepath.Join(utils.MigrationsDir, "0_init.sql")
sql := "create schema test"
require.NoError(t, afero.WriteFile(fsys, path, []byte(sql), 0644))
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestSquashCommand(t *testing.T) {
assert.ErrorIs(t, err, repair.ErrInvalidVersion)
})

t.Run("throws error on missing config", func(t *testing.T) {
t.Run("throws error on missing migration", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
Expand Down Expand Up @@ -241,6 +241,7 @@ func TestSquashMigrations(t *testing.T) {
t.Run("throws error on shadow migrate failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, flags.LoadConfig(fsys))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
Expand Down

0 comments on commit af5f928

Please sign in to comment.