mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-14 12:02:27 +08:00
Merge pull request #3056 from CoolCoolTomato/fix/postgres-dsn-dbname
fix(setup): bootstrap postgres connection with maintenance db
This commit is contained in:
@@ -160,13 +160,23 @@ func NeedsSetup() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildPostgresDSN(cfg *DatabaseConfig, dbName string) string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
|
||||||
|
cfg.Host, cfg.Port, cfg.User, cfg.Password, dbName, cfg.SSLMode,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildDatabaseConnectionDSNs(cfg *DatabaseConfig) (bootstrapDSN, targetDSN string) {
|
||||||
|
return buildPostgresDSN(cfg, "postgres"), buildPostgresDSN(cfg, cfg.DBName)
|
||||||
|
}
|
||||||
|
|
||||||
// TestDatabaseConnection tests the database connection and creates database if not exists
|
// TestDatabaseConnection tests the database connection and creates database if not exists
|
||||||
func TestDatabaseConnection(cfg *DatabaseConfig) error {
|
func TestDatabaseConnection(cfg *DatabaseConfig) error {
|
||||||
// First, connect to the default 'postgres' database to check/create target database
|
// First, connect to the default 'postgres' database to check/create target database.
|
||||||
defaultDSN := fmt.Sprintf(
|
// Connecting to cfg.DBName here fails when the target database has not been
|
||||||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
|
// created yet, so the bootstrap connection must use PostgreSQL's maintenance DB.
|
||||||
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode,
|
defaultDSN, targetDSN := buildDatabaseConnectionDSNs(cfg)
|
||||||
)
|
|
||||||
|
|
||||||
db, err := sql.Open("postgres", defaultDSN)
|
db, err := sql.Open("postgres", defaultDSN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -214,11 +224,6 @@ func TestDatabaseConnection(cfg *DatabaseConfig) error {
|
|||||||
}
|
}
|
||||||
db = nil
|
db = nil
|
||||||
|
|
||||||
targetDSN := fmt.Sprintf(
|
|
||||||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
|
|
||||||
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode,
|
|
||||||
)
|
|
||||||
|
|
||||||
targetDB, err := sql.Open("postgres", targetDSN)
|
targetDB, err := sql.Open("postgres", targetDSN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to connect to database '%s': %w", cfg.DBName, err)
|
return fmt.Errorf("failed to connect to database '%s': %w", cfg.DBName, err)
|
||||||
|
|||||||
@@ -87,3 +87,26 @@ func TestWriteConfigFileKeepsDefaultUserConcurrency(t *testing.T) {
|
|||||||
t.Fatalf("config missing default user concurrency, got:\n%s", string(data))
|
t.Fatalf("config missing default user concurrency, got:\n%s", string(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildDatabaseConnectionDSNsUsesPostgresForBootstrap(t *testing.T) {
|
||||||
|
cfg := &DatabaseConfig{
|
||||||
|
Host: "db",
|
||||||
|
Port: 5432,
|
||||||
|
User: "sub2api",
|
||||||
|
Password: "secret",
|
||||||
|
DBName: "sub2api",
|
||||||
|
SSLMode: "disable",
|
||||||
|
}
|
||||||
|
|
||||||
|
bootstrapDSN, targetDSN := buildDatabaseConnectionDSNs(cfg)
|
||||||
|
|
||||||
|
if !strings.Contains(bootstrapDSN, "dbname=postgres") {
|
||||||
|
t.Fatalf("bootstrap DSN = %q, want default postgres database", bootstrapDSN)
|
||||||
|
}
|
||||||
|
if strings.Contains(bootstrapDSN, "dbname=sub2api") {
|
||||||
|
t.Fatalf("bootstrap DSN = %q, should not connect to target database before checking/creating it", bootstrapDSN)
|
||||||
|
}
|
||||||
|
if !strings.Contains(targetDSN, "dbname=sub2api") {
|
||||||
|
t.Fatalf("target DSN = %q, want configured database", targetDSN)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user