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

Update pgrx, clerk-rs, chrono; support Postgres 17 #38

Merged
merged 3 commits into from
Dec 10, 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
55 changes: 21 additions & 34 deletions .github/workflows/extension_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,26 @@ jobs:
- name: Clippy
run: cargo clippy

# figure out how to test work
# test:
# name: Run tests
# runs-on: ubuntu-22.04
# steps:
# - uses: actions/checkout@v2
# - name: Install Rust stable toolchain
# uses: actions-rs/toolchain@v1
# with:
# toolchain: stable
# - uses: Swatinem/rust-cache@v2
# with:
# prefix-key: "pgmq-extension-test"
# workspaces: |
# pgmq
# # Additional directories to cache
# cache-directories: |
# /home/runner/.pgrx
# - uses: ./.github/actions/pgx-init
# with:
# working-directory: ./
# - name: test
# run: |
# sudo apt-get update && sudo apt-get install -y postgresql-server-dev-14
# git clone https://github.com/pgpartman/pg_partman.git && \
# cd pg_partman && \
# sudo make install && cd ../
# cp /usr/share/postgresql/14/extension/pg_partman* ~/.pgrx/15.4/pgrx-install/share/postgresql/extension/
# cp /usr/lib/postgresql/14/lib/pg_partman_bgw.so ~/.pgrx/15.4/pgrx-install/lib/postgresql/
# rm -rf ./target/pgrx-test-data-* || true
# pg_version=$(stoml Cargo.toml features.default)
# cargo pgrx run ${pg_version} --pgcli || true
# cargo pgrx test ${pg_version}
test:
runs-on: ubuntu-latest
container: pgxn/pgxn-tools
strategy:
matrix:
pg: [14, 15, 16, 17]
name: 🐘 Postgres ${{ matrix.pg }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Start PostgreSQL ${{ matrix.pg }}
run: pg-start ${{ matrix.pg }}
- name: Setup Rust Cache
uses: Swatinem/rust-cache@v2
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
- name: Install pg_partman
run: pgxn install pg_partman
- name: Test on PostgreSQL ${{ matrix.pg }}
run: pgrx-build-test

publish:
# only publish release events
Expand All @@ -83,7 +70,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
pg-version: [14, 15, 16]
pg-version: [14, 15, 16, 17]
steps:
- uses: actions/checkout@v2
- name: Install Rust stable toolchain
Expand Down
17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
[package]
name = "clerk_fdw"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "lib"]

[features]
default = ["pg16"]
default = ["pg17"]
pg14 = ["pgrx/pg14", "pgrx-tests/pg14", "supabase-wrappers/pg14"]
pg15 = ["pgrx/pg15", "pgrx-tests/pg15", "supabase-wrappers/pg15"]
pg16 = ["pgrx/pg16", "pgrx-tests/pg16", "supabase-wrappers/pg16"]
pg17 = ["pgrx/pg17", "pgrx-tests/pg17", "supabase-wrappers/pg17"]
pg_test = []

[dependencies]
backoff = { version = "0.4.0", features = ["tokio"] }
chrono = "0.4.26"
clerk-rs = "0.3.0"
pgrx = "=0.11.3"
chrono = "0.4.38"
clerk-rs = "0.4.0"
pgrx = "=0.12.6"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
supabase-wrappers = { git = "https://github.com/supabase/wrappers.git", default-features = false }
supabase-wrappers = { version = "0.1.20", default-features = false }
tokio = { version = "1", features = ["full"] }

[dev-dependencies]
pgrx-tests = "=0.11.3"
pgrx-tests = "=0.12.6"
50 changes: 45 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
PGRX_POSTGRES ?= pg16
DISTNAME = $(shell grep -m 1 '^name' Trunk.toml | sed -e 's/[^"]*"\([^"]*\)",\{0,1\}/\1/')
PG_CONFIG ?= $(shell which pg_config)
PGRXV = $(shell perl -nE '/^pgrx\s+=\s"=?([^"]+)/ && do { say $$1; exit }' Cargo.toml)
PGV = $(shell perl -E 'shift =~ /(\d+)/ && say $$1' "$(shell $(PG_CONFIG) --version)")
DISTNAME = $(shell grep -m 1 '^name' Trunk.toml | sed -e 's/[^"]*"\([^"]*\)",\{0,1\}/\1/')
DISTVERSION = $(shell grep -m 1 '^version' Trunk.toml | sed -e 's/[^"]*"\([^"]*\)",\{0,1\}/\1/')

all: package

.DEFAULT_GOAL: package # Build for the PostgreSQL cluster identified by pg_config.
package:
@cargo pgrx package --pg-config "$(PG_CONFIG)"

.PHONY: install # Install jsonschema into the PostgreSQL cluster identified by pg_config.
install:
@cargo pgrx install --release --pg-config "$(PG_CONFIG)"

.PHONY: test # Run the full test suite against the PostgreSQL version identified by pg_config.
test:
@cargo test --all --no-default-features --features "pg$(PGV) pg_test" -- --nocapture

clean:
@rm -rf META.json $(DISTNAME)-$(DISTVERSION).zip

.PHONY: pg-version # Print the current PGRX version from Cargo.toml
pgrx-version:
@echo $(PGRXV)

.PHONY: pg-version # Print the current Postgres version reported by pg_config.
pg-version: Cargo.toml
@echo $(PGV)

.PHONY: install-pgrx # Install the version of PGRX specified in Cargo.toml.
install-pgrx: Cargo.toml
@cargo install --locked cargo-pgrx --version "$(PGRXV)"

.PHONY: pgrx-init # Initialize pgrx for the PostgreSQL version identified by pg_config.
pgrx-init: Cargo.toml
@cargo pgrx init "--pg$(PGV)"="$(PG_CONFIG)"

.PHONY: lint # Format and lint.
lint:
@cargo fmt --all --check
@cargo clippy --features "pg$(PGV)" --no-default-features

# Create the PGXN META.json file.
META.json: META.json.in Cargo.toml
@sed "s/@CARGO_VERSION@/$(DISTVERSION)/g" $< > $@

# Create a PGXN-compatible zip file.
$(DISTNAME)-$(DISTVERSION).zip: META.json
git archive --format zip --prefix $(DISTNAME)-$(DISTVERSION)/ --add-file $< -o $(DISTNAME)-$(DISTVERSION).zip HEAD

## pgxn-zip: Create a PGXN-compatible zip file.
pgxn-zip: $(DISTNAME)-$(DISTVERSION).zip

clean:
@rm -rf META.json $(DISTNAME)-$(DISTVERSION).zip
Empty file added sql/clerk_fdw--0.3.1--0.3.2.sql
Empty file.
Empty file added sql/clerk_fdw--0.3.2--0.3.3.sql
Empty file.
1 change: 1 addition & 0 deletions src/bin/pgrx_embed_clerk_fdw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::pgrx::pgrx_embed!();
54 changes: 44 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ impl From<ClerkFdwError> for ErrorReport {
type ClerkFdwResult<T> = Result<T, ClerkFdwError>;

impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
fn new(options: &HashMap<String, String>) -> ClerkFdwResult<Self> {
let token = if let Some(access_token) = options.get("api_key") {
fn new(server: supabase_wrappers::prelude::ForeignServer) -> ClerkFdwResult<Self> {
let token = if let Some(access_token) = server.options.get("api_key") {
access_token.to_owned()
} else {
warning!("Cannot find api_key in options");
Expand Down Expand Up @@ -224,11 +224,11 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
let run = self.rt.block_on(async {
if obj == "organization_memberships" {
// Get all organizations first
let mut offset: f32 = 0.0;
let mut offset: u64 = 0;
loop {
let org_resp = Organization::list_organizations(
&self.clerk_client,
Some(PAGE_SIZE as f32),
Some(PAGE_SIZE as u64),
Some(offset),
None,
None,
Expand All @@ -244,7 +244,7 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
OrganizationMembership::list_organization_memberships(
&self.clerk_client,
&org.id,
Some(PAGE_SIZE as f32),
Some(PAGE_SIZE as u64),
None,
)
.await
Expand Down Expand Up @@ -287,7 +287,7 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
info!("clerk_fdw: finished fetching all memberships, total={}", result.len());
break;
} else {
offset += PAGE_SIZE as f32;
offset += PAGE_SIZE as u64;
info!("clerk_fdw: fetching more organizations, offset={}", offset);
}
} else {
Expand All @@ -310,8 +310,8 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
None,
None,
None,
Some(PAGE_SIZE as f32),
Some(offset as f32),
Some(PAGE_SIZE as u64),
Some(offset as u64),
None,
)
.await
Expand All @@ -327,8 +327,8 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
"organizations" => {
match Organization::list_organizations(
&self.clerk_client,
Some(PAGE_SIZE as f32),
Some(offset as f32),
Some(PAGE_SIZE as u64),
Some(offset as u64),
None,
None,
)
Expand Down Expand Up @@ -393,3 +393,37 @@ impl ForeignDataWrapper<ClerkFdwError> for ClerkFdw {
Ok(())
}
}

/// This module is required by `cargo pgrx test` invocations.
/// It must be visible at the root of your extension crate.
#[cfg(test)]
mod pg_test {
pub fn setup(_options: Vec<&str>) {
// perform one-off initialization when the pg_test framework starts
}

pub fn postgresql_conf_options() -> Vec<&'static str> {
// return any postgresql.conf settings that are required for your tests
vec![]
}
}

// pgrx tests.
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use super::*;
#[pg_test]
fn create_fdw() {
// Just make sure it loads.
if let Err(e) = Spi::run(
r"
CREATE FOREIGN DATA WRAPPER clerk_wrapper
HANDLER clerk_fdw_handler
VALIDATOR clerk_fdw_validator
",
) {
panic!("CREATE FAILED: {e}");
}
}
}
Loading