92次发布

0.12.26 2024年6月25日
0.12.23 2024年4月12日
0.12.22 2024年3月20日
0.12.2 2023年12月29日
0.0.1-alpha.42023年3月23日

数据库接口 中排名第 159

Download history 59/week @ 2024-05-01 128/week @ 2024-05-08 161/week @ 2024-05-15 245/week @ 2024-05-22 177/week @ 2024-05-29 425/week @ 2024-06-05 237/week @ 2024-06-12 138/week @ 2024-06-19 56/week @ 2024-06-26 45/week @ 2024-07-03 51/week @ 2024-07-10 67/week @ 2024-07-17 418/week @ 2024-07-24 62/week @ 2024-07-31 63/week @ 2024-08-07 127/week @ 2024-08-14

每月下载 690

Apache-2.0

670KB
3.5K SLoC

Trunk CLI

Trunk CLI允许构建、发布和安装各种Postgres扩展。它通过以下命令抽象出扩展开发和管理的复杂性:

  • trunk build - 将扩展编译成可发布和可安装的工件。
  • trunk publish - 将扩展发布到Trunk注册表,使其对Postgres社区可用,以便发现和安装。
  • trunk install - 从Trunk注册表下载Postgres扩展,并在Trunk运行的环境中进行安装。

安装

Trunk CLI托管在crates.io上,可以使用cargo进行安装。

  1. curlhttps://sh.rustup.rs -sSf | sh
  2. cargoinstall pg-trunk
  • 要检查Trunk的版本,请调用 trunk --version
  • 要升级到Trunk的最新版本,请运行 cargo install pg-trunk

trunk build

《build》命令允许从源代码编译和打包Postgres扩展。打包后的扩展会被写入到.trunk/<extension-name>-<extension-version>.tar.gz

 trunk build --help
Usage: trunk build [OPTIONS]

Options:
  -p, --path <PATH>                        [default: .]
  -o, --output-path <OUTPUT_PATH>          [default: ./.trunk]
      --version <VERSION>
      --name <NAME>
      --platform <PLATFORM>
      --dockerfile <DOCKERFILE_PATH>
      --install-command <INSTALL_COMMAND>
  -h, --help                               Print help

基于PGRX的扩展

扩展可以通过多种方式构建,而PGRX允许我们使用Rust来完成这项工作。Trunk使得基于PGRX的扩展的构建和打包变得比以往任何时候都要简单。

使用基于PGRX的扩展pgmq的示例trunk build

 trunk build
Building from path .
Detected that we are building a pgrx extension
Detected pgrx version range 0.7.4
Using pgrx version 0.7.4
Building pgrx extension at path .
.
.
.
Creating package at: ./.trunk/pgmq-0.5.0.tar.gz
Create Trunk bundle:
	pgmq.so
	extension/pgmq--0.5.0.sql
	extension/pgmq.control
	manifest.json
Packaged to ./.trunk/pgmq-0.5.0.tar.gz

C & SQL 基础的扩展

扩展也可以用C & SQL编写。让我们看看如何使用Trunk构建C & SQL基础的扩展。

使用C & SQL基础的扩展pg_cron的示例trunk build

 trunk build --name pg_cron --version 1.5.2
Building from path .
Detected a Makefile, guessing that we are building an extension with 'make', 'make install...'
Using install command make install
Building with name pg_cron
Building with version 1.5.2
.
.
.
Creating package at: ./.trunk/pg_cron-1.5.2.tar.gz
Create Trunk bundle:
	bitcode/pg_cron/src/entry.bc
	bitcode/pg_cron/src/job_metadata.bc
	bitcode/pg_cron/src/misc.bc
	bitcode/pg_cron/src/pg_cron.bc
	bitcode/pg_cron/src/task_states.bc
	bitcode/pg_cron.index.bc
	pg_cron.so
	extension/pg_cron--1.0--1.1.sql
	extension/pg_cron--1.0.sql
	extension/pg_cron--1.1--1.2.sql
	extension/pg_cron--1.2--1.3.sql
	extension/pg_cron--1.3--1.4.sql
	extension/pg_cron--1.4--1.4-1.sql
	extension/pg_cron--1.4-1--1.5.sql
	extension/pg_cron.control
	manifest.json
Packaged to ./.trunk/pg_cron-1.5.2.tar.gz

有些扩展是更大项目的一部分,并包含指向父目录的Makefiles。这类扩展的例子可以在postgres/contrib中找到。Trunk可以帮助我们构建和打包这类扩展。

使用C & SQL基础的扩展pg_stat_statements的示例trunk build

postgres/contrib仓库的根目录下创建一个名为Dockerfile.pg_stat_statements的自定义Dockerfile

ARG PG_VERSION=15
FROM quay.io/coredb/c-builder:pg${PG_VERSION}
USER root

# Postgres build dependencies. Additional system dependencies for the extension can be added here.
# https://wiki.postgresql.ac.cn/wiki/Compile_and_Install_from_source_code
RUN apt-get update && apt-get install -y  build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt-dev libssl-dev libxml2-utils xsltproc ccache

# Copy working directory into container
COPY --chown=postgres:postgres . .
# Necessary step for building extensions in postgres/contrib
RUN ./configure
# Run make in the pg_stat_statements directory
RUN cd contrib/pg_stat_statements && make

使用--dockerfile--install-command标志运行trunk build

 trunk build \
--name pg_stat_statements \
--version 1.10.0 \
--dockerfile Dockerfile.pg_stat_statements \
--install-command \
"cd contrib/pg_stat_statements \
&& make install \
&& set -x \
&& mv /usr/local/pgsql/share/extension/* /usr/share/postgresql/15/extension \
&& mv /usr/local/pgsql/lib/* /usr/lib/postgresql/15/lib"
Building from path .
Detected a Makefile, guessing that we are building an extension with 'make', 'make install...'
Using Dockerfile at Dockerfile.pg_stat_statements
Using install command /bin/sh -c cd contrib/pg_stat_statements && make install && set -x && mv /usr/local/pgsql/share/extension/* /usr/share/postgresql/15/extension && mv /usr/local/pgsql/lib/* /usr/lib/postgresql/15/lib
Building with name pg_stat_statements
Building with version 1.10.0
.
.
.
Creating package at: ./.trunk/pg_stat_statements-1.10.0.tar.gz
Create Trunk bundle:
pg_stat_statements.so
extension/pg_stat_statements--1.0--1.1.sql
extension/pg_stat_statements--1.1--1.2.sql
extension/pg_stat_statements--1.2--1.3.sql
extension/pg_stat_statements--1.3--1.4.sql
extension/pg_stat_statements--1.4--1.5.sql
extension/pg_stat_statements--1.4.sql
extension/pg_stat_statements--1.5--1.6.sql
extension/pg_stat_statements--1.6--1.7.sql
extension/pg_stat_statements--1.7--1.8.sql
extension/pg_stat_statements--1.8--1.9.sql
extension/pg_stat_statements--1.9--1.10.sql
extension/pg_stat_statements.control
manifest.json
Packaged to ./.trunk/pg_stat_statements-1.10.0.tar.gz

trunk publish

《publish》命令允许您将新创建的Postgres扩展发布到Trunk注册表。

 trunk publish --help
Usage: trunk publish [OPTIONS] --version <VERSION> <NAME>

Arguments:
  <NAME>

Options:
  -v, --version <VERSION>
  -f, --file <FILE>
  -d, --description <DESCRIPTION>
  -D, --documentation <DOCUMENTATION>
  -H, --homepage <HOMEPAGE>
  -l, --license <LICENSE>
  -r, --registry <REGISTRY>            [default: https://registry.pgtrunk.io]
  -R, --repository <REPOSITORY>
  -h, --help                           Print help
  1. Trunk注册表上登录并点击Profile

  2. API Token下,点击Create New Token

  3. export TRUNK_API_TOKEN=<your-new-token>

  4. 从您的扩展所在的目录运行以下命令

 trunk publish pgmq \
--version 0.5.0 \
--description "Message Queue for postgres" \
--documentation "https://tembo-io.github.io/coredb/extensions/pgmq" \
--repository "https://github.com/tembo-io/coredb" \
--license "Apache-2.0" \
--homepage "https://www.coredb.io"

trunk install

《install》命令允许您从Trunk注册表安装Postgres扩展。Trunk将自动安装注册表中存在的任何附加扩展依赖项。

 trunk install --help
Usage: trunk install [OPTIONS] <NAME>

Arguments:
  <NAME>

Options:
  -p, --pg-config <PG_CONFIG>
  -f, --file <FILE>
  -v, --version <VERSION>      [default: latest]
  -r, --registry <REGISTRY>    [default: https://registry.pgtrunk.io]
  -h, --help                   Print help
 trunk install pgmq
Using pg_config: /usr/bin/pg_config
Using pkglibdir: "/usr/lib/postgresql/15/lib"
Using sharedir: "/usr/share/postgresql/15"
Downloading from: https://cdb-plat-use1-prod-pgtrunkio.s3.amazonaws.com/extensions/pgmq/pgmq-0.5.0.tar.gz
Dependencies: ["pg_partman"]
Installing pgmq 0.5.0
[+] pgmq.so => /usr/lib/postgresql/15/lib
[+] extension/pgmq--0.5.0.sql => /usr/share/postgresql/15
[+] extension/pgmq.control => /usr/share/postgresql/15

依赖关系

~22–38MB
~638K SLoC