no_mistakes_postgres_sql/
migration.rs1use super::schema::relation_name;
2use super::types::SqlSchemaFileFacts;
3use sqlparser::ast::{ObjectName, ObjectType, Statement};
4mod constraints;
5mod definitions;
6mod dynamic;
7mod dynamic_merge;
8mod identifiers;
9mod indexes;
10mod locating;
11pub(super) use locating::qualified_relation;
12pub(crate) use locating::statement_keyword_position;
13mod predicate;
14mod recovery;
15mod statements;
16mod table_events;
17mod trigger_locks;
18#[doc(hidden)]
19pub use recovery::recovery_failures;
20pub(crate) use trigger_locks::{body_facts as extract_migration_body_facts, TriggerSource};
21pub fn extract_migration_facts(sql: &str) -> SqlSchemaFileFacts {
22 let parsed = super::parse::parse_postgres_sql_with_function_sources(sql);
23 recovery::extract_located_migration_facts(sql, parsed, false, TriggerSource::default())
24}
25
26#[doc(hidden)]
27pub fn extract_from_parsed_with_failures(
28 sql: &str,
29 statements: &[Statement],
30 functions: &[super::SqlFunctionCallFact],
31 failures: &[super::types::SqlUnanalyzableStatement],
32 tokens: &[&[sqlparser::tokenizer::TokenWithSpan]],
33 statement_locations: Option<&[sqlparser::tokenizer::Location]>,
34) -> SqlSchemaFileFacts {
35 let mut facts = extract_parsed_migration_facts(
36 sql,
37 statements,
38 failures,
39 tokens,
40 statement_locations,
41 false,
42 TriggerSource::default(),
43 );
44 facts.function_calls.extend_from_slice(functions);
45 facts
46}
47
48pub(crate) use statements::policy_facts_with_locations;
49#[doc(hidden)]
50pub use statements::{collect_query_complexity, SUPPORTED_KINDS, TRANSACTION_CONTROL_KINDS};
51
52fn extract_parsed_migration_facts(
53 sql: &str,
54 statements: &[Statement],
55 failures: &[super::types::SqlUnanalyzableStatement],
56 tokens: &[&[sqlparser::tokenizer::TokenWithSpan]],
57 statement_locations: Option<&[sqlparser::tokenizer::Location]>,
58 procedural_body: bool,
59 trigger: TriggerSource<'_>,
60) -> SqlSchemaFileFacts {
61 let mut facts = SqlSchemaFileFacts {
62 unanalyzable_statements: failures
63 .iter()
64 .filter(|failure| failure.schema_unanalyzable)
65 .cloned()
66 .collect(),
67 table_events_collected: true,
68 tables: statements
69 .iter()
70 .filter_map(|statement| match statement {
71 Statement::CreateTable(table) => Some(super::schema::table_metadata(table)),
72 _ => None,
73 })
74 .collect(),
75 ..Default::default()
76 };
77 let transactions = (!procedural_body && failures.is_empty()).then_some((
78 statements,
79 tokens,
80 statement_locations,
81 ));
82 let mut barriers = definitions::Barriers::new(statements::record_migration_policy(
83 sql,
84 &mut facts,
85 transactions,
86 ));
87 let mut identifier_locations = identifiers::Locations::new(sql);
88 facts
89 .declared_identifiers
90 .extend(identifier_locations.unparsed_declarations());
91 let mut table_positions = table_events::Positions::new(sql);
92 for (ordinal, statement) in statements.iter().enumerate() {
93 let tokens = tokens.get(ordinal).copied().unwrap_or_default();
94 let locations = locating::Locations::new(tokens);
95 facts
96 .declared_identifiers
97 .extend(identifiers::collect(&mut identifier_locations, statement));
98 let position = locations.statement_position(
99 statement,
100 statement_locations
101 .and_then(|locs| locs.get(ordinal))
102 .copied(),
103 );
104 let line = position.line.max(1) as usize;
105 statements::record(line, statement, tokens, procedural_body, &mut facts);
106 if !procedural_body {
107 barriers.record(statement, position, &mut facts.definition_events);
108 }
109 table_events::record(statement, &mut table_positions, &mut facts.table_events);
110 match statement {
111 Statement::CreateIndex(index) => {
112 facts
113 .indexes
114 .push(indexes::from_create_index(line, ordinal, index));
115 }
116 Statement::Drop {
117 object_type: ObjectType::Index,
118 names,
119 ..
120 } => {
121 facts
122 .dropped_indexes
123 .extend(indexes::from_drop_index(line, ordinal, names));
124 }
125 Statement::Drop {
126 object_type: ObjectType::Table,
127 names,
128 ..
129 } => {
130 facts
131 .dropped_tables
132 .extend(indexes::from_drop_table(line, ordinal, names));
133 }
134 Statement::CreateTable(table) => {
135 let table_name = relation_name(&table.name);
136 facts.indexes.extend(indexes::covering_from_table(
137 &qualified_relation(&table.name),
138 table,
139 ));
140 constraints::collect_create_table_fks(
141 line,
142 &table_name,
143 table,
144 &mut facts.foreign_keys,
145 );
146 }
147 Statement::AlterTable(alter) => {
148 constraints::collect_alter_table(line, alter, &mut facts)
149 }
150 _ => {}
151 }
152 }
153 if !procedural_body {
154 barriers.finish(&mut facts.definition_events);
155 }
156 dynamic_merge::append(sql, &mut facts, trigger);
157 facts.trigger_row_lock_lines.sort_unstable();
158 facts.trigger_row_lock_lines.dedup();
159 if let Some(ddl) = &mut facts.ddl_projection {
160 ddl.unanalyzable_statements.sort_by_key(|item| item.line);
161 table_positions.apply_transaction_projection(&mut ddl.table_events);
162 }
163 facts.unanalyzable_statements.sort_by_key(|item| item.line);
164 table_positions.apply_transaction_projection(&mut facts.table_events);
165 facts
166}
167
168pub(super) fn relation(name: &ObjectName) -> String {
169 relation_name(name)
170}
171
172#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
173mod tests;