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