sea_schema/postgres/def/
constraints.rs

1#[cfg(feature = "with-serde")]
2use serde::{Deserialize, Serialize};
3
4use crate as sea_schema;
5
6#[derive(Clone, Debug, PartialEq)]
7#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
8/// An enum consisting of all constraints
9pub enum Constraint {
10    Check(Check),
11    NotNull(NotNull),
12    Unique(Unique),
13    PrimaryKey(PrimaryKey),
14    References(References),
15    Exclusion(Exclusion),
16}
17
18#[derive(Clone, Debug, PartialEq)]
19#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
20/// A constraint which states that a value must satisfy the following Boolean expression
21pub struct Check {
22    pub name: String,
23    /// The Boolean expression that must be satisfied
24    pub expr: String,
25    /// If marked with NO INHERIT, the constraint will not propogate to child tables
26    pub no_inherit: bool,
27}
28
29#[derive(Clone, Debug, PartialEq)]
30#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
31/// The constraint that a value must not be null
32pub struct NotNull;
33
34impl NotNull {
35    pub fn from_bool(boolean: bool) -> Option<NotNull> {
36        if boolean {
37            Some(NotNull)
38        } else {
39            None
40        }
41    }
42}
43
44#[derive(Clone, Debug, PartialEq)]
45#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
46/// That each set of values for these columns must be unique across the whole table
47pub struct Unique {
48    pub name: String,
49    pub columns: Vec<String>,
50}
51
52#[derive(Clone, Debug, PartialEq)]
53#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
54/// A constraint stating that the given columns act as a unique identifier for rows in the table.
55/// This implies that the columns are not null and are unique together
56pub struct PrimaryKey {
57    pub name: String,
58    pub columns: Vec<String>,
59}
60
61#[derive(Clone, Debug, PartialEq)]
62#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
63/// A constraint that column references the values appearing in the row of another table
64pub struct References {
65    pub name: String,
66    pub columns: Vec<String>,
67    pub table: String,
68    pub foreign_columns: Vec<String>,
69    pub on_update: Option<ForeignKeyAction>,
70    pub on_delete: Option<ForeignKeyAction>,
71}
72
73#[derive(Clone, Debug, PartialEq, sea_schema_derive::Name)]
74#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
75pub enum ForeignKeyAction {
76    #[name = "CASCADE"]
77    Cascade,
78    #[name = "SET NULL"]
79    SetNull,
80    #[name = "SET DEFAULT"]
81    SetDefault,
82    #[name = "RESTRICT"]
83    Restrict,
84    #[name = "NO ACTION"]
85    NoAction,
86}
87
88#[derive(Clone, Debug, PartialEq)]
89#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
90/// A constraint that ensures that, if any two rows are compared on the specified columns or
91/// expressions using the specified operators, at least one of these operator comparisons returns
92/// false or null
93pub struct Exclusion {
94    pub name: String,
95    pub using: String,
96    pub columns: Vec<String>,
97    pub operation: String,
98}