Skip to content

Commit 075e524

Browse files
committed
only use a custom cache dir for built-in postgres when running with --ephemeral
1 parent 7ca935b commit 075e524

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

cli/server.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,16 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
413413
if !vals.InMemoryDatabase && vals.PostgresURL == "" {
414414
var closeFunc func() error
415415
cliui.Infof(inv.Stdout, "Using built-in PostgreSQL (%s)", config.PostgresPath())
416-
pgURL, closeFunc, err := startBuiltinPostgres(ctx, config, logger, cacheDir)
416+
customPostgresCacheDir := ""
417+
// By default, built-in PostgreSQL will use the Coder root directory
418+
// for its cache. However, when a deployment is ephemeral, the root
419+
// directory is wiped clean on shutdown, defeating the purpose of using
420+
// it as a cache. So here we use a cache directory that will not get
421+
// removed on restart.
422+
if vals.EphemeralDeployment.Value() {
423+
customPostgresCacheDir = cacheDir
424+
}
425+
pgURL, closeFunc, err := startBuiltinPostgres(ctx, config, logger, customPostgresCacheDir)
417426
if err != nil {
418427
return err
419428
}
@@ -1964,7 +1973,7 @@ func embeddedPostgresURL(cfg config.Root) (string, error) {
19641973
return fmt.Sprintf("postgres://coder@localhost:%s/coder?sslmode=disable&password=%s", pgPort, pgPassword), nil
19651974
}
19661975

1967-
func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logger, cacheDir string) (string, func() error, error) {
1976+
func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logger, customCacheDir string) (string, func() error, error) {
19681977
usr, err := user.Current()
19691978
if err != nil {
19701979
return "", nil, err
@@ -1992,8 +2001,8 @@ func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logg
19922001
}
19932002

19942003
cachePath := filepath.Join(cfg.PostgresPath(), "cache")
1995-
if cacheDir != "" {
1996-
cachePath = filepath.Join(cacheDir, "postgres")
2004+
if customCacheDir != "" {
2005+
cachePath = filepath.Join(customCacheDir, "postgres")
19972006
}
19982007
stdlibLogger := slog.Stdlib(ctx, logger.Named("postgres"), slog.LevelDebug)
19992008
ep := embeddedpostgres.NewDatabase(

0 commit comments

Comments
 (0)