mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
terminal: thread a context into postgresDatabaseExists
This commit is contained in:
parent
6d2bf233e8
commit
42c3105537
3 changed files with 26 additions and 12 deletions
18
config.go
18
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)
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue