1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! Explicit transaction-scoped tenant binding.
//!
//! This is the right path when you are not inside the request-scoped pool
//! hook model from [`crate::pool`]: background jobs, one-off scripts,
//! admin tasks, queue consumers, or any code that already owns a
//! transaction and wants to scope a subset of work to one tenant.
//!
//! ## Default-config example
//!
//! ```no_run
//! # async fn run(pool: sqlx::PgPool, tenant: tenaxum::TenantId) -> sqlx::Result<()> {
//! use tenaxum::PgPoolExt;
//!
//! let mut tx = pool.begin_tenant(&tenant).await?;
//! sqlx::query("SELECT 1").execute(&mut *tx).await?;
//! tx.commit().await?;
//! # Ok(()) }
//! ```
//!
//! ## Custom-config example
//!
//! ```no_run
//! # async fn run(pool: sqlx::PgPool, tenant: tenaxum::TenantId) -> sqlx::Result<()> {
//! use tenaxum::Tenancy;
//!
//! let tenancy = Tenancy::new().guc("app.org_id");
//! let mut tx = tenancy.begin_tenant(&pool, &tenant).await?;
//! sqlx::query("SELECT 1").execute(&mut *tx).await?;
//! tx.commit().await?;
//! # Ok(()) }
//! ```
use crateTenancy;
use crateTenantId;
use ;
use Future;
/// Set the default GUC (`app.tenant_id`) on an open transaction via
/// `SET LOCAL`.
///
/// Equivalent to `Tenancy::default().set_tenant(tx, tenant)`. Use the
/// [`Tenancy`](crate::Tenancy) form if you need a custom GUC name.
///
/// ## `SET LOCAL` vs the pool-hook path
///
/// This function uses `set_config(..., true)` — the third argument
/// `true` makes the binding **transaction-scoped**. When the tx ends
/// (commit or rollback), the connection returns to the pool with no
/// lingering tenant binding.
///
/// The pool-hook path in [`crate::pool`] uses `set_config(..., false)`
/// (session-scoped) instead, because pool-checked-out connections can be
/// used outside an explicit transaction; the hooks compensate by running
/// `RESET <guc>` on `after_release`. The asymmetry is intentional —
/// pick the form that matches your call site:
///
/// | path | scope arg | reset by |
/// |------|-----------|----------|
/// | this function (in a tx) | `true` (LOCAL) | tx commit / rollback |
/// | `pool::with_tenant_hooks` | `false` (SESSION) | `after_release` hook |
pub async
/// Extension trait on [`sqlx::PgPool`] adding tenant-scoped transaction
/// helpers using the default [`Tenancy`].
///
/// Written as `-> impl Future<...> + Send` rather than `async fn` so the
/// returned future is `Send`-bounded — required for callers spawning the
/// future onto a multi-threaded runtime.
///
/// For a custom GUC name, use [`Tenancy::begin_tenant`] directly.