Skip to content

Commit

Permalink
Lint: script + zig formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
urso committed Mar 20, 2024
1 parent 0a5ef25 commit fe00b10
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
nix develop --command -- pre-commit run --all-files --color always
- name: Create local postgres installation
run: |
sudo chown $USER /run/postgresql
sudo chown "$USER" /run/postgresql
nix develop --command -- ./ci/setup.sh
- name: Build and run tests
run: |
Expand Down
122 changes: 60 additions & 62 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,108 +4,106 @@
set -o pipefail

test_pgzx() {
rc=0
run_unit_tests ./ || rc=1
extension_drop pgzx_unit
return $rc
rc=0
run_unit_tests ./ || rc=1
extension_drop pgzx_unit
return $rc
}

test_char_count_zig() {
extension_create char_count_zig
trap "extension_drop char_count_zig" INT TERM
extension_create char_count_zig
trap "extension_drop char_count_zig" INT TERM

rc=0
run_regression_tests ./examples/char_count_zig || rc=1
run_unit_tests ./examples/char_count_zig || rc=1
rc=0
run_regression_tests ./examples/char_count_zig || rc=1
run_unit_tests ./examples/char_count_zig || rc=1

extension_drop char_count_zig
return $rc
extension_drop char_count_zig
return $rc
}

test_pgaudit_zig() {
run_unit_tests ./examples/pgaudit_zig
run_unit_tests ./examples/pgaudit_zig
}


extension_build() {
cwd=$(pwd)
cd "$1" || return 1
zig build -freference-trace -p "$PG_HOME" || return 1
cd "$cwd" || return 1
cwd=$(pwd)
cd "$1" || return 1
zig build -freference-trace -p "$PG_HOME" || return 1
cd "$cwd" || return 1
}

extension_create() {
echo "Create extension $1"
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS $1"
echo "Create extension $1"
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS $1"
}

extension_drop() {
echo "Drop extension $1"
psql -U postgres -c "DROP EXTENSION IF EXISTS $1"
echo "Drop extension $1"
psql -U postgres -c "DROP EXTENSION IF EXISTS $1"
}

run_regression_tests() {
echo "Run regression tests"
echo "Run regression tests"

cwd=$(pwd)
cd "$1" || return 1
zig build pg_regress --verbose || return 1
cd "$cwd" || return 1
cwd=$(pwd)
cd "$1" || return 1
zig build pg_regress --verbose || return 1
cd "$cwd" || return 1
}

run_unit_tests() {
echo "Run unit tests"
echo "Run unit tests"

cwd=$(pwd)
cd "$1" || return 1
zig build -freference-trace -p "$PG_HOME" unit || return 1
cd "$cwd" || return 1
cwd=$(pwd)
cd "$1" || return 1
zig build -freference-trace -p "$PG_HOME" unit || return 1
cd "$cwd" || return 1
}

run_test_suites() {
for t in "$@"; do
echo "# Run $t"
if ! $t; then
return 1
fi
done
for t in "$@"; do
echo "# Run $t"
if ! $t; then
return 1
fi
done
}

fail () {
echo "$1" >&2
exit 1
fail() {
echo "$1" >&2
exit 1
}

main() {
echo "Build and install extension"
eval "$(pgenv)"

log_init_size=0;
if [ -f "$PG_CLUSTER_LOG_FILE" ]; then
log_init_size=$(stat -c %s "$PG_CLUSTER_LOG_FILE")
fi
echo "Server log size: $log_init_size"
echo "Build and install extension"
eval "$(pgenv)"

extension_build ./examples/char_count_zig || fail "Failed to build char_count_zig"
extension_build ./examples/pgaudit_zig || fail "Failed to build pgaudit_zig"
log_init_size=0
if [ -f "$PG_CLUSTER_LOG_FILE" ]; then
log_init_size=$(stat -c %s "$PG_CLUSTER_LOG_FILE")
fi
echo "Server log size: $log_init_size"

echo "Start PostgreSQL"
pgstart || fail "Failed to start PostgreSQL"
trap pgstop TERM INT EXIT
extension_build ./examples/char_count_zig || fail "Failed to build char_count_zig"
extension_build ./examples/pgaudit_zig || fail "Failed to build pgaudit_zig"

echo "Start PostgreSQL"
pgstart || fail "Failed to start PostgreSQL"
trap pgstop TERM INT EXIT

ok=true
run_test_suites test_pgzx test_char_count_zig test_pgaudit_zig || ok=false
ok=true
run_test_suites test_pgzx test_char_count_zig test_pgaudit_zig || ok=false

if ! $ok; then
printf "\n\nServer log:"
if ! $ok; then
printf "\n\nServer log:"

log_size=$(stat -c %s "$PG_CLUSTER_LOG_FILE")
tail -c $((log_size - log_init_size)) "$PG_CLUSTER_LOG_FILE"
fail "Regression tests failed"
fi
log_size=$(stat -c %s "$PG_CLUSTER_LOG_FILE")
tail -c $((log_size - log_init_size)) "$PG_CLUSTER_LOG_FILE"
fail "Regression tests failed"
fi

echo "Success!"
echo "Success!"
}

main
112 changes: 56 additions & 56 deletions dev/bin/pgbuild
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,44 @@ POSTGRES_BRANCH=${POSTGRES_BRANCH:-"REL_16_STABLE"}

POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-h)
echo "$USAGE"
exit 0
;;
--repo | -r)
POSTGRES_REPO="$2"
shift 2
;;
-b | --branch)
POSTGRES_BRANCH="$2"
shift 2
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
case $1 in
-h)
echo "$USAGE"
exit 0
;;
--repo | -r)
POSTGRES_REPO="$2"
shift 2
;;
-b | --branch)
POSTGRES_BRANCH="$2"
shift 2
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters

clone() {
echo "Downloading postgres source from $POSTGRES_REPO"
echo "Downloading postgres source from $POSTGRES_REPO"

if [ -d "$PG_SRC_DIR" ] && [ -n "$(ls -A "$PG_SRC_DIR")" ]; then
echo "The directory $PG_SRC_DIR already exists and is not empty. Aborting."
return 0
fi
if [ -d "$PG_SRC_DIR" ] && [ -n "$(ls -A "$PG_SRC_DIR")" ]; then
echo "The directory $PG_SRC_DIR already exists and is not empty. Aborting."
return 0
fi

mkdir -p "$(dirname "$PG_SRC_DIR")"
git clone --single-branch -b "$POSTGRES_BRANCH" "$POSTGRES_REPO" "$PG_SRC_DIR"
mkdir -p "$(dirname "$PG_SRC_DIR")"
git clone --single-branch -b "$POSTGRES_BRANCH" "$POSTGRES_REPO" "$PG_SRC_DIR"
}

patch() {
# The meson scripts assume that clang is part of the LLVM suite, which is used when
# enabling the jit build (which we do).
# Unfortunately the is not always the case. We work around this by patching the build scripts:
git -C "$PG_SRC_DIR" apply - <<EOF
# The meson scripts assume that clang is part of the LLVM suite, which is used when
# enabling the jit build (which we do).
# Unfortunately the is not always the case. We work around this by patching the build scripts:
git -C "$PG_SRC_DIR" apply - <<EOF
diff --git a/meson.build b/meson.build
index 6804f941be..d62142c8f7 100644
--- a/meson.build
Expand All @@ -91,12 +91,12 @@ EOF
}

configure() {
echo "Configuring postgres build"
echo "Configuring postgres build"

patch
patch

meson setup "$PG_BUILD_DIR" "$PG_SRC_DIR" \
--debug \
meson setup "$PG_BUILD_DIR" "$PG_SRC_DIR" \
--debug \
-Dprefix="${PG_INSTALL_DIR}" \
-Dbindir="${PG_INSTALL_DIR}/bin" \
-Ddatadir="${PG_INSTALL_DIR}/share" \
Expand All @@ -121,13 +121,13 @@ configure() {
}

build() {
echo "Building postgres"
echo "Building postgres"

ninja -C "$PG_BUILD_DIR"
}

install() {
echo "Installing postgres to $PG_INSTALL_DIR"
echo "Installing postgres to $PG_INSTALL_DIR"

mkdir -p "${PG_INSTALL_DIR}"
ninja -C "${PG_BUILD_DIR}" install
Expand All @@ -136,28 +136,28 @@ install() {
# The first argument determines the function to run. If no argument is given we run all commands in order:

if [ -z "$1" ]; then
clone && configure && build && install
exit 0
clone && configure && build && install
exit 0
fi

case $1 in
clone)
clone
;;
patch)
patch
;;
configure)
configure
;;
build)
build
;;
install)
install
;;
*)
echo "Usage: $0 {clone|configure|build|install}"
exit 1
;;
clone)
clone
;;
patch)
patch
;;
configure)
configure
;;
build)
build
;;
install)
install
;;
*)
echo "Usage: $0 {clone|configure|build|install}"
exit 1
;;
esac
Loading

0 comments on commit fe00b10

Please sign in to comment.