Skip to content

Commit

Permalink
chore: clean up db pull implementation (#2175)
Browse files Browse the repository at this point in the history
* chore: clean up db pull implementation

* chore: update unit tests
  • Loading branch information
sweatybridge authored May 8, 2024
1 parent eaa23f6 commit 2647536
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 107 deletions.
3 changes: 0 additions & 3 deletions internal/db/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ 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 := utils.AssertDockerIsRunning(ctx); err != nil {
return err
}
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
Expand Down
59 changes: 0 additions & 59 deletions internal/db/pull/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pull
import (
"context"
"errors"
"net/http"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -41,41 +40,9 @@ var escapedSchemas = []string{
}

func TestPullCommand(t *testing.T) {
t.Run("throws error on missing docker", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Head("/_ping").
ReplyError(errors.New("network error"))
gock.New(utils.Docker.DaemonHost()).
Get("/_ping").
ReplyError(errors.New("network error"))
// Run test
err := Run(context.Background(), nil, pgconn.Config{}, "", fsys)
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("throws error on missing config", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Head("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
gock.New(utils.Docker.DaemonHost()).
Get("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
// Run test
err := Run(context.Background(), nil, pgconn.Config{}, "", fsys)
// Check error
Expand All @@ -87,19 +54,6 @@ func TestPullCommand(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Head("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
gock.New(utils.Docker.DaemonHost()).
Get("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
// Run test
err := Run(context.Background(), nil, pgconn.Config{}, "", fsys)
// Check error
Expand All @@ -111,19 +65,6 @@ func TestPullCommand(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Head("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
gock.New(utils.Docker.DaemonHost()).
Get("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
Expand Down
9 changes: 2 additions & 7 deletions internal/db/remote/changes/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ var output string

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

if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
Expand Down
9 changes: 2 additions & 7 deletions internal/db/remote/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ import (

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

if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error {
Expand Down
10 changes: 0 additions & 10 deletions internal/db/start/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,6 @@ func TestStartCommand(t *testing.T) {
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Head("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/").
Reply(http.StatusOK).
Expand All @@ -219,11 +214,6 @@ func TestStartCommand(t *testing.T) {
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Head("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/").
Reply(http.StatusNotFound)
Expand Down
10 changes: 0 additions & 10 deletions internal/start/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ func TestStartCommand(t *testing.T) {
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New("http:///var/run/docker.sock").
Head("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
gock.New(utils.Docker.DaemonHost()).
Get("/_ping").
Reply(http.StatusOK).
SetHeader("API-Version", utils.Docker.ClientVersion()).
SetHeader("OSType", "linux")
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers").
Reply(http.StatusOK).
Expand Down
11 changes: 0 additions & 11 deletions internal/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ func NewDocker() *client.Client {
return cli.Client().(*client.Client)
}

func AssertDockerIsRunning(ctx context.Context) error {
if _, err := Docker.Ping(ctx); err != nil {
if client.IsErrConnectionFailed(err) {
CmdSuggestion = suggestDockerInstall
}
return errors.Errorf("failed to ping docker daemon: %w", err)
}

return nil
}

const (
CliProjectLabel = "com.supabase.cli.project"
composeProjectLabel = "com.docker.compose.project"
Expand Down

0 comments on commit 2647536

Please sign in to comment.