Skip to content

Commit

Permalink
feat: add db.seed enabled config params
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Sep 11, 2024
1 parent f609084 commit b7a6b8e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
5 changes: 3 additions & 2 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ var (
Use: "reset",
Short: "Resets the local database to current migrations",
RunE: func(cmd *cobra.Command, args []string) error {
skipSeed, _ := cmd.Flags().GetBool("skip-seed")
return reset.Run(cmd.Context(), migrationVersion, flags.DbConfig, afero.NewOsFs(), skipSeed)
noSeed, _ := cmd.Flags().GetBool("no-seed")
utils.Config.Db.Seed.Enabled = !noSeed
return reset.Run(cmd.Context(), migrationVersion, flags.DbConfig, afero.NewOsFs())
},
}

Expand Down
24 changes: 12 additions & 12 deletions internal/db/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/supabase/cli/pkg/migration"
)

func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.Fs, skipSeed bool, options ...func(*pgx.ConnConfig)) error {
func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
if len(version) > 0 {
if _, err := strconv.Atoi(version); err != nil {
return errors.New(repair.ErrInvalidVersion)
Expand All @@ -43,14 +43,14 @@ func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.F
} else if !shouldReset {
return errors.New(context.Canceled)
}
return resetRemote(ctx, version, config, fsys, skipSeed, options...)
return resetRemote(ctx, version, config, fsys, options...)
}
// Config file is loaded before parsing --linked or --local flags
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
}
// Reset postgres database because extensions (pg_cron, pg_net) require postgres
if err := resetDatabase(ctx, version, fsys, skipSeed, options...); err != nil {
if err := resetDatabase(ctx, version, fsys, options...); err != nil {
return err
}
// Seed objects from supabase/buckets directory
Expand All @@ -67,12 +67,12 @@ func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.F
return nil
}

func resetDatabase(ctx context.Context, version string, fsys afero.Fs, skipSeed bool, options ...func(*pgx.ConnConfig)) error {
func resetDatabase(ctx context.Context, version string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
fmt.Fprintln(os.Stderr, "Resetting local database"+toLogMessage(version))
if utils.Config.Db.MajorVersion <= 14 {
return resetDatabase14(ctx, version, fsys, skipSeed, options...)
return resetDatabase14(ctx, version, fsys, options...)
}
return resetDatabase15(ctx, version, fsys, skipSeed, options...)
return resetDatabase15(ctx, version, fsys, options...)
}

func toLogMessage(version string) string {
Expand All @@ -82,7 +82,7 @@ func toLogMessage(version string) string {
return "..."
}

func resetDatabase14(ctx context.Context, version string, fsys afero.Fs, skipSeed bool, options ...func(*pgx.ConnConfig)) error {
func resetDatabase14(ctx context.Context, version string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
if err := recreateDatabase(ctx, options...); err != nil {
return err
}
Expand All @@ -102,10 +102,10 @@ func resetDatabase14(ctx context.Context, version string, fsys afero.Fs, skipSee
return err
}
}
return apply.MigrateAndSeed(ctx, version, conn, fsys, skipSeed)
return apply.MigrateAndSeed(ctx, version, conn, fsys)
}

func resetDatabase15(ctx context.Context, version string, fsys afero.Fs, skipSeed bool, options ...func(*pgx.ConnConfig)) error {
func resetDatabase15(ctx context.Context, version string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
if err := utils.Docker.ContainerRemove(ctx, utils.DbId, container.RemoveOptions{Force: true}); err != nil {
return errors.Errorf("failed to remove container: %w", err)
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func resetDatabase15(ctx context.Context, version string, fsys afero.Fs, skipSee
if err := start.SetupDatabase(ctx, conn, utils.DbId, os.Stderr, fsys); err != nil {
return err
}
if err := apply.MigrateAndSeed(ctx, version, conn, fsys, skipSeed); err != nil {
if err := apply.MigrateAndSeed(ctx, version, conn, fsys); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "Restarting containers...")
Expand Down Expand Up @@ -222,7 +222,7 @@ func listServicesToRestart() []string {
return []string{utils.StorageId, utils.GotrueId, utils.RealtimeId, utils.PoolerId}
}

func resetRemote(ctx context.Context, version string, config pgconn.Config, fsys afero.Fs, skipSeed bool, options ...func(*pgx.ConnConfig)) error {
func resetRemote(ctx context.Context, version string, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
fmt.Fprintln(os.Stderr, "Resetting remote database"+toLogMessage(version))
conn, err := utils.ConnectByConfigStream(ctx, config, io.Discard, options...)
if err != nil {
Expand All @@ -232,7 +232,7 @@ func resetRemote(ctx context.Context, version string, config pgconn.Config, fsys
if err := migration.DropUserSchemas(ctx, conn); err != nil {
return err
}
return apply.MigrateAndSeed(ctx, version, conn, fsys, skipSeed)
return apply.MigrateAndSeed(ctx, version, conn, fsys)
}

func LikeEscapeSchema(schemas []string) (result []string) {
Expand Down
19 changes: 10 additions & 9 deletions internal/db/reset/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestResetCommand(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := Run(context.Background(), "", pgconn.Config{Host: "db.supabase.co"}, fsys, false)
err := Run(context.Background(), "", pgconn.Config{Host: "db.supabase.co"}, fsys)
// Check error
assert.ErrorIs(t, err, context.Canceled)
})
Expand All @@ -52,7 +52,7 @@ func TestResetCommand(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := Run(context.Background(), "", pgconn.Config{Host: "db.supabase.co"}, fsys, false)
err := Run(context.Background(), "", pgconn.Config{Host: "db.supabase.co"}, fsys)
// Check error
assert.ErrorContains(t, err, "invalid port (outside range)")
})
Expand All @@ -67,7 +67,7 @@ func TestResetCommand(t *testing.T) {
Get("/v" + utils.Docker.ClientVersion() + "/containers").
Reply(http.StatusNotFound)
// Run test
err := Run(context.Background(), "", dbConfig, fsys, false)
err := Run(context.Background(), "", dbConfig, fsys)
// Check error
assert.ErrorIs(t, err, utils.ErrNotRunning)
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand All @@ -89,7 +89,7 @@ func TestResetCommand(t *testing.T) {
Delete("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId).
ReplyError(errors.New("network error"))
// Run test
err := Run(context.Background(), "", dbConfig, fsys, false)
err := Run(context.Background(), "", dbConfig, fsys)
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand Down Expand Up @@ -352,11 +352,11 @@ func TestResetRemote(t *testing.T) {
Query(migration.INSERT_MIGRATION_VERSION, "0", "schema", nil).
Reply("INSERT 0 1")
// Run test
err := resetRemote(context.Background(), "", dbConfig, fsys, false, conn.Intercept)
err := resetRemote(context.Background(), "", dbConfig, fsys, conn.Intercept)
// Check error
assert.NoError(t, err)
})
t.Run("resets remote database with skip-seed flag", func(t *testing.T) {
t.Run("resets remote database with seed config disabled", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
path := filepath.Join(utils.MigrationsDir, "0_schema.sql")
Expand All @@ -376,8 +376,9 @@ func TestResetRemote(t *testing.T) {
helper.MockMigrationHistory(conn).
Query(migration.INSERT_MIGRATION_VERSION, "0", "schema", nil).
Reply("INSERT 0 1")
utils.Config.Db.Seed.Enabled = false
// Run test
err := resetRemote(context.Background(), "", dbConfig, fsys, true, conn.Intercept)
err := resetRemote(context.Background(), "", dbConfig, fsys, conn.Intercept)
// No error should be raised since we're skipping the seed
assert.NoError(t, err)
})
Expand All @@ -386,7 +387,7 @@ func TestResetRemote(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Run test
err := resetRemote(context.Background(), "", pgconn.Config{}, fsys, false)
err := resetRemote(context.Background(), "", pgconn.Config{}, fsys)
// Check error
assert.ErrorContains(t, err, "invalid port (outside range)")
})
Expand All @@ -402,7 +403,7 @@ func TestResetRemote(t *testing.T) {
Query(migration.DropObjects).
ReplyError(pgerrcode.InsufficientPrivilege, "permission denied for relation supabase_migrations")
// Run test
err := resetRemote(context.Background(), "", dbConfig, fsys, false, conn.Intercept)
err := resetRemote(context.Background(), "", dbConfig, fsys, conn.Intercept)
// Check error
assert.ErrorContains(t, err, "ERROR: permission denied for relation supabase_migrations (SQLSTATE 42501)")
})
Expand Down
2 changes: 1 addition & 1 deletion internal/db/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func setupDatabase(ctx context.Context, fsys afero.Fs, w io.Writer, options ...f
if err := SetupDatabase(ctx, conn, utils.DbId, w, fsys); err != nil {
return err
}
return apply.MigrateAndSeed(ctx, "", conn, fsys, false)
return apply.MigrateAndSeed(ctx, "", conn, fsys)
}

func SetupDatabase(ctx context.Context, conn *pgx.Conn, host string, w io.Writer, fsys afero.Fs) error {
Expand Down
4 changes: 2 additions & 2 deletions internal/migration/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
"github.com/supabase/cli/pkg/migration"
)

func MigrateAndSeed(ctx context.Context, version string, conn *pgx.Conn, fsys afero.Fs, skipSeed bool) error {
func MigrateAndSeed(ctx context.Context, version string, conn *pgx.Conn, fsys afero.Fs) error {
migrations, err := list.LoadPartialMigrations(version, fsys)
if err != nil {
return err
}
if err := migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys)); err != nil {
return err
}
if skipSeed {
if !utils.Config.Db.Seed.Enabled {
return nil
}
return SeedDatabase(ctx, conn, fsys)
Expand Down
11 changes: 6 additions & 5 deletions internal/migration/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func TestMigrateDatabase(t *testing.T) {
Query(migration.INSERT_MIGRATION_VERSION, "0", "test", []string{sql}).
Reply("INSERT 0 1")
// Run test
err := MigrateAndSeed(context.Background(), "", conn.MockClient(t), fsys, false)
err := MigrateAndSeed(context.Background(), "", conn.MockClient(t), fsys)
// Check error
assert.NoError(t, err)
})

t.Run("skip seeding when skip-seed flag is set", func(t *testing.T) {
t.Run("skip seeding when seed config is disabled", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
path := filepath.Join(utils.MigrationsDir, "0_test.sql")
Expand All @@ -55,21 +55,22 @@ func TestMigrateDatabase(t *testing.T) {
Reply("CREATE SCHEMA").
Query(migration.INSERT_MIGRATION_VERSION, "0", "test", []string{sql}).
Reply("INSERT 0 1")
utils.Config.Db.Seed.Enabled = false
// Run test
err := MigrateAndSeed(context.Background(), "", conn.MockClient(t), fsys, true)
err := MigrateAndSeed(context.Background(), "", conn.MockClient(t), fsys)
// No error should be returned since seeding is skipped
assert.NoError(t, err)
})

t.Run("ignores empty local directory", func(t *testing.T) {
assert.NoError(t, MigrateAndSeed(context.Background(), "", nil, afero.NewMemMapFs(), false))
assert.NoError(t, MigrateAndSeed(context.Background(), "", nil, afero.NewMemMapFs()))
})

t.Run("throws error on open failure", func(t *testing.T) {
// Setup in-memory fs
fsys := &fstest.OpenErrorFs{DenyPath: utils.MigrationsDir}
// Run test
err := MigrateAndSeed(context.Background(), "", nil, fsys, false)
err := MigrateAndSeed(context.Background(), "", nil, fsys)
// Check error
assert.ErrorIs(t, err, os.ErrPermission)
})
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type CustomClaims struct {
jwt.RegisteredClaims
}

type DbSeed struct {
Enabled bool
}

const (
defaultJwtSecret = "super-secret-jwt-token-with-at-least-32-characters-long"
defaultJwtExpiry = 1983812996
Expand Down Expand Up @@ -160,6 +164,11 @@ type (
Password string `toml:"-"`
RootKey string `toml:"-" mapstructure:"root_key"`
Pooler pooler `toml:"pooler"`
Seed seed `toml:"seed"`
}

seed struct {
Enabled bool `toml:"enabled"`
}

pooler struct {
Expand Down Expand Up @@ -457,6 +466,9 @@ func NewConfig(editors ...ConfigEditor) config {
EncryptionKey: "12345678901234567890123456789032",
SecretKeyBase: "EAx3IQ/wRG1v47ZD4NE4/9RzBI8Jmil3x0yhcW4V2NHBP6c2iPIzwjofi2Ep4HIG",
},
Seed: seed{
Enabled: true,
},
},
Realtime: realtime{
Image: realtimeImage,
Expand Down

0 comments on commit b7a6b8e

Please sign in to comment.