Skip to main content

no_mistakes_postgres_sql/
types.rs

1mod table_events;
2pub use table_events::SqlTableSchemaEvent;
3mod schema_file;
4pub use schema_file::{
5    SqlColumnMetadata, SqlCreateTableMetadata, SqlDeclaredIdentifier, SqlSchemaFileFacts,
6    SqlUnanalyzableStatement,
7};
8
9use std::fmt;
10use std::path::{Path, PathBuf};
11
12/// Options for selecting SQL schema files. There is no hardcoded migrations root.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct PostgresSchemaOptions {
15    pub sql_include: Vec<String>,
16}
17
18impl Default for PostgresSchemaOptions {
19    fn default() -> Self {
20        Self {
21            sql_include: vec!["**/*.sql".to_string()],
22        }
23    }
24}
25
26/// One key column of a btree index, including opclass and sort options.
27#[derive(Debug, Clone, PartialEq, Eq, Default)]
28pub struct SqlIndexParam {
29    pub name: Option<String>,
30    pub expression: String,
31    pub opclass: Option<String>,
32    pub ordering: Option<String>,
33    pub nulls_ordering: Option<String>,
34}
35
36/// One `DROP INDEX` so later migrations can remove earlier creates.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct SqlDropIndexMetadata {
39    pub name: String,
40    pub line: usize,
41    pub source_order: usize,
42}
43
44/// One `CREATE INDEX` or unique/primary-key covering index.
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct SqlCreateIndexMetadata {
47    pub table_name: String,
48    pub name: Option<String>,
49    pub leading_column: Option<String>,
50    pub columns: Vec<SqlIndexParam>,
51    pub include_columns: Vec<String>,
52    pub access_method: String,
53    pub unique: bool,
54    pub has_predicate: bool,
55    pub not_null_predicate_column: Option<String>,
56    pub predicate_key: Option<String>,
57    pub line: usize,
58    pub source_order: usize,
59}
60
61impl Default for SqlCreateIndexMetadata {
62    fn default() -> Self {
63        Self {
64            table_name: String::new(),
65            name: None,
66            leading_column: None,
67            columns: Vec::new(),
68            include_columns: Vec::new(),
69            access_method: "btree".to_string(),
70            unique: false,
71            has_predicate: false,
72            not_null_predicate_column: None,
73            predicate_key: None,
74            line: 1,
75            source_order: 0,
76        }
77    }
78}
79
80/// One `ALTER TABLE … ADD COLUMN`.
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct SqlAddColumnMetadata {
83    pub table_name: String,
84    /// Decoded last identifier component, preserving dots inside quoted names.
85    pub unqualified_table_name: String,
86    pub column_name: String,
87    pub data_type: String,
88    pub nullable: bool,
89    pub default: Option<String>,
90    /// Whether the column is computed by a GENERATED expression.
91    pub is_generated: bool,
92    pub line: usize,
93}
94
95/// One foreign key from `CREATE TABLE` or `ALTER TABLE`.
96#[derive(Debug, Clone, PartialEq, Eq)]
97pub struct SqlForeignKeyMetadata {
98    pub table_name: String,
99    pub column_names: Vec<String>,
100    pub referenced_table_name: String,
101    pub delete_action: Option<String>,
102    pub line: usize,
103}
104
105/// A named constraint add or `VALIDATE CONSTRAINT`.
106#[derive(Debug, Clone, PartialEq, Eq)]
107pub struct SqlNamedConstraint {
108    pub table_name: String,
109    pub name: String,
110    pub line: usize,
111}
112
113/// An unnamed `ALTER TABLE ADD` FOREIGN KEY or CHECK.
114#[derive(Debug, Clone, PartialEq, Eq)]
115pub struct SqlUnnamedConstraint {
116    pub table_name: String,
117    pub kind: String,
118    pub line: usize,
119}
120
121/// One schema-level statement kind recorded from PostgreSQL SQL.
122#[derive(Debug, Clone, PartialEq, Eq)]
123pub struct SqlStatementKind {
124    pub kind: String,
125    pub line: usize,
126}
127
128/// A syntactically recoverable PostgreSQL parameter change.
129#[derive(Debug, Clone, PartialEq, Eq)]
130pub struct SqlSettingUse {
131    pub name: String,
132    pub line: usize,
133}
134
135/// Path-aware extractor failure. Extractors return this instead of panicking.
136#[derive(Debug, Clone, PartialEq, Eq)]
137pub struct PostgresFactError {
138    pub path: Option<PathBuf>,
139    pub message: String,
140}
141
142impl PostgresFactError {
143    pub fn message(message: impl Into<String>) -> Self {
144        Self {
145            path: None,
146            message: message.into(),
147        }
148    }
149
150    pub fn for_path(path: &Path, message: impl Into<String>) -> Self {
151        Self {
152            path: Some(path.to_path_buf()),
153            message: message.into(),
154        }
155    }
156}
157
158impl fmt::Display for PostgresFactError {
159    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160        match &self.path {
161            Some(path) => write!(f, "{}: {}", path.display(), self.message),
162            None => f.write_str(&self.message),
163        }
164    }
165}
166
167impl std::error::Error for PostgresFactError {}
168
169mod policy_clause;
170#[doc(hidden)]
171pub use policy_clause::{SqlPolicyClause, SqlPolicyClauseUse};