1use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
15pub struct SchemaModel {
16 #[serde(default)]
18 pub manage_comments: bool,
19 #[serde(default)]
21 pub manage_functions: bool,
22 pub schemas: Vec<String>,
24 pub tables: Vec<TableModel>,
26 #[serde(default)]
28 pub functions: Vec<FunctionModel>,
29}
30
31impl SchemaModel {
32 pub fn new(schemas: Vec<String>, tables: Vec<TableModel>) -> Self {
34 Self {
35 manage_comments: false,
36 manage_functions: false,
37 schemas,
38 tables,
39 functions: Vec::new(),
40 }
41 }
42
43 pub fn with_functions(
45 schemas: Vec<String>,
46 tables: Vec<TableModel>,
47 functions: Vec<FunctionModel>,
48 ) -> Self {
49 let manage_comments = tables.iter().any(|table| {
50 table.comment.is_some() || table.columns.iter().any(|column| column.comment.is_some())
51 }) || functions.iter().any(|function| function.comment.is_some());
52 Self {
53 manage_comments,
54 manage_functions: true,
55 schemas,
56 tables,
57 functions,
58 }
59 }
60
61 pub fn from_tables(tables: Vec<TableModel>) -> Self {
63 let mut schemas = Vec::new();
64 for t in &tables {
65 if t.schema != "public" && !schemas.contains(&t.schema) {
66 schemas.push(t.schema.clone());
67 }
68 }
69 let manage_comments = tables.iter().any(|table| {
70 table.comment.is_some() || table.columns.iter().any(|column| column.comment.is_some())
71 });
72 Self {
73 manage_comments,
74 manage_functions: false,
75 schemas,
76 tables,
77 functions: Vec::new(),
78 }
79 }
80
81 pub fn is_empty(&self) -> bool {
83 self.tables.is_empty() && (!self.manage_functions || self.functions.is_empty())
84 }
85}
86
87#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
89pub struct TableModel {
90 pub schema: String,
92 pub name: String,
94 pub rename_from: Option<String>,
97 #[serde(default)]
99 pub comment: Option<String>,
100 pub columns: Vec<ColumnModel>,
102 pub primary_key: Option<PrimaryKeyModel>,
104 pub unique_constraints: Vec<UniqueConstraintModel>,
106 pub foreign_keys: Vec<ForeignKeyModel>,
108 pub indexes: Vec<IndexModel>,
110}
111
112impl TableModel {
113 pub fn key(&self) -> (String, String) {
115 (self.schema.clone(), self.name.clone())
116 }
117}
118
119#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
121pub struct ColumnModel {
122 pub name: String,
124 pub sql_type: String,
126 pub nullable: bool,
128 pub primary_key: bool,
130 pub rename_from: Option<String>,
132 pub default_sql: Option<String>,
134 #[serde(default)]
136 pub comment: Option<String>,
137}
138
139#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
141pub struct PrimaryKeyModel {
142 pub name: Option<String>,
144 pub columns: Vec<String>,
146}
147
148#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
150pub struct UniqueConstraintModel {
151 pub name: Option<String>,
153 pub columns: Vec<String>,
155}
156
157#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
159pub struct ForeignKeyModel {
160 pub name: Option<String>,
162 pub columns: Vec<String>,
164 pub ref_table: String,
166 pub ref_columns: Vec<String>,
168 pub on_delete: Option<String>,
170 pub on_update: Option<String>,
172}
173
174#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
176pub struct IndexModel {
177 pub name: Option<String>,
179 pub columns: Vec<String>,
181 pub unique: bool,
183 pub predicate: Option<String>,
185}
186
187#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
193pub struct FunctionModel {
194 pub schema: String,
195 pub name: String,
196 #[serde(default)]
197 pub identity_arguments: String,
198 pub definition_sql: String,
199 #[serde(default)]
200 pub comment: Option<String>,
201}
202
203impl FunctionModel {
204 pub fn key(&self) -> (String, String, String) {
206 (
207 self.schema.clone(),
208 self.name.clone(),
209 normalize_identity_arguments(&self.identity_arguments),
210 )
211 }
212}
213
214pub fn normalize_identity_arguments(arguments: &str) -> String {
216 arguments
217 .split(',')
218 .map(str::trim)
219 .collect::<Vec<_>>()
220 .join(", ")
221}