Skip to content

Commit e20dad6

Browse files
committed
[FIX] PostgreSQL support.
1 parent 5072c3b commit e20dad6

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

internal/engine/install/engine.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,14 +1750,7 @@ func statusLevelForKey(status domain.SystemStatus, key string) (domain.StatusLev
17501750
return domain.StatusError, false
17511751
}
17521752

1753-
func testDatabaseConnection(ctx context.Context, workDir string, cfg dbConfig) (bool, string, error) {
1754-
raw, err := json.Marshal(cfg)
1755-
if err != nil {
1756-
return false, "", err
1757-
}
1758-
encoded := base64.StdEncoding.EncodeToString(raw)
1759-
1760-
script := `
1753+
const dbConnectionTestScript = `
17611754
$cfg = json_decode(base64_decode($argv[1] ?? ''), true);
17621755
if (!is_array($cfg)) { echo json_encode(["ok"=>false,"error"=>"Invalid config"]); exit(0); }
17631756
$type = $cfg["type"] ?? "mysql";
@@ -1782,7 +1775,7 @@ try {
17821775
echo json_encode(["ok"=>true]); exit(0);
17831776
}
17841777
$dsnNoDb = match($type) {
1785-
"pgsql" => "pgsql:host={$host};port={$port}",
1778+
"pgsql" => $port > 0 ? "pgsql:host={$host};port={$port};dbname=postgres" : "pgsql:host={$host};dbname=postgres",
17861779
"sqlsrv" => $port > 0 ? "sqlsrv:Server={$host},{$port}" : "sqlsrv:Server={$host}",
17871780
default => "mysql:host={$host};port={$port};charset=utf8mb4",
17881781
};
@@ -1807,6 +1800,15 @@ try {
18071800
}
18081801
`
18091802

1803+
func testDatabaseConnection(ctx context.Context, workDir string, cfg dbConfig) (bool, string, error) {
1804+
raw, err := json.Marshal(cfg)
1805+
if err != nil {
1806+
return false, "", err
1807+
}
1808+
encoded := base64.StdEncoding.EncodeToString(raw)
1809+
1810+
script := dbConnectionTestScript
1811+
18101812
cmd := exec.CommandContext(ctx, "php", "-r", script, encoded)
18111813
if strings.TrimSpace(workDir) != "" {
18121814
cmd.Dir = workDir

internal/engine/install/engine_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package install
22

3-
import "testing"
3+
import (
4+
"strings"
5+
"testing"
6+
)
47

58
func TestSanitizeAdminDir(t *testing.T) {
69
t.Parallel()
@@ -58,3 +61,11 @@ func TestCmpSemver(t *testing.T) {
5861
t.Fatalf("cmpSemver less=%d; want <0", got)
5962
}
6063
}
64+
65+
func TestDbConnectionTestScriptUsesPostgresMaintenanceDb(t *testing.T) {
66+
t.Parallel()
67+
68+
if !strings.Contains(dbConnectionTestScript, "dbname=postgres") {
69+
t.Fatalf("dbConnectionTestScript does not contain PostgreSQL maintenance dbname")
70+
}
71+
}

src/Commands/InstallCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,11 @@ protected function testDatabaseConnection(array $config): bool
474474
// For server-based databases, try to connect without database name first
475475
$configWithoutDb = $config;
476476
unset($configWithoutDb['name']);
477+
if ($type === 'pgsql') {
478+
// PostgreSQL requires a database name; otherwise it defaults to using the username.
479+
// Use a maintenance database for credential/host validation.
480+
$configWithoutDb['name'] = 'postgres';
481+
}
477482
$this->createConnection($configWithoutDb);
478483

479484
// If database name is provided, try to connect to it

src/Concerns/ConfiguresDatabase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ protected function createDatabase(array $config, string $collation): bool
183183
// MySQL / PostgreSQL
184184
$configWithoutDb = $config;
185185
unset($configWithoutDb['name']);
186+
if ($type === 'pgsql') {
187+
// PostgreSQL requires a database name; otherwise it defaults to using the username.
188+
// Use a maintenance database for CREATE DATABASE operations.
189+
$configWithoutDb['name'] = 'postgres';
190+
}
186191

187192
try {
188193
$dbh = $this->createConnection($configWithoutDb);
@@ -222,4 +227,3 @@ protected function createDatabase(array $config, string $collation): bool
222227
}
223228
}
224229
}
225-

0 commit comments

Comments
 (0)