Skip to main content

rango_orm_core/
schema.rs

1use crate::constraints::{Constraint, OrderBy};
2
3/// Represents the schema of a single table, as declared by a Model.
4#[derive(Debug, Clone)]
5pub struct TableSchema {
6    pub table_name: String,
7    pub columns: Vec<ColumnDef>,
8    pub constraints: Vec<Constraint>,
9    pub ordering: Vec<OrderBy>,
10    pub managed: bool,
11    pub comment: Option<String>,
12}
13
14impl Default for TableSchema {
15    fn default() -> Self {
16        Self {
17            table_name: String::new(),
18            columns: Vec::new(),
19            constraints: Vec::new(),
20            ordering: Vec::new(),
21            managed: true,
22            comment: None,
23        }
24    }
25}
26
27/// Definition of a single column.
28#[derive(Debug, Clone)]
29pub struct ColumnDef {
30    pub name: String,
31    pub col_type: ColumnType,
32    pub nullable: bool,
33    pub primary_key: bool,
34    pub unique: bool,
35    pub default: Option<DefaultValue>,
36    pub references: Option<FkReference>,
37    /// Create a b-tree index on this column.
38    pub index: bool,
39}
40
41/// Common column types, backend-agnostic.
42/// Backends map these to their native SQL types.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub enum ColumnType {
45    Bool,
46    SmallInt,
47    Int,
48    BigInt,
49    /// `INTEGER` primary key generated by the database (identity / auto-increment).
50    AutoInt,
51    /// `BIGINT` primary key generated by the database (identity / auto-increment).
52    AutoBigInt,
53    Float,
54    Double,
55    Decimal {
56        precision: u8,
57        scale: u8,
58    },
59    Text,
60    Varchar(u32),
61    Bytea,
62    Uuid,
63    Date,
64    Time,
65    DateTime,
66    Json,
67    Jsonb, // Postgres-specific — backends that don't support it fall back to Json
68}
69
70#[derive(Debug, Clone)]
71pub enum DefaultValue {
72    Literal(String),
73    CurrentTimestamp,
74    GeneratedUuid,
75}
76
77/// Foreign key reference.
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct FkReference {
80    pub table: String,
81    pub column: String,
82    pub on_delete: ReferentialAction,
83    pub on_update: ReferentialAction,
84}
85
86/// Standard SQL referential actions.
87#[derive(Debug, Clone, PartialEq, Eq)]
88pub enum ReferentialAction {
89    Cascade,
90    SetNull,
91    SetDefault,
92    Restrict,
93    NoAction,
94}