systemprompt_database/lifecycle/installation/
backfill.rs1use std::collections::BTreeSet;
35
36use pg_query::NodeEnum;
37use pg_query::protobuf::{self, Node};
38
39pub const PROJECTION_BACKLOG: &str = "projection_backlog";
40
41#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct BackfillStatement {
46 pub position: usize,
47 pub target: String,
48 pub form: &'static str,
49}
50
51impl BackfillStatement {
52 #[must_use]
53 pub fn is_projection_cleanup(&self, projection_tables: &BTreeSet<String>) -> bool {
54 self.form == "DELETE"
55 && (self.target == PROJECTION_BACKLOG || projection_tables.contains(&self.target))
56 }
57}
58
59#[must_use]
60pub fn projection_tables<'a>(schemas: impl IntoIterator<Item = &'a str>) -> BTreeSet<String> {
61 let mut queued = BTreeSet::new();
62 let mut created = BTreeSet::new();
63 for sql in schemas {
64 queued.extend(queued_projections(sql));
65 created.extend(created_tables(sql));
66 }
67 queued.intersection(&created).cloned().collect()
68}
69
70fn queued_projections(sql: &str) -> Vec<String> {
73 const CALL: &str = "queue_projection_backlog(";
74 sql.match_indices(CALL)
75 .filter_map(|(at, _)| {
76 let rest = sql[at + CALL.len()..].trim_start().strip_prefix('\'')?;
77 let end = rest.find('\'')?;
78 Some(rest[..end].to_owned())
79 })
80 .collect()
81}
82
83fn created_tables(sql: &str) -> Vec<String> {
84 let Ok(parsed) = pg_query::parse(sql) else {
85 return Vec::new();
86 };
87 parsed
88 .protobuf
89 .stmts
90 .iter()
91 .filter_map(|raw| match raw.stmt.as_ref()?.node.as_ref()? {
92 NodeEnum::CreateStmt(stmt) => Some(stmt.relation.as_ref()?.relname.clone()),
93 _ => None,
94 })
95 .collect()
96}
97
98#[must_use]
99pub fn backfill_statements(sql: &str) -> Vec<BackfillStatement> {
100 let Ok(parsed) = pg_query::parse(sql) else {
101 return Vec::new();
102 };
103 parsed
104 .protobuf
105 .stmts
106 .iter()
107 .filter_map(|raw| raw.stmt.as_ref().and_then(|s| s.node.as_ref()))
108 .enumerate()
109 .filter_map(|(index, node)| {
110 classify(node).map(|(target, form)| BackfillStatement {
111 position: index + 1,
112 target,
113 form,
114 })
115 })
116 .collect()
117}
118
119fn classify(node: &NodeEnum) -> Option<(String, &'static str)> {
120 match node {
121 NodeEnum::UpdateStmt(stmt) => Some((stmt.relation.as_ref()?.relname.clone(), "UPDATE")),
122 NodeEnum::DeleteStmt(stmt) => Some((stmt.relation.as_ref()?.relname.clone(), "DELETE")),
123 NodeEnum::InsertStmt(stmt) => {
124 let select = select_of(stmt.select_stmt.as_deref())?;
125 let table = stmt.relation.as_ref()?.relname.clone();
126 if table == PROJECTION_BACKLOG && is_keys_only(select) {
127 return None;
128 }
129 Some((table, "INSERT … SELECT"))
130 },
131 NodeEnum::SelectStmt(select) => {
132 refresh_call(select).map(|name| (name, "SELECT refresh_*()"))
133 },
134 _ => None,
135 }
136}
137
138fn select_of(node: Option<&Node>) -> Option<&protobuf::SelectStmt> {
141 match node?.node.as_ref()? {
142 NodeEnum::SelectStmt(select) if select.values_lists.is_empty() => Some(select),
143 _ => None,
144 }
145}
146
147fn is_keys_only(select: &protobuf::SelectStmt) -> bool {
153 let single_relation = select.from_clause.len() == 1
154 && matches!(
155 select.from_clause[0].node.as_ref(),
156 Some(NodeEnum::RangeVar(_))
157 );
158 single_relation
159 && select.target_list.len() == 2
160 && select.group_clause.is_empty()
161 && select.having_clause.is_none()
162 && select.with_clause.is_none()
163}
164
165fn refresh_call(select: &protobuf::SelectStmt) -> Option<String> {
166 select.target_list.iter().find_map(|target| {
167 let NodeEnum::ResTarget(res) = target.node.as_ref()? else {
168 return None;
169 };
170 let NodeEnum::FuncCall(call) = res.val.as_ref()?.node.as_ref()? else {
171 return None;
172 };
173 let name = call.funcname.last().and_then(|n| match n.node.as_ref()? {
174 NodeEnum::String(s) => Some(s.sval.clone()),
175 _ => None,
176 })?;
177 name.starts_with("refresh_").then_some(name)
178 })
179}