Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions internal/postgres/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"github.com/pgplex/pgschema/cmd/util"
)

// binariesPath is the path that contains the Postgres binaries.
// This is meant to be injected via ldflags (for example, to use the nix
// postgres versions when building with nix).
var binariesPath string

// PostgresVersion is an alias for the embedded-postgres version type.
type PostgresVersion = embeddedpostgres.PostgresVersion

Expand Down Expand Up @@ -91,14 +96,16 @@ func StartEmbeddedPostgres(config *EmbeddedPostgresConfig) (*EmbeddedPostgres, e
Password(config.Password).
Port(uint32(port)).
RuntimePath(runtimePath).
BinariesPath(binariesPath).
DataPath(filepath.Join(runtimePath, "data")).
Logger(io.Discard). // Suppress embedded-postgres startup logs
StartParameters(map[string]string{
"logging_collector": "off", // Disable log collector
"log_destination": "stderr", // Send logs to stderr (which we discard)
"log_min_messages": "PANIC", // Only log PANIC level messages
"log_statement": "none", // Don't log SQL statements
"log_min_duration_statement": "-1", // Don't log slow queries
"logging_collector": "off", // Disable log collector
"log_destination": "stderr", // Send logs to stderr (which we discard)
"log_min_messages": "PANIC", // Only log PANIC level messages
"log_statement": "none", // Don't log SQL statements
"log_min_duration_statement": "-1", // Don't log slow queries
"unix_socket_directories": runtimePath, // Use a directory that is guaranteed to exist
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added because the default directory Postgres uses to create the lockfiles may not exist. Using runtimePath ensures the directory exists before Postgres starts.

})

// Create and start PostgreSQL instance
Expand Down
5 changes: 4 additions & 1 deletion nix/pgschema.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{ pkgs ? import <nixpkgs> {}, rev ? "unknown", buildDate ? "unknown" }:
{ pkgs ? import <nixpkgs> {}, rev ? "unknown", buildDate ? "unknown", postgresql ? null }:

let
lib = pkgs.lib;
version = lib.strings.removeSuffix "\n" (builtins.readFile ../internal/version/VERSION);
postgres = if postgresql == null then pkgs.postgresql else postgresql;
in
pkgs.buildGoModule {
pname = "pgschema";
Expand Down Expand Up @@ -31,6 +32,8 @@ pkgs.buildGoModule {
"github.com/pgplex/pgschema/cmd.GitCommit=${rev}"
"-X"
"github.com/pgplex/pgschema/cmd.BuildDate=${buildDate}"
"-X"
"github.com/pgplex/pgschema/internal/postgres.binariesPath=${postgres}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Wrong binary directory The injected value points at the PostgreSQL package root, but embedded-postgres checks and runs pg_ctl under <binariesPath>/bin/pg_ctl. A Nix PostgreSQL package exposes pg_ctl from its bin output, so split-output packages can put the executable under ${postgresql.bin}/bin/pg_ctl rather than ${postgresql}/bin/pg_ctl. In that case the Nix-built pgschema still cannot start embedded Postgres and fails before planning.

Context Used: CLAUDE.md (source)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Pinned server mismatch This always injects the default Nix PostgreSQL package, while StartEmbeddedPostgres still selects an embedded-postgres version from the target database. If the target database is a different major version from the Nix package, the code asks embedded-postgres for one version but actually runs another version's initdb and postgres binaries. Planning against a PostgreSQL 14 or 15 target on a package set whose default is 17 can validate desired state with the wrong server version.

Context Used: CLAUDE.md (source)

];

meta = with lib; {
Expand Down