From 42c31055379e4fa9699dca69dd2a4223ae568a89 Mon Sep 17 00:00:00 2001 From: Viktor Torstensson Date: Tue, 16 Jun 2026 21:38:35 +0200 Subject: [PATCH] terminal: thread a context into postgresDatabaseExists --- config.go | 18 ++++++++++++------ config_test.go | 18 +++++++++++++----- terminal.go | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index 6ff2b635..fa39c6bc 100644 --- a/config.go +++ b/config.go @@ -578,7 +578,9 @@ func defaultConfig() *Config { // loadAndValidateConfig loads the terminal's main configuration and validates // its content. -func loadAndValidateConfig(interceptor signal.Interceptor) (*Config, error) { +func loadAndValidateConfig(ctx context.Context, + interceptor signal.Interceptor) (*Config, error) { + // Start with the default configuration. preCfg := defaultConfig() @@ -729,7 +731,7 @@ func loadAndValidateConfig(interceptor signal.Interceptor) (*Config, error) { ) } - err = validateExclusiveSQLBackends(cfg, litDir) + err = validateExclusiveSQLBackends(ctx, cfg, litDir) if err != nil { return nil, err } @@ -1160,7 +1162,9 @@ func readAutoMigrateKVDB(config *Config) error { // inactive SQL backend still has data at its default location. This prevents // silently switching to a different SQL store and starting against an empty // database. -func validateExclusiveSQLBackends(cfg *Config, litDir string) error { +func validateExclusiveSQLBackends(ctx context.Context, cfg *Config, + litDir string) error { + switch cfg.DatabaseBackend { case DatabaseBackendPostgres: sqlitePath := filepath.Join( @@ -1184,7 +1188,7 @@ func validateExclusiveSQLBackends(cfg *Config, litDir string) error { } case DatabaseBackendSqlite: - exists, err := postgresDatabaseExists(cfg.Postgres) + exists, err := postgresDatabaseExists(ctx, cfg.Postgres) if err != nil { return fmt.Errorf("unable to check for existing "+ "postgres database %q at %s:%d for user %q. "+ @@ -1228,7 +1232,9 @@ func sqliteDatabaseExists(path string) (bool, error) { // postgresDatabaseExists reports whether a Postgres database can be reached // with the configured connection info. If the configuration does not identify // a concrete database, the check is skipped. -func postgresDatabaseExists(cfg *db.PostgresConfig) (bool, error) { +func postgresDatabaseExists(ctx context.Context, + cfg *db.PostgresConfig) (bool, error) { + if !hasPostgresConnectionInfo(cfg) { return false, nil } @@ -1240,7 +1246,7 @@ func postgresDatabaseExists(cfg *db.PostgresConfig) (bool, error) { } defer dbConn.Close() - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() err = dbConn.PingContext(ctx) diff --git a/config_test.go b/config_test.go index 03a7c6a4..0a5d2430 100644 --- a/config_test.go +++ b/config_test.go @@ -36,7 +36,7 @@ func TestBlockStartupForPostgresIfSqliteDBExists(t *testing.T) { Postgres: fixture.GetConfig(), } - err := validateExclusiveSQLBackends(cfg, litDir) + err := validateExclusiveSQLBackends(t.Context(), cfg, litDir) require.Error(t, err) require.Contains(t, err.Error(), "sqlite database file already exists") require.Contains(t, err.Error(), sqlitePath) @@ -57,7 +57,7 @@ func TestBlockStartupForSqliteIfPostgresDBExists(t *testing.T) { Postgres: fixture.GetConfig(), } - err := validateExclusiveSQLBackends(cfg, "") + err := validateExclusiveSQLBackends(t.Context(), cfg, "") require.Error(t, err) require.Contains(t, err.Error(), "postgres database") require.Contains(t, err.Error(), cfg.Postgres.DBName) @@ -70,7 +70,9 @@ func TestDontBlockSqliteOnlyStartup(t *testing.T) { DatabaseBackend: DatabaseBackendSqlite, } - require.NoError(t, validateExclusiveSQLBackends(cfg, "")) + require.NoError( + t, validateExclusiveSQLBackends(t.Context(), cfg, ""), + ) } // TestDontBlockSqliteStartupIfConfiguredPostgresDoesntExist verifies that a @@ -92,7 +94,9 @@ func TestDontBlockSqliteStartupIfConfiguredPostgresDoesntExist(t *testing.T) { Postgres: pgCfg, } - require.NoError(t, validateExclusiveSQLBackends(cfg, "")) + require.NoError( + t, validateExclusiveSQLBackends(t.Context(), cfg, ""), + ) // We also validate that the validateExclusiveSQLBackends passed because // the db with the configured DBName doesn't exist and not because the @@ -139,5 +143,9 @@ func TestDontBlockPostgresOnlyStartup(t *testing.T) { Postgres: fixture.GetConfig(), } - require.NoError(t, validateExclusiveSQLBackends(cfg, litDir)) + require.NoError( + t, validateExclusiveSQLBackends( + t.Context(), cfg, litDir, + ), + ) } diff --git a/terminal.go b/terminal.go index 9fda836b..f4d01b65 100644 --- a/terminal.go +++ b/terminal.go @@ -289,7 +289,7 @@ func (g *LightningTerminal) Run(ctx context.Context) error { } }() - cfg, err := loadAndValidateConfig(shutdownInterceptor) + cfg, err := loadAndValidateConfig(ctx, shutdownInterceptor) if err != nil { return fmt.Errorf("could not load config: %w", err) }