diff --git a/examples/sqlfns/README.md b/examples/sqlfns/README.md new file mode 100644 index 0000000..2461523 --- /dev/null +++ b/examples/sqlfns/README.md @@ -0,0 +1,2 @@ +# sqlfns - Sample extension showing how to create function that can be called from Postgres + diff --git a/examples/sqlfns/build.zig b/examples/sqlfns/build.zig new file mode 100644 index 0000000..f8ac2b0 --- /dev/null +++ b/examples/sqlfns/build.zig @@ -0,0 +1,53 @@ +const std = @import("std"); + +// Load pgzx build support. The build utilities use pg_config to find all dependencies +// and provide functions go create and test extensions. +const PGBuild = @import("pgzx").Build; + +pub fn build(b: *std.Build) void { + // Project meta data + const name = "sqlfns"; + const version = .{ .major = 0, .minor = 1 }; + + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + // Load the pgzx module and initialize the build utilities + const dep_pgzx = b.dependency("pgzx", .{ .target = target, .optimize = optimize }); + const pgzx = dep_pgzx.module("pgzx"); + var pgbuild = PGBuild.create(b, .{ .target = target, .optimize = optimize }); + + const build_options = b.addOptions(); + build_options.addOption(bool, "testfn", b.option(bool, "testfn", "Register test function") orelse false); + + // Register the dependency with the build system + // and add pgzx as module dependency. + { + const ext = pgbuild.addInstallExtension(.{ + .name = name, + .version = version, + .root_source_file = b.path("src/main.zig"), + .root_dir = ".", + }); + ext.lib.root_module.addImport("pgzx", pgzx); + ext.lib.root_module.addOptions("build_options", build_options); + + b.getInstallStep().dependOn(&ext.step); + } + + // Configure pg_regress based testing for the current extension. + { + const extest = pgbuild.addRegress(.{ + .db_user = "postgres", + .db_port = 5432, + .root_dir = ".", + .scripts = &[_][]const u8{ + "sqlfns_test", + }, + }); + + // Make regression tests available to `zig build` + var regress = b.step("pg_regress", "Run regression tests"); + regress.dependOn(&extest.step); + } +} diff --git a/examples/sqlfns/build.zig.zon b/examples/sqlfns/build.zig.zon new file mode 100644 index 0000000..2fd4f93 --- /dev/null +++ b/examples/sqlfns/build.zig.zon @@ -0,0 +1,13 @@ +.{ + .name = "sqlfns", + .version = "0.1.0", + .paths = .{ + "extension", + "src", + }, + .dependencies = .{ + .pgzx = .{ + .path = "./../..", + }, + }, +} diff --git a/examples/sqlfns/ci/run.sh b/examples/sqlfns/ci/run.sh new file mode 100755 index 0000000..8be58ce --- /dev/null +++ b/examples/sqlfns/ci/run.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +#set -x +set -o pipefail + +EXTENSION_NAME=sqlfns + +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= <