From e774529b04badc11b5e07b8590dacc5e5b6f80b9 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Tue, 3 Oct 2023 20:59:29 +0200 Subject: [PATCH] feat: add outbox Signed-off-by: kjuulh --- Cargo.toml | 2 +- crates/crunch-postgres/build.rs | 5 +++ .../migrations/20231003181849_initial.sql | 8 +++++ crates/crunch-postgres/src/lib.rs | 32 +++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 crates/crunch-postgres/build.rs create mode 100644 crates/crunch-postgres/migrations/20231003181849_initial.sql diff --git a/Cargo.toml b/Cargo.toml index fd0bcc7..2976217 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,6 @@ genco = {version = "0.17.6"} walkdir = {version = "2.4.0"} regex = {version = "1.9.5"} inquire = {version = "0.6.2"} -sqlx = {version = "0.7.2", default-features = false, features = ["migrate", "macros", "postgres", "runtime-tokio", "tls-rustls", "chrono" ]} +sqlx = {version = "0.7.2", default-features = false, features = ["migrate", "macros", "postgres", "runtime-tokio", "tls-rustls", "chrono", "json" ]} pretty_assertions = "1.4.0" \ No newline at end of file diff --git a/crates/crunch-postgres/build.rs b/crates/crunch-postgres/build.rs new file mode 100644 index 0000000..7609593 --- /dev/null +++ b/crates/crunch-postgres/build.rs @@ -0,0 +1,5 @@ +// generated by `sqlx migrate build-script` +fn main() { + // trigger recompilation when a new migration is added + println!("cargo:rerun-if-changed=migrations"); +} \ No newline at end of file diff --git a/crates/crunch-postgres/migrations/20231003181849_initial.sql b/crates/crunch-postgres/migrations/20231003181849_initial.sql new file mode 100644 index 0000000..580b2d1 --- /dev/null +++ b/crates/crunch-postgres/migrations/20231003181849_initial.sql @@ -0,0 +1,8 @@ +-- Add migration script here +CREATE TABLE outbox ( + id UUID NOT NULL, + metadata JSONB NOT NULL, + content BYTEA NOT NULL, + inserted_time TIMESTAMPTZ NOT NULL DEFAULT now(), + state VARCHAR NOT NULL, +); diff --git a/crates/crunch-postgres/src/lib.rs b/crates/crunch-postgres/src/lib.rs index 8b13789..393fd1a 100644 --- a/crates/crunch-postgres/src/lib.rs +++ b/crates/crunch-postgres/src/lib.rs @@ -1 +1,33 @@ +use async_trait::async_trait; +use crunch_traits::{errors::PersistenceError, EventInfo}; +use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; +pub struct PostgresPersistence { + pool: Pool, +} + +impl PostgresPersistence { + pub async fn new(dsn: &str) -> anyhow::Result { + let pool = PgPoolOptions::new().max_connections(5).connect(dsn).await?; + + sqlx::migrate!().run(&pool).await?; + + Ok(Self { pool }) + } +} + +#[async_trait] +impl crunch_traits::Persistence for PostgresPersistence { + async fn insert(&self, event_info: &EventInfo, content: Vec) -> anyhow::Result<()> { + todo!() + } + async fn next(&self) -> Option { + todo!() + } + async fn get(&self, event_id: &str) -> Result)>, PersistenceError> { + todo!() + } + async fn update_published(&self, event_id: &str) -> Result<(), PersistenceError> { + todo!() + } +}