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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! `INSERT … ON CONFLICT (<pk>) DO UPDATE …`, but with the
//! create/update distinction *resolved* rather than merely guessed, so
//! we can:
//!
//! * pick the right policy slot (both must allow at call time)
//! * emit the correct ModelEventKind (Created vs Updated)
//! * capture an audit `before` snapshot only on the update branch
//!
//! A `SELECT … FOR UPDATE` probe inside the same transaction predicts
//! the branch — binding when it finds a row (it holds the lock), a
//! guess when it does not — so the statement itself gets the last word.
//! See `upsert_resolve`'s module doc for how a mispredicted insert is
//! recovered into a proper update (cratestack#745).
//!
//! The upsert is always transactional regardless of whether the model
//! emits events or has `@@audit`. One extra round-trip for the
//! SELECT, in exchange for clean event/audit semantics. Upsert is not
//! a hot read path — callers who need raw insert/update throughput
//! should use `.create()` / `.update()` directly.
use cratestack_core::{CratestackContext, CratestackError};
use crate::audit::{RunInTxOutcome, dispatch_audit_sink};
use crate::{
ConflictTarget, ModelDescriptor, SqlxRuntime, UpsertModelInput, cratestack_error_from_sqlx,
sqlx,
};
use super::upsert_do_nothing::UpsertRecordDoNothing;
use super::upsert_exec::run_upsert_in_tx;
#[derive(Debug, Clone)]
pub struct UpsertRecord<'a, M: 'static, PK: 'static, I> {
pub(crate) runtime: &'a SqlxRuntime,
pub(crate) descriptor: &'static ModelDescriptor<M, PK>,
pub(crate) input: I,
pub(crate) conflict_target: ConflictTarget,
}
impl<'a, M: 'static, PK: 'static, I> UpsertRecord<'a, M, PK, I>
where
I: UpsertModelInput<M>,
{
/// Choose the conflict target. Defaults to the model's primary
/// key; pass [`ConflictTarget::columns`] to upsert on a composite
/// unique key instead. The named columns must form a `UNIQUE`
/// constraint/index on the target table.
pub fn on_conflict(mut self, target: ConflictTarget) -> Self {
self.conflict_target = target;
self
}
/// Switch this call to `ON CONFLICT ... DO NOTHING` semantics
/// (cratestack#487), independent of `descriptor.upsert_update_columns`:
/// on conflict, leave the existing row completely untouched instead
/// of merging `upsert_update_columns` into it. This is the
/// idempotent-insert shape ledger-style writes need — e.g. a
/// cash-in claim that inserts a `PENDING` row and treats a
/// conflict as "already in flight" must never let a retry's blank
/// values overwrite an existing `COMPLETED` row's `transfer_ref`.
///
/// Returns a distinct builder type rather than a flag on
/// `UpsertRecord` because the return shape genuinely changes: a
/// real `DO NOTHING` returns nothing on conflict, so the caller
/// needs `Inserted` vs `Existing` distinguishable in the type
/// ([`crate::UpsertOutcome`]) rather than collapsed into a plain
/// `M` the way `.run()` returns it today. Encoding that as a
/// separate type also means existing `.upsert(..).run(..)` callers
/// keep their `Result<M, CratestackError>` signature unchanged — this is
/// purely additive, not a behavior change for the DO UPDATE path.
pub fn do_nothing(self) -> UpsertRecordDoNothing<'a, M, PK, I> {
UpsertRecordDoNothing {
runtime: self.runtime,
descriptor: self.descriptor,
input: self.input,
conflict_target: self.conflict_target,
}
}
/// Render an approximate SQL preview. The actual upsert wraps a
/// `SELECT … FOR UPDATE` around the `INSERT … ON CONFLICT`, but
/// this preview returns only the conflict-bearing statement.
///
/// Deliberately does NOT call [`ConflictTarget::validate`]
/// (cratestack#741 finding 3) — every other `preview_sql()` in
/// this codebase (`find_many`, `create`, `update`, `delete`, …)
/// returns a bare `String`, no `Result`, so it can never fail;
/// matching that established shape here means `cratestack-studio`'s
/// interactive SQL-preview tooling can keep calling it uniformly
/// across every builder without a special case for upsert. The
/// combination this would reject (a predicate on
/// [`ConflictTarget::PrimaryKey`]) is still caught before any SQL
/// *runs* — [`Self::run`]/[`Self::run_in_tx`] call `.validate()` via
/// `prepare_upsert_insert` first — so nothing unsafe executes;
/// only the preview string itself can show a `WHERE` clause paired
/// with a target `.validate()` would reject. A caller that wants to
/// know ahead of rendering can call `.validate()` on the same
/// `ConflictTarget` value passed to `.on_conflict(..)`.
pub fn preview_sql(&self) -> String {
let values = self.input.sql_values();
let placeholders = (1..=values.len())
.map(|index| format!("${index}"))
.collect::<Vec<_>>()
.join(", ");
let columns = values
.iter()
.map(|value| value.column)
.collect::<Vec<_>>()
.join(", ");
let update_assignments = self
.descriptor
.upsert_update_columns
.iter()
.map(|column| format!("{column} = EXCLUDED.{column}"))
.collect::<Vec<_>>()
.join(", ");
let version_bump = match self.descriptor.version_column {
Some(col) => format!(
", {col} = {table}.{col} + 1",
table = self.descriptor.table_name,
col = col
),
None => String::new(),
};
let conflict_tuple = match self.conflict_target.as_columns() {
None => self.descriptor.primary_key.to_owned(),
Some(cols) => cols.join(", "),
};
let conflict_predicate = match self.conflict_target.predicate() {
Some(predicate) => format!(" WHERE {predicate}"),
None => String::new(),
};
format!(
"INSERT INTO {table} ({columns}) VALUES ({placeholders}) \
ON CONFLICT ({conflict_tuple}){conflict_predicate} DO UPDATE SET {update_assignments}{version_bump} \
RETURNING {projection}",
table = self.descriptor.table_name,
projection = self.descriptor.select_projection(),
)
}
pub async fn run(self, ctx: &CratestackContext) -> Result<M, CratestackError>
where
for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
{
let runtime = self.runtime;
let mut tx = runtime
.pool()
.begin()
.await
.map_err(cratestack_error_from_sqlx)?;
let (record, emits_event, audit_event) = run_upsert_in_tx(
&mut tx,
runtime,
self.descriptor,
self.input,
self.conflict_target,
ctx,
)
.await?;
tx.commit().await.map_err(cratestack_error_from_sqlx)?;
if emits_event {
let _ = runtime.drain_event_outbox().await;
}
if let Some(event) = &audit_event {
dispatch_audit_sink(runtime, std::slice::from_ref(event)).await;
}
Ok(record)
}
/// Like [`Self::run`] but participates in a caller-supplied
/// transaction. The conflict probe runs against `tx`, so the row
/// lock is held until the caller commits. Neither the event outbox
/// drain nor the `AuditSink` fan-out happens here — see
/// `create.rs`'s `run_in_tx` doc comment for the full contract and
/// how a caller opts into both after their own commit.
pub async fn run_in_tx<'tx>(
self,
tx: &mut sqlx::Transaction<'tx, sqlx::Postgres>,
ctx: &CratestackContext,
) -> Result<RunInTxOutcome<M>, CratestackError>
where
for<'r> M: Send + Unpin + sqlx::FromRow<'r, sqlx::postgres::PgRow> + serde::Serialize,
PK: Send + sqlx::Type<sqlx::Postgres> + for<'q> sqlx::Encode<'q, sqlx::Postgres>,
{
let (record, _emits_event, audit_event) = run_upsert_in_tx(
tx,
self.runtime,
self.descriptor,
self.input,
self.conflict_target,
ctx,
)
.await?;
Ok(RunInTxOutcome::new(
record,
audit_event.into_iter().collect(),
))
}
}