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 table_identity: Option<String>,
49    pub name: Option<String>,
50    pub raw_name: Option<String>,
51    pub leading_column: Option<String>,
52    pub columns: Vec<SqlIndexParam>,
53    pub include_columns: Vec<String>,
54    pub access_method: String,
55    pub unique: bool,
56    pub has_predicate: bool,
57    pub not_null_predicate_column: Option<String>,
58    pub predicate_key: Option<String>,
59    pub line: usize,
60    pub source_order: usize,
61}
62
63impl Default for SqlCreateIndexMetadata {
64    fn default() -> Self {
65        Self {
66            table_name: String::new(),
67            table_identity: None,
68            name: None,
69            raw_name: None,
70            leading_column: None,
71            columns: Vec::new(),
72            include_columns: Vec::new(),
73            access_method: "btree".to_string(),
74            unique: false,
75            has_predicate: false,
76            not_null_predicate_column: None,
77            predicate_key: None,
78            line: 1,
79            source_order: 0,
80        }
81    }
82}
83
84/// One `ALTER TABLE … ADD COLUMN`.
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub struct SqlAddColumnMetadata {
87    pub table_name: String,
88    /// Decoded last identifier component, preserving dots inside quoted names.
89    pub unqualified_table_name: String,
90    pub column_name: String,
91    pub data_type: String,
92    pub nullable: bool,
93    pub default: Option<String>,
94    /// Whether the column is computed by a GENERATED expression.
95    pub is_generated: bool,
96    pub line: usize,
97}
98
99/// One foreign key from `CREATE TABLE` or `ALTER TABLE`.
100#[derive(Debug, Clone, PartialEq, Eq)]
101pub struct SqlForeignKeyMetadata {
102    pub table_name: String,
103    pub column_names: Vec<String>,
104    pub referenced_table_name: String,
105    pub delete_action: Option<String>,
106    pub line: usize,
107}
108
109/// A named constraint add or `VALIDATE CONSTRAINT`.
110#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct SqlNamedConstraint {
112    pub table_name: String,
113    pub name: String,
114    pub line: usize,
115}
116
117/// An unnamed `ALTER TABLE ADD` FOREIGN KEY or CHECK.
118#[derive(Debug, Clone, PartialEq, Eq)]
119pub struct SqlUnnamedConstraint {
120    pub table_name: String,
121    pub kind: String,
122    pub line: usize,
123}
124
125/// One schema-level statement kind recorded from PostgreSQL SQL.
126#[derive(Debug, Clone, PartialEq, Eq)]
127pub struct SqlStatementKind {
128    pub kind: String,
129    pub line: usize,
130}
131
132/// A syntactically recoverable PostgreSQL parameter change.
133#[derive(Debug, Clone, PartialEq, Eq)]
134pub struct SqlSettingUse {
135    pub name: String,
136    pub line: usize,
137}
138
139/// Path-aware extractor failure. Extractors return this instead of panicking.
140#[derive(Debug, Clone, PartialEq, Eq)]
141pub struct PostgresFactError {
142    pub path: Option<PathBuf>,
143    pub message: String,
144}
145
146impl PostgresFactError {
147    pub fn message(message: impl Into<String>) -> Self {
148        Self {
149            path: None,
150            message: message.into(),
151        }
152    }
153
154    pub fn for_path(path: &Path, message: impl Into<String>) -> Self {
155        Self {
156            path: Some(path.to_path_buf()),
157            message: message.into(),
158        }
159    }
160}
161
162impl fmt::Display for PostgresFactError {
163    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164        match &self.path {
165            Some(path) => write!(f, "{}: {}", path.display(), self.message),
166            None => f.write_str(&self.message),
167        }
168    }
169}
170
171impl std::error::Error for PostgresFactError {}
172
173mod policy_clause;
174#[doc(hidden)]
175pub use policy_clause::{SqlPolicyClause, SqlPolicyClauseUse};
176
177mod query_complexity;
178pub use query_complexity::{SqlComplexQuery, SqlQueryComplexity};