Skip to content

Commit

Permalink
nix: improve dev workflow
Browse files Browse the repository at this point in the history
Solves PostgREST#5.

This is done by avoiding building the postgres extension inside the nix
derivation. Instead we build it with `make` on the current directory and
install it on postgres using the upcoming postgres
`extension_control_path` GUC.

This is taken from the patch on
https://www.postgresql.org/message-id/flat/E7C7BFFB-8857-48D4-A71F-88B359FADCFD%40justatheory.com
and backpatched to postgres 12, 13, 14, 15 and 16,

Thanks to this, it's now possible to do watch commands:

```
nxpg-watch nxpg-16 nxpg-build (rebuilds when changing sources)

nxpg-watch nxpg-13 nxpg-tmp nxpg-test (runs tests when changing sources)
```

Future improvements:

* Makes possible to enable code coverage, since the `.gcno` files will
  live outside the nix store.

* It's wasteful to rebuild the patched pgs everytime, later on this
  `nxpg` could live in another repo and benefit from Nix caching.

Additionally:

* removes clean_generated in favor of builtin EXTRA_CLEAN

* Don't hardcode sources in Makefile

* Update github action
  • Loading branch information
steve-chavez committed Feb 4, 2025
1 parent 676178d commit 91e2c82
Show file tree
Hide file tree
Showing 12 changed files with 2,560 additions and 79 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
with:
nix_path: nixpkgs=channel:nixos-unstable
- name: Run tests
run: nix-shell --run "with-pg-${{ matrix.pg-version }} make installcheck"
run: nix-shell --run "nxpg-${{ matrix.pg-version }} nxpg-tmp nxpg-test"
- if: ${{ failure() }}
run: cat output/regression.diffs
run: |
cat regression.out
cat regression.diffs
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ results/
.history
*.so
*.bc
*.gcda
*.gcno
tags
plmustache.control
sql/plmustache--*.sql
plmustache--*.sql
coverage.info
coverage_html
regression.diffs
regression.out
32 changes: 14 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
## Our variables
EXTENSION = plmustache
EXTVERSION = 0.1
SED ?= sed

DATA = $(wildcard sql/*--*.sql)
## PGXS variables
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)

MODULE_big = $(EXTENSION)
OBJS = src/plmustache.o src/observation.o src/build.o
SRC = $(wildcard src/*.c)
OBJS = $(patsubst src/%.c, src/%.o, $(SRC))
SHLIB_LINK = -lmustach
PG_CFLAGS = -std=c99 -Wno-declaration-after-statement -Wall -Werror -Wshadow

TESTS = $(wildcard test/sql/*.sql)
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
REGRESS_OPTS = --inputdir=test

PG_CONFIG = pg_config
SHLIB_LINK = -lmustach

PG_CFLAGS = -std=c99 -Wno-declaration-after-statement -Wall -Werror -Wshadow

PGXS := $(shell $(PG_CONFIG) --pgxs)

all: sql/$(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control
DATA = $(wildcard *--*.sql)

.PHONY: clean_generated
clean_generated:
rm -f $(EXTENSION).control
rm -f sql/$(EXTENSION)--$(EXTVERSION).sql
EXTRA_CLEAN = $(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control

# extra dep for clean target in pgxs.mk
clean: clean_generated
all: $(EXTENSION)--$(EXTVERSION).sql $(EXTENSION).control

sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
cp $< $@

$(EXTENSION).control:
sed "s/@EXTVERSION@/$(EXTVERSION)/g" $(EXTENSION).control.in > $(EXTENSION).control
$(SED) "s/@EXTVERSION@/$(EXTVERSION)/g" $(EXTENSION).control.in > $@

include $(PGXS)
85 changes: 85 additions & 0 deletions nix/nxpg.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{ writeShellScriptBin, findutils, entr, callPackage, postgresql_16, postgresql_15, postgresql_14, postgresql_13, postgresql_12 } :
let
prefix = "nxpg";
supportedPgs = [
postgresql_16
postgresql_15
postgresql_14
postgresql_13
postgresql_12
];
build =
writeShellScriptBin "${prefix}-build" ''
set -euo pipefail
make clean
make
'';
test =
writeShellScriptBin "${prefix}-test" ''
set -euo pipefail
make clean
make
make installcheck
'';

watch =
writeShellScriptBin "${prefix}-watch" ''
set -euo pipefail
${findutils}/bin/find . -type f \( -name '*.c' -o -name '*.h' \) | ${entr}/bin/entr -dr "$@"
'';

tmpDb =
writeShellScriptBin "${prefix}-tmp" ''
set -euo pipefail
export tmpdir="$(mktemp -d)"
export PGDATA="$tmpdir"
export PGHOST="$tmpdir"
export PGUSER=postgres
export PGDATABASE=postgres
trap 'pg_ctl stop -m i && rm -rf "$tmpdir"' sigint sigterm exit
PGTZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "$PGUSER"
# pg versions older than 16 don't support adding "-c" to initdb to add these options
# so we just modify the resulting postgresql.conf to avoid an error
echo "dynamic_library_path='\$libdir:$(pwd)'" >> $PGDATA/postgresql.conf
echo "extension_control_path='\$system:$(pwd)'" >> $PGDATA/postgresql.conf
default_options="-F -c listen_addresses=\"\" -k $PGDATA"
pg_ctl start -o "$default_options"
"$@"
'';
allPgPaths = map (pg:
let
ver = builtins.head (builtins.splitVersion pg.version);
patchedPg = pg.overrideAttrs(oldAttrs: {
patches = oldAttrs.patches ++ [
./patches/${ver}-add-extension_control_path-for.patch
];
});
script = ''
set -euo pipefail
export PATH=${patchedPg}/bin:"$PATH"
"$@"
'';
in
writeShellScriptBin "${prefix}-${ver}" script
) supportedPgs;
in
[
build
test
watch
tmpDb
allPgPaths
]
Loading

0 comments on commit 91e2c82

Please sign in to comment.