Skip to content
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
10 changes: 10 additions & 0 deletions .github/sh/create-database-gleam_cake_pog_test-postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env sh

set -eu

psql <<SQL
SELECT 'CREATE DATABASE gleam_cake_pog_test'
WHERE NOT EXISTS (
SELECT FROM pg_database WHERE datname = 'gleam_cake_pog_test'
)\\gexec
SQL
10 changes: 0 additions & 10 deletions .github/sh/create-database-gleam_cake_test-postgres.sh

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/abstract_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ on:
gleam_version:
type: string
required: true
default: "1.9.0"
default: "1.11.1"
erlang_version:
type: string
default: "26"
default: "27"
test_erlang:
type: boolean
default: true
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
with:
gleam_version: ${{ inputs.gleam_version }}
erlang_version: ${{ matrix.erlang }}
- run: ./.github/sh/create-database-gleam_cake_test-postgres.sh
- run: ./.github/sh/create-database-gleam_cake_pog_test-postgres.sh
env:
PGHOST: localhost
PGPORT: 5432
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:
test_lowest:
uses: ./.github/workflows/abstract_test.yml
with:
erlang_version: "26"
gleam_version: "1.9.0"
erlang_version: "27"
gleam_version: "1.11.1"
test_erlang: true
test_node: false
test_highest:
uses: ./.github/workflows/abstract_test.yml
with:
erlang_version: "27"
gleam_version: "1.9.1"
erlang_version: "28"
gleam_version: "1.11.1"
test_erlang: true
test_node: false
6 changes: 2 additions & 4 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# erlang 26.2.5.6
erlang 27.3.1
gleam 1.9.1
# gleam nightly
erlang 28.0.1
gleam 1.11.1
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

<!-- ## [Unreleased] -->

## [1.2.0] - 2025-07-09

- Utilize `pog ~> 4.0.0` dependency.

## [1.1.1] - 2025-04-02

- Updated dependencies.
Expand Down
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ services:
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=gleam_cake_test
- POSTGRES_DB=gleam_cake_pog_test
command: postgres -c 'max_connections=10'
ports:
- "127.0.0.1:5432:5432"
volumes:
- ./docker/data/postgres-data:/var/lib/postgresql/data
# - ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready", "-d", "gleam_cake_test"]
test: ["CMD-SHELL", "pg_isready", "-d", "gleam_cake_pog_test"]
interval: 30s
timeout: 60s
retries: 5
start_period: 80s

volumes:
gleam-cake-pog-test:
17 changes: 9 additions & 8 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
name = "cake_pog"
version = "1.1.1"
version = "1.2.0"
description = "🎂Cake 🐘PostgreSQL adapter which passes PreparedStatements to the pog library for execution written in Gleam."
licences = ["MPL-2.0"]
repository = { type = "github", user = "inoas", repo = "gleam-cake-pog" }
# links = [{ title = "Website", href = "https://gleam.run" }]

gleam = ">= 1.9.0"
gleam = ">= 1.10.0"

[documentation]
pages = [
{ title = "CHANGELOG", path = "changelog.html", source = "CHANGELOG.md" },
]

[dependencies]
cake = ">= 2.1.0 and < 3.0.0"
gleam_stdlib = ">= 0.58.0 and < 2.0.0"
pog = ">= 3.2.0 and < 4.0.0"
cake = ">= 2.1.2 and < 3.0.0"
gleam_erlang = ">= 1.2.0 and < 2.0.0"
gleam_stdlib = ">= 0.61.0 and < 2.0.0"
pog = ">= 4.0.0 and < 5.0.0"

[dev-dependencies]
birdie = ">= 1.2.6 and < 2.0.0"
gleeunit = ">= 1.3.0 and < 2.0.0"
pprint = ">= 1.0.4 and < 2.0.0"
birdie = ">= 1.3.1 and < 2.0.0"
gleeunit = ">= 1.6.0 and < 2.0.0"
pprint = ">= 1.0.6 and < 2.0.0"
46 changes: 25 additions & 21 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@
packages = [
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ name = "backoff", version = "1.1.6", build_tools = ["rebar3"], requirements = [], otp_app = "backoff", source = "hex", outer_checksum = "CF0CFFF8995FB20562F822E5CC47D8CCF664C5ECDC26A684CBE85C225F9D7C39" },
{ name = "birdie", version = "1.2.6", build_tools = ["gleam"], requirements = ["argv", "edit_distance", "filepath", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "justin", "rank", "simplifile", "term_size", "trie_again"], otp_app = "birdie", source = "hex", outer_checksum = "1363F4C7E7433A4A8350CC682BCDDBA5BBC6F66C94EFC63BC43025F796C4F6D0" },
{ name = "cake", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "cake", source = "hex", outer_checksum = "141FEBE63A3BBD2872232BBB61B83E75F9367A4C07DD825286D58AF8D86D7702" },
{ name = "birdie", version = "1.3.1", build_tools = ["gleam"], requirements = ["argv", "edit_distance", "filepath", "glance", "gleam_community_ansi", "gleam_stdlib", "justin", "rank", "simplifile", "term_size", "trie_again"], otp_app = "birdie", source = "hex", outer_checksum = "F811C9EDAF920EF48597A26E788907AAF80D9239A5E8C8CCFBD0DD1BB10184D7" },
{ name = "cake", version = "2.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "cake", source = "hex", outer_checksum = "E692DED831B81F34CEB52042E27A9D1F78684BDF58E26B9245550D5613B4F2CE" },
{ name = "edit_distance", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "edit_distance", source = "hex", outer_checksum = "A1E485C69A70210223E46E63985FA1008B8B2DDA9848B7897469171B29020C05" },
{ name = "exception", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "329D269D5C2A314F7364BD2711372B6F2C58FA6F39981572E5CA68624D291F8C" },
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
{ name = "glam", version = "2.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "4932A2D139AB0389E149396407F89654928D7B815E212BB02F13C66F53B1BBA1" },
{ name = "glance", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "106111453AE9BA959184302B7DADF2E8CF322B27A7CB68EE78F3EE43FEACCE2C" },
{ name = "glance", version = "5.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "FAA3DAC74AF71D47C67D88EB32CE629075169F878D148BB1FF225439BE30070A" },
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
{ name = "gleam_stdlib", version = "0.58.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "091F2D2C4A3A4E2047986C47E2C2C9D728A4E068ABB31FDA17B0D347E6248467" },
{ name = "gleeunit", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "0E6C83834BA65EDCAAF4FE4FB94AC697D9262D83E6F58A750D63C9F6C8A9D9FF" },
{ name = "gleam_community_colour", version = "2.0.2", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "E34DD2C896AC3792151EDA939DA435FF3B69922F33415ED3C4406C932FBE9634" },
{ name = "gleam_erlang", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "F91CE62A2D011FA13341F3723DB7DB118541AAA5FE7311BD2716D018F01EF9E3" },
{ name = "gleam_json", version = "3.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "874FA3C3BB6E22DD2BB111966BD40B3759E9094E05257899A7C08F5DE77EC049" },
{ name = "gleam_otp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "7020E652D18F9ABAC9C877270B14160519FA0856EE80126231C505D719AD68DA" },
{ name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" },
{ name = "gleam_stdlib", version = "0.61.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "3DC407D6EDA98FCE089150C11F3AD892B6F4C3CA77C87A97BAE8D5AB5E41F331" },
{ name = "gleam_time", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_time", source = "hex", outer_checksum = "D71F1AFF7FEB534FF55E5DC58E534E9201BA75A444619788A2E4DEA4EBD87D16" },
{ name = "gleeunit", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "63022D81C12C17B7F1A60E029964E830A4CBD846BBC6740004FC1F1031AE0326" },
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" },
{ name = "opentelemetry_api", version = "1.4.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "3DFBBFAA2C2ED3121C5C483162836C4F9027DEF469C41578AF5EF32589FCFC58" },
{ name = "pg_types", version = "0.4.0", build_tools = ["rebar3"], requirements = [], otp_app = "pg_types", source = "hex", outer_checksum = "B02EFA785CAECECF9702C681C80A9CA12A39F9161A846CE17B01FB20AEEED7EB" },
{ name = "pgo", version = "0.14.0", build_tools = ["rebar3"], requirements = ["backoff", "opentelemetry_api", "pg_types"], otp_app = "pgo", source = "hex", outer_checksum = "71016C22599936E042DC0012EE4589D24C71427D266292F775EBF201D97DF9C9" },
{ name = "pog", version = "3.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "pgo"], otp_app = "pog", source = "hex", outer_checksum = "63152DADEBAE3150C81E9612909974C3C38F3E35B41F252798069A2FD117308E" },
{ name = "pprint", version = "1.0.4", build_tools = ["gleam"], requirements = ["glam", "gleam_stdlib"], otp_app = "pprint", source = "hex", outer_checksum = "C310A98BDC0995644847C3C8702DE19656D6BCD638B2A8A358B97824379ECAA1" },
{ name = "pg_types", version = "0.5.0", build_tools = ["rebar3"], requirements = [], otp_app = "pg_types", source = "hex", outer_checksum = "A3023B464AA960BC1628635081E30CCA4F676F2D4C23CCD6179C1C11C9B4A642" },
{ name = "pgo", version = "0.15.0", build_tools = ["rebar3"], requirements = ["backoff", "opentelemetry_api", "pg_types"], otp_app = "pgo", source = "hex", outer_checksum = "4B883D751B8D4247F4D8A6FBAE58EDB6FA9DBC183371299BF84975EE36406388" },
{ name = "pog", version = "4.0.0", build_tools = ["gleam"], requirements = ["exception", "gleam_erlang", "gleam_otp", "gleam_stdlib", "gleam_time", "pgo"], otp_app = "pog", source = "hex", outer_checksum = "E495AAD2C4FD0BB3A92C505682705FA2460053B2D3ABC6F9024C3FED6868417D" },
{ name = "pprint", version = "1.0.6", build_tools = ["gleam"], requirements = ["glam", "gleam_stdlib"], otp_app = "pprint", source = "hex", outer_checksum = "4E9B34AE03B2E81D60F230B9BAF1792BE1AC37AFB5564B8DEBEE56BAEC866B7D" },
{ name = "rank", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "rank", source = "hex", outer_checksum = "5660E361F0E49CBB714CC57CC4C89C63415D8986F05B2DA0C719D5642FAD91C9" },
{ name = "simplifile", version = "2.2.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C88E0EE2D509F6D86EB55161D631657675AA7684DAB83822F7E59EB93D9A60E3" },
{ name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" },
{ name = "term_size", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "term_size", source = "hex", outer_checksum = "D00BD2BC8FB3EBB7E6AE076F3F1FF2AC9D5ED1805F004D0896C784D06C6645F1" },
{ name = "trie_again", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "trie_again", source = "hex", outer_checksum = "5B19176F52B1BD98831B57FDC97BD1F88C8A403D6D8C63471407E78598E27184" },
{ name = "trie_again", version = "1.1.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "trie_again", source = "hex", outer_checksum = "365FE609649F3A098D1D7FC7EA5222EE422F0B3745587BF2AB03352357CA70BB" },
]

[requirements]
birdie = { version = ">= 1.2.6 and < 2.0.0" }
cake = { version = ">= 2.1.0 and < 3.0.0" }
gleam_stdlib = { version = ">= 0.58.0 and < 2.0.0" }
gleeunit = { version = ">= 1.3.0 and < 2.0.0" }
pog = { version = ">= 3.2.0 and < 4.0.0" }
pprint = { version = ">= 1.0.4 and < 2.0.0" }
birdie = { version = ">= 1.3.1 and < 2.0.0" }
cake = { version = ">= 2.1.2 and < 3.0.0" }
gleam_erlang = { version = ">= 1.2.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.61.0 and < 2.0.0" }
gleeunit = { version = ">= 1.6.0 and < 2.0.0" }
pog = { version = ">= 4.0.0 and < 5.0.0" }
pprint = { version = ">= 1.0.6 and < 2.0.0" }
73 changes: 59 additions & 14 deletions src/cake/adapter/postgres.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ import cake/param.{
type Param, BoolParam, FloatParam, IntParam, NullParam, StringParam,
}
import gleam/dynamic/decode.{type Decoder}
import gleam/erlang/process
import gleam/list
import gleam/option.{type Option}
import pog.{type Connection, type QueryError, type Returned, type Value}

/// Connection to a PostgreSQL database.
const default_process_name = "cake_pog"

/// Runs a callbakc on a connection to a PostgreSQL database.
///
/// NOTICE: ⚠️⚠️⚠️ You should probably be using `gleam_erlang`'s
/// `supervisor.new`, `supervisor.add`, and `supervisor.start` instead,
/// to create the database connection.
///
/// See `pog`'s official documentation <https://hexdocs.pm/pog/>.
///
/// This is a thin wrapper around the `pog` library's `Connection` type.
///
Expand All @@ -27,19 +36,55 @@ pub fn with_connection(
database database: String,
callback callback: fn(Connection) -> a,
) -> a {
let connection =
pog.Config(
..pog.default_config(),
host: host,
port: port,
user: username,
password: password,
database: database,
)
|> pog.connect

let value = callback(connection)
pog.disconnect(connection)
let process = process.new_name(default_process_name)

with_named_connection(
process:,
host:,
port:,
username:,
password:,
database:,
callback:,
)
}

/// Runs a callback on a connection to a PostgreSQL database.
///
/// NOTICE: ⚠️⚠️⚠️ You should probably be using `gleam_erlang`'s
/// `supervisor.new`, `supervisor.add`, and `supervisor.start` instead,
/// to create the database connection.
///
/// See `pog`'s official documentation <https://hexdocs.pm/pog/>.
///
/// This is a thin wrapper around the `pog` library's `Connection` type.
///
/// In addition to `with_connection` you may specify the erlang process name.
///
pub fn with_named_connection(
process process: process.Name(pog.Message),
host host: String,
port port: Int,
username username: String,
password password: Option(String),
database database: String,
callback callback: fn(Connection) -> a,
) -> a {
let pog_config =
pog.default_config(process)
|> pog.port(port)
|> pog.user(username)
|> pog.password(password)
|> pog.host(host)
|> pog.database(database)

let assert Ok(actor) = pog.start(pog_config)
let pid = actor.pid
let db = actor.data

let value = callback(db)

process.send_exit(pid)

value
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper/postgres_test_helper.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn with_local_test_connection(callback callback) {
port: 5432,
username: "postgres",
password: Some("postgres"),
database: "gleam_cake_test",
database: "gleam_cake_pog_test",
callback:,
)
}
Expand Down
Loading