cratestack_sql/descriptor/unique_columns.rs
1//! Which columns an `UPDATE` cannot change without taking `FOR UPDATE`.
2
3use super::ModelDescriptor;
4
5impl<M, PK> ModelDescriptor<M, PK> {
6 /// Record the model's `@unique` / `@@unique` columns. A separate step from
7 /// [`ModelDescriptor::new`], like [`ModelDescriptor::with_subscribed`].
8 pub const fn with_unique_columns(self, unique_columns: &'static [&'static str]) -> Self {
9 Self {
10 unique_columns,
11 ..self
12 }
13 }
14
15 /// Whether changing `column` is a key update: the primary key or a unique
16 /// column. Postgres then takes `FOR UPDATE` on the row, not `FOR NO KEY
17 /// UPDATE`, because a foreign key may point at the old value.
18 pub fn is_key_column(&self, column: &str) -> bool {
19 column == self.primary_key || self.unique_columns.contains(&column)
20 }
21}