Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example sqlfns #79

Merged
merged 4 commits into from
Jul 15, 2024
Merged
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
154 changes: 57 additions & 97 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,75 @@
#set -x
set -o pipefail

test_pgzx() {
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

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
}

test_pgaudit_zig() {
run_unit_tests ./examples/pgaudit_zig
}

test_spi_sql() {
local rc=0
run_regression_tests ./examples/spi_sql || rc=1
return $rc
}

extension_build() {
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"
}
examples=(./examples/*)

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

run_regression_tests() {
echo "Run regression tests"

build_example() {
cwd=$(pwd)
cd "$1" || return 1
zig build pg_regress --verbose || return 1
./ci/run.sh build || return 1
cd "$cwd" || return 1
}

run_unit_tests() {
echo "Run unit tests"

run_example() {
cwd=$(pwd)
cd "$1" || return 1
zig build -freference-trace -p "$PG_HOME" unit || return 1
./ci/run.sh || return 1
cd "$cwd" || return 1
}

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

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"

extension_build ./examples/char_count_zig || fail "Failed to build char_count_zig"
extension_build ./examples/pgaudit_zig || fail "Failed to build pgaudit_zig"
extension_build ./examples/spi_sql || fail "Failed to build spi_sql"

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 test_spi_sql || ok=false

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

echo "Success!"
test_pgzx() {
rc=0
zig build -freference-trace -p "$PG_HOME" unit || rc=1
psql -U postgres -c "DROP EXTENSION IF EXISTS pgzx_unit"
return $rc
}

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"

# build examples
echo "${examples[@]}"
build_jobs=()
for example in "${examples[@]}"; do
echo -e "\n\nBuild example $example"
build_example "$example" &
build_jobs+=($!)
done
for job in "${build_jobs[@]}"; do
wait "$job" || fail "Failed to build example"
done

echo -e "\n\nStart PostgreSQL"
pgstart || fail "Failed to start PostgreSQL"
trap pgstop TERM INT EXIT

echo -e "\n\nRun pgzx unit tests:"
test_pgzx || fail "Failed to run pgzx unit tests"

for example in "${examples[@]}"; do
echo -e "\n\nRun example CI script $example"
run_example "$example" || fail "Failed to run CI script for $example"
done

ok=true

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

echo "Success!"
63 changes: 63 additions & 0 deletions examples/char_count_zig/ci/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash

#set -x
set -o pipefail

EXTENSION_NAME=char_count_zig

build() {
echo "Build extension $EXTENSION_NAME"
zig build -freference-trace -p "$PG_HOME" || return 1
}

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

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

regression_tests() {
echo "Run regression tests: $EXTENSION_NAME"
zig build pg_regress --verbose || return 1
}

unit_tests() {
echo "Run unit tests: $EXTENSION_NAME"
zig build -freference-trace -p "$PG_HOME" unit || return 1
}

all() {
build && create_extension && unit_tests && regression_tests && extension_drop
}

# optional command. Use all if not specified
command=${1:-all}

#shellcheck disable=SC1007
HELP= <<EOF
Usage: $0 [command]

commands (default 'all'):
all - build nand run tests
build - build and install extension
create_extension - create extension
extension_drop - drop extension
regression_tests - run regression tests
unit_tests - run unit tests
help - show this help message
EOF

case $command in
all) all ;;
build) build ;;
create_extension) create_extension ;;
extension_drop) extension_drop ;;
regression_tests) regression_tests ;;
unit_tests) unit_tests ;;
help) echo "$HELP" ;;
*) echo "$HELP" ;;
esac
56 changes: 56 additions & 0 deletions examples/pgaudit_zig/ci/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash

#set -x
set -o pipefail

EXTENSION_NAME=pgaudit_zig

build() {
echo "Build extension $EXTENSION_NAME"
zig build -freference-trace -p "$PG_HOME" || return 1
}

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

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

unit_tests() {
echo "Run unit tests: $EXTENSION_NAME"
zig build -freference-trace -p "$PG_HOME" unit || return 1
}

all() {
build && create_extension && unit_tests && extension_drop
}

# optional command. Use all if not specified
command=${1:-all}

#shellcheck disable=SC1007
HELP= <<EOF
Usage: $0 [command]

commands (default 'all'):
all - build nand run tests
build - build and install extension
create_extension - create extension
extension_drop - drop extension
unit_tests - run unit tests
help - show this help message
EOF

case $command in
all) all ;;
build) build ;;
create_extension) create_extension ;;
extension_drop) extension_drop ;;
unit_tests) unit_tests ;;
help) echo "$HELP" ;;
*) echo "$HELP" ;;
esac
57 changes: 57 additions & 0 deletions examples/spi_sql/ci/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

#set -x
set -o pipefail

EXTENSION_NAME=spi_sql

build() {
echo "Build extension $EXTENSION_NAME"
zig build -freference-trace -p "$PG_HOME" || return 1
}

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

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

regression_tests() {
echo "Run regression tests: $EXTENSION_NAME"
zig build pg_regress --verbose || return 1
}

all() {
build && create_extension && regression_tests && extension_drop
}

# optional command. Use all if not specified
command=${1:-all}

#shellcheck disable=SC1007
HELP= <<EOF
Usage: $0 [command]

commands (default 'all'):
all - build nand run tests
build - build and install extension
create_extension - create extension
extension_drop - drop extension
regression_tests - run regression tests
help - show this help message
EOF

case $command in
all) all ;;
build) build ;;
create_extension) create_extension ;;
extension_drop) extension_drop ;;
regression_tests) regression_tests ;;
unit_tests) unit_tests ;;
help) echo "$HELP" ;;
*) echo "$HELP" ;;
esac
2 changes: 2 additions & 0 deletions examples/sqlfns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# sqlfns - Sample extension showing how to create function that can be called from Postgres

Loading