use std::process::ExitCode;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use id_effect::{Cause, Effect, Exit, HCons, HNil, Layer, fail, hlist, need, succeed};
use id_effect_sql::audit::{self, AuditContext, AuditEntry, AuditLog};
use id_effect_sql::pg::{PgPoolConfig, PgSqlClient, pg_pool_from_config};
use id_effect_sql::sqlite::{SqlitePoolConfig, SqliteSqlClient, sqlite_pool_from_config};
use id_effect_sql::{
CurrentTenant, Dialect, Entity, FromRow, Repository, SqlClient, SqlClientService, SqlError,
SqlTransactionService, transaction_layer,
};
use id_effect_tokio::TokioRuntime;
use serde_json::{Value, json};
#[derive(Clone, Debug, PartialEq, FromRow, Entity)]
#[entity(table = "example_050_invoices", tenant = "org_id")]
struct Invoice {
#[entity(id)]
id: i64,
name: String,
amount: i64,
#[entity(version)]
version: i64,
}
impl Invoice {
fn new(id: i64, name: &str, amount: i64) -> Self {
Self {
id,
name: name.into(),
amount,
version: 1,
}
}
fn state(&self) -> Value {
json!({ "name": self.name, "amount": self.amount, "version": self.version })
}
}
const TABLE: &str = "CREATE TABLE example_050_invoices (
id BIGINT PRIMARY KEY,
org_id TEXT NOT NULL,
name TEXT NOT NULL,
amount BIGINT NOT NULL,
version BIGINT NOT NULL
)";
struct Caller {
user: &'static str,
tenant: Option<String>,
}
type RequestEnv = HCons<
SqlClientService,
HCons<CurrentTenant, HCons<AuditContext, HCons<AuditLog, HCons<SqlTransactionService, HNil>>>>,
>;
#[derive(Debug)]
enum AppError {
Sql(SqlError),
NoTenant,
Downstream(&'static str),
}
impl From<SqlError> for AppError {
fn from(e: SqlError) -> Self {
AppError::Sql(e)
}
}
struct Db {
client: Arc<dyn SqlClient>,
dialect: Dialect,
live: bool,
}
impl Db {
async fn plain<A, E>(&self, eff: Effect<A, E>) -> Result<A, E>
where
A: Send + 'static,
E: std::fmt::Debug + Send + 'static,
{
match TokioRuntime::current().expect("tokio").run(eff).await {
Exit::Success(a) => Ok(a),
Exit::Failure(Cause::Fail(e)) => Err(e),
Exit::Failure(cause) => panic!("not a typed failure: {cause:?}"),
}
}
async fn request<A: Send + 'static>(
&self,
caller: &Caller,
handler: Effect<A, AppError, RequestEnv>,
) -> Result<A, AppError> {
let Some(tenant) = caller.tenant.clone() else {
return Err(AppError::NoTenant);
};
let caps = Layer::succeed(hlist![
SqlClientService(Arc::clone(&self.client)),
CurrentTenant::new(tenant.clone()),
AuditContext::for_tenant(caller.user, tenant),
AuditLog::new(),
]);
let per_request = transaction_layer::<AppError>().provide_merge(caps);
self.plain(handler.provide_layer(per_request)).await
}
}
fn create(dialect: Dialect, invoice: Invoice) -> Effect<Invoice, AppError, RequestEnv> {
Repository::<Invoice>::for_current_tenant(dialect)
.flat_map(move |repo| {
need::<SqlTransactionService>()
.flat_map(move |tx| repo.on(tx.0.clone()).insert(invoice).narrow())
})
.map_err(AppError::from)
}
fn list(dialect: Dialect) -> Effect<Vec<Invoice>, AppError, RequestEnv> {
Repository::<Invoice>::for_current_tenant(dialect)
.flat_map(move |repo| {
need::<SqlTransactionService>().flat_map(move |tx| repo.on(tx.0.clone()).find_all().narrow())
})
.map_err(AppError::from)
}
fn rename(
dialect: Dialect,
id: i64,
name: String,
) -> Effect<(Invoice, Invoice), AppError, RequestEnv> {
Repository::<Invoice>::for_current_tenant(dialect)
.flat_map(move |repo| {
need::<SqlTransactionService>().flat_map(move |tx| {
let invoices = repo.on(tx.0.clone());
let update = invoices.clone();
invoices
.find(id)
.flat_map(move |found| match found {
None => fail(SqlError::RowNotFound),
Some(before) => update
.update(Invoice {
name,
..before.clone()
})
.map(move |after| (before, after)),
})
.narrow()
})
})
.map_err(AppError::from)
}
fn rename_audited(
dialect: Dialect,
id: i64,
name: String,
then_fail: bool,
) -> Effect<Invoice, AppError, RequestEnv> {
rename(dialect, id, name).flat_map(move |(before, after)| {
let entry = AuditEntry::new("invoice.rename", format!("invoice:{id}"))
.with_before(before.state())
.with_after(after.state());
audit::record(entry)
.map_err(AppError::from)
.flat_map(move |record| {
println!(
" audit: seq {} by {} for {:?}: {}",
record.seq, record.actor, record.tenant, record.action
);
if then_fail {
fail(AppError::Downstream("notification service unavailable"))
} else {
succeed(after)
}
})
})
}
fn names(invoices: &[Invoice]) -> Vec<&str> {
invoices.iter().map(|i| i.name.as_str()).collect()
}
async fn steps(db: &Db) -> Result<(), AppError> {
let d = db.dialect;
let suffix = if db.live {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |t| t.as_nanos());
format!("-{nanos}")
} else {
String::new()
};
let (acme, globex) = (format!("acme{suffix}"), format!("globex{suffix}"));
let ada = Caller {
user: "ada",
tenant: Some(acme.clone()),
};
let bob = Caller {
user: "bob",
tenant: Some(globex.clone()),
};
let anonymous = Caller {
user: "anonymous",
tenant: None,
};
db.request(&ada, create(d, Invoice::new(1, "draft", 100)))
.await?;
db.request(&ada, create(d, Invoice::new(2, "hosting", 40)))
.await?;
db.request(&bob, create(d, Invoice::new(3, "consulting", 70)))
.await?;
let mine = db.request(&ada, list(d)).await?;
let theirs = db.request(&bob, list(d)).await?;
println!(
"list: {acme} sees {:?}, {globex} sees {:?}",
names(&mine),
names(&theirs)
);
assert_eq!(names(&mine), ["draft", "hosting"]);
assert_eq!(names(&theirs), ["consulting"]);
let renamed = if db.live {
db.request(&ada, rename_audited(d, 1, "final".into(), false))
.await?
} else {
db.request(&ada, rename(d, 1, "final".into())).await?.1
};
assert_eq!((renamed.name.as_str(), renamed.version), ("final", 2));
println!("rename: {acme} renamed invoice 1 to {:?}", renamed.name);
match db.request(&bob, rename(d, 1, "hijacked".into())).await {
Err(AppError::Sql(SqlError::RowNotFound)) => {
println!("rename: {globex} cannot see invoice 1 (not found)")
}
other => panic!("expected not found, got {other:?}"),
}
match db.request(&anonymous, list(d)).await {
Err(AppError::NoTenant) => println!("list: a principal without a tenant is refused"),
other => panic!("expected a refusal, got {other:?}"),
}
let unscoped_by_mistake = Repository::<Invoice>::new(d).on(Arc::clone(&db.client));
match db.plain(unscoped_by_mistake.find_all()).await {
Err(SqlError::Unsupported(why)) => println!("repository without a tenant: {why}"),
other => panic!("expected a fail-closed error, got {other:?}"),
}
if !db.live {
println!("audit: skipped (PostgreSQL only: set DATABASE_URL or pass --live)");
return Ok(());
}
match db
.request(&ada, rename_audited(d, 2, "renamed".into(), true))
.await
{
Err(AppError::Downstream(why)) => println!("rename: rolled back ({why})"),
other => panic!("expected a downstream failure, got {other:?}"),
}
let after = db.request(&ada, list(d)).await?;
assert_eq!(names(&after), ["final", "hosting"]);
db.request(&ada, rename_audited(d, 2, "hosting 2026".into(), false))
.await?;
let verify = |tenant: String| {
audit::verify_chain(Some(tenant.as_str())).provide_all(hlist![
AuditLog::new(),
SqlClientService(Arc::clone(&db.client)),
])
};
let report = db.plain(verify(acme.clone())).await?;
assert!(report.is_intact(), "{report:?}");
assert_eq!(report.records, 2);
println!(
"verify: {acme}: {} records intact, head {}",
report.records,
report.head.as_deref().unwrap_or("-")
);
let other = db.plain(verify(globex.clone())).await?;
assert_eq!((other.records, other.is_intact()), (0, true));
let edited = db
.plain(db.client.execute(
"UPDATE audit_log SET after = '{\"name\": \"forged\"}' WHERE chain = $1 AND seq = 1",
&[acme.as_str().into()],
))
.await?;
assert_eq!(edited, 1);
let broken = db.plain(verify(acme.clone())).await?;
let link = broken.first_broken.expect("the edit is detected");
println!("verify after an edit: {link}");
assert_eq!(link.seq, 1);
Ok(())
}
async fn open() -> Result<Db, String> {
let live = std::env::args().skip(1).any(|a| a == "--live");
match std::env::var("DATABASE_URL") {
Ok(url) if !url.is_empty() => {
let pool = pg_pool_from_config(PgPoolConfig::from_url(url))
.await
.map_err(|e| format!("cannot build pool: {}", e.0))?;
let client: Arc<dyn SqlClient> = Arc::new(PgSqlClient::new(pool));
let db = Db {
client,
dialect: Dialect::Postgres,
live: true,
};
db.plain(audit::migrator().run_on(Arc::clone(&db.client)))
.await
.map_err(|e| format!("audit schema: {e}"))?;
for sql in ["DROP TABLE IF EXISTS example_050_invoices", TABLE] {
db.plain(db.client.execute(sql, &[]))
.await
.map_err(|e| format!("{sql}: {e}"))?;
}
Ok(db)
}
_ if live => Err("--live needs DATABASE_URL (postgres://user:pass@host:port/db)".into()),
_ => {
let pool = sqlite_pool_from_config(SqlitePoolConfig::memory())
.await
.map_err(|e| format!("sqlite: {}", e.0))?;
let db = Db {
client: Arc::new(SqliteSqlClient::new(pool)),
dialect: Dialect::Sqlite,
live: false,
};
db.plain(db.client.execute(TABLE, &[]))
.await
.map_err(|e| format!("{TABLE}: {e}"))?;
Ok(db)
}
}
}
#[tokio::main]
async fn main() -> ExitCode {
let db = match open().await {
Ok(db) => db,
Err(why) => {
eprintln!("050_tenancy_audit: {why}");
return ExitCode::from(2);
}
};
let mode = if db.live {
"on PostgreSQL: tenant filter, transaction, audit chain"
} else {
"SQLite in memory: tenant filter, transaction"
};
let outcome = steps(&db).await;
if db.live {
let _ = db
.plain(
db.client
.execute("DROP TABLE IF EXISTS example_050_invoices", &[]),
)
.await;
}
match outcome {
Ok(()) => {
println!("050_tenancy_audit ok ({mode})");
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("050_tenancy_audit failed ({mode}): {e:?}");
ExitCode::FAILURE
}
}
}