Skip to content

Commit

Permalink
feat: db reset skip seed flag (#2665)
Browse files Browse the repository at this point in the history
* wip

* feat: db reset --skip-seed flag

Add a  flag option to the  command
Closes #2534

* chore: apply suggestions from code review

Co-authored-by: Han Qiao <[email protected]>

* feat: add db.seed enabled config params

* chore: remove unused type

* Update db.go

* Apply suggestions from code review

---------

Co-authored-by: Han Qiao <[email protected]>
  • Loading branch information
avallete and sweatybridge authored Sep 11, 2024
1 parent 6003aae commit e60bc21
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,15 @@ var (
},
}

noSeed bool

dbResetCmd = &cobra.Command{
Use: "reset",
Short: "Resets the local database to current migrations",
RunE: func(cmd *cobra.Command, args []string) error {
if noSeed {
utils.Config.Db.Seed.Enabled = false
}
return reset.Run(cmd.Context(), migrationVersion, flags.DbConfig, afero.NewOsFs())
},
}
Expand Down Expand Up @@ -304,6 +309,7 @@ func init() {
resetFlags.String("db-url", "", "Resets the database specified by the connection string (must be percent-encoded).")
resetFlags.Bool("linked", false, "Resets the linked project with local migrations.")
resetFlags.Bool("local", true, "Resets the local database with local migrations.")
resetFlags.BoolVar(&noSeed, "no-seed", false, "Skip running the seed script after reset.")
dbResetCmd.MarkFlagsMutuallyExclusive("db-url", "linked", "local")
resetFlags.StringVar(&migrationVersion, "version", "", "Reset up to the specified version.")
dbCmd.AddCommand(dbResetCmd)
Expand Down
27 changes: 27 additions & 0 deletions internal/db/reset/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,33 @@ func TestResetRemote(t *testing.T) {
assert.NoError(t, err)
})

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")
require.NoError(t, afero.WriteFile(fsys, path, nil, 0644))
seedPath := filepath.Join(utils.SeedDataPath)
// Will raise an error when seeding
require.NoError(t, afero.WriteFile(fsys, seedPath, []byte("INSERT INTO test_table;"), 0644))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(migration.ListSchemas, escapedSchemas).
Reply("SELECT 1", []interface{}{"private"}).
Query("DROP SCHEMA IF EXISTS private CASCADE").
Reply("DROP SCHEMA").
Query(migration.DropObjects).
Reply("INSERT 0")
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, conn.Intercept)
// No error should be raised since we're skipping the seed
assert.NoError(t, err)
})

t.Run("throws error on connect failure", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
Expand Down
3 changes: 3 additions & 0 deletions internal/migration/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func MigrateAndSeed(ctx context.Context, version string, conn *pgx.Conn, fsys af
if err := migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys)); err != nil {
return err
}
if !utils.Config.Db.Seed.Enabled {
return nil
}
return SeedDatabase(ctx, conn, fsys)
}

Expand Down
24 changes: 24 additions & 0 deletions internal/migration/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ func TestMigrateDatabase(t *testing.T) {
assert.NoError(t, err)
})

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")
sql := "create schema public"
require.NoError(t, afero.WriteFile(fsys, path, []byte(sql), 0644))
seedPath := filepath.Join(utils.SeedDataPath)
// This will raise an error when seeding
require.NoError(t, afero.WriteFile(fsys, seedPath, []byte("INSERT INTO test_table;"), 0644))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
helper.MockMigrationHistory(conn).
Query(sql).
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)
// 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()))
})
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,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 +462,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 e60bc21

Please sign in to comment.