1use crate::ast::identifiers::ObjectId;
4use crate::db::cache::{CACHE_V5_MAGIC, DbCache, DbCacheVersioned, ForeignKeyCache, IndexCache};
5use crate::db::cache_file::protect_cache_bytes;
6use crate::model::relation::{Persistence, RelationKind, RelationState};
7use anyhow::{Context, Result};
8use postgres::config::Host;
9use postgres::{Client, Config as PostgresConfig, NoTls};
10use std::io::Write;
11use std::path::Path;
12use std::time::{SystemTime, UNIX_EPOCH};
13use tempfile::NamedTempFile;
14
15#[cfg(windows)]
16use std::fs;
17
18pub fn sync_cache(
19 out_path: &Path,
20 schemas: Option<&[String]>,
21 cache_encryption: bool,
22) -> Result<()> {
23 let db_url = std::env::var("DATABASE_URL")
25 .context("DATABASE_URL environment variable is required to sync PostgreSQL schema metadata and statistics. Do not pass credentials via CLI flags or config files.")?;
26
27 let mut client = connect_database(&db_url)?;
28
29 let cache = populate_cache(&mut client, schemas)?;
30
31 write_cache(out_path, cache, cache_encryption)
32}
33
34fn connect_database(db_url: &str) -> Result<Client> {
35 let config: PostgresConfig = db_url
36 .parse()
37 .context("DATABASE_URL is not a valid PostgreSQL connection string")?;
38
39 if config
40 .get_hosts()
41 .iter()
42 .any(|host| matches!(host, Host::Tcp(name) if !is_local_host(name)))
43 {
44 anyhow::bail!(
45 "Remote DATABASE_URL connections are not supported by this build. Use an SSH tunnel and connect through localhost or a Unix socket."
46 );
47 }
48
49 config
50 .connect(NoTls)
51 .context("Failed to connect to PostgreSQL")
52}
53
54pub(crate) fn is_local_host(host: &str) -> bool {
55 if host.starts_with('/') || host.eq_ignore_ascii_case("localhost") {
56 return true;
57 }
58 host.trim_start_matches('[')
59 .trim_end_matches(']')
60 .parse::<std::net::IpAddr>()
61 .is_ok_and(|address| address.is_loopback())
62}
63
64pub(crate) fn cache_search_path(
65 database_search_path: Vec<String>,
66 schemas: Option<&[String]>,
67) -> Vec<String> {
68 let Some(schemas) = schemas else {
69 return database_search_path;
70 };
71
72 let mut scoped_search_path = Vec::new();
73 for schema in database_search_path
74 .into_iter()
75 .filter(|schema| schemas.contains(schema))
76 .chain(schemas.iter().cloned())
77 {
78 if !scoped_search_path.contains(&schema) {
79 scoped_search_path.push(schema);
80 }
81 }
82 scoped_search_path
83}
84
85pub(crate) fn parse_search_path_setting(setting: &str) -> Vec<String> {
88 let mut entries = Vec::new();
89 let mut current = String::new();
90 let mut chars = setting.chars().peekable();
91 let mut quoted = false;
92
93 while let Some(ch) = chars.next() {
94 match ch {
95 '"' if quoted && chars.peek() == Some(&'"') => {
96 current.push('"');
97 chars.next();
98 }
99 '"' => quoted = !quoted,
100 ',' if !quoted => {
101 let entry = current.trim();
102 if !entry.is_empty() {
103 entries.push(entry.to_string());
104 }
105 current.clear();
106 }
107 _ => current.push(ch),
108 }
109 }
110
111 let entry = current.trim();
112 if !entry.is_empty() {
113 entries.push(entry.to_string());
114 }
115 entries
116}
117
118pub(crate) fn relation_owner_id(owner_name: impl Into<String>) -> ObjectId {
119 ObjectId::new("", owner_name)
120}
121
122pub(crate) fn is_system_schema(schema: &str) -> bool {
123 schema == "information_schema" || schema.starts_with("pg_")
124}
125
126fn write_cache(out_path: &Path, cache: DbCache, cache_encryption: bool) -> Result<()> {
127 write_cache_with_protection(out_path, cache, |compressed| {
128 protect_cache_bytes(compressed, cache_encryption)
129 })
130}
131
132fn write_cache_with_protection(
133 out_path: &Path,
134 cache: DbCache,
135 protect: impl FnOnce(Vec<u8>) -> Result<Vec<u8>>,
136) -> Result<()> {
137 let parent = out_path.parent().unwrap_or_else(|| Path::new("."));
138 let mut temp_file = NamedTempFile::new_in(parent).with_context(|| {
139 format!(
140 "Failed to create temporary cache file beside {}",
141 out_path.display()
142 )
143 })?;
144 let mut compressed = Vec::new();
145 let mut encoder = zstd::stream::Encoder::new(&mut compressed, 3)
146 .context("Failed to init zstd compression")?;
147
148 encoder
149 .write_all(CACHE_V5_MAGIC)
150 .context("Failed to write cache V5 payload header")?;
151
152 let versioned = DbCacheVersioned::V5(Box::new(cache));
153 let bincode_config = bincode::config::standard().with_variable_int_encoding();
154
155 bincode::serde::encode_into_std_write(&versioned, &mut encoder, bincode_config)
156 .context("Failed bincode schema compilation and write")?;
157
158 encoder
159 .finish()
160 .context("Failed to flush final zstd stream to disk")?;
161
162 let cache_bytes = protect(compressed)?;
163 temp_file
164 .write_all(&cache_bytes)
165 .context("Failed to write cache payload")?;
166 temp_file.flush().context("Failed to flush cache payload")?;
167
168 replace_cache(temp_file, out_path)?;
169
170 Ok(())
171}
172
173#[cfg(not(windows))]
174fn replace_cache(temp_file: NamedTempFile, out_path: &Path) -> Result<()> {
175 temp_file
176 .persist(out_path)
177 .map_err(|error| error.error)
178 .with_context(|| {
179 format!(
180 "Failed to atomically replace cache file: {}",
181 out_path.display()
182 )
183 })?;
184 Ok(())
185}
186
187#[cfg(windows)]
188fn replace_cache(temp_file: NamedTempFile, out_path: &Path) -> Result<()> {
189 if !out_path.exists() {
190 temp_file
191 .persist(out_path)
192 .map_err(|error| error.error)
193 .with_context(|| format!("Failed to install cache file: {}", out_path.display()))?;
194 return Ok(());
195 }
196
197 let backup = out_path.with_extension("safe-migrate.backup");
198 fs::rename(out_path, &backup).with_context(|| {
199 format!(
200 "Failed to stage existing cache for replacement: {}",
201 out_path.display()
202 )
203 })?;
204
205 match temp_file.persist(out_path) {
206 Ok(_) => {
207 fs::remove_file(&backup).with_context(|| {
208 format!(
209 "Installed new cache but failed to remove backup: {}",
210 backup.display()
211 )
212 })?;
213 Ok(())
214 }
215 Err(error) => {
216 let restore_result = fs::rename(&backup, out_path);
217 let message = if let Err(restore_error) = restore_result {
218 format!(
219 "Failed to install new cache: {}. The old cache could not be restored: {}",
220 error.error, restore_error
221 )
222 } else {
223 format!(
224 "Failed to install new cache; restored the previous cache: {}",
225 error.error
226 )
227 };
228 Err(anyhow::anyhow!(message))
229 }
230 }
231}
232
233pub fn populate_cache(client: &mut Client, schemas: Option<&[String]>) -> Result<DbCache> {
234 let mut cache = DbCache::new();
235 let schema_values = schemas.map(|items| items.to_vec());
236 cache.metadata.created_at_unix_secs = Some(
237 SystemTime::now()
238 .duration_since(UNIX_EPOCH)
239 .unwrap_or_default()
240 .as_secs(),
241 );
242 cache.metadata.schemas = schema_values.clone();
243
244 let schema_filter = "AND ($1::text[] IS NULL OR n.nspname = ANY($1))";
245 let schema_filter_with_fk = r#"
246 AND (
247 $1::text[] IS NULL
248 OR n.nspname = ANY($1)
249 OR c.oid IN (
250 SELECT conrelid FROM pg_constraint cst
251 JOIN pg_class c2 ON c2.oid = cst.confrelid
252 JOIN pg_namespace n2 ON n2.oid = c2.relnamespace
253 WHERE n2.nspname = ANY($1)
254 )
255 OR c.oid IN (
256 SELECT confrelid FROM pg_constraint cst
257 JOIN pg_class c2 ON c2.oid = cst.conrelid
258 JOIN pg_namespace n2 ON n2.oid = c2.relnamespace
259 WHERE n2.nspname = ANY($1)
260 )
261 )
262 "#;
263 let schema_filter_n1_or_n2 =
264 "AND ($1::text[] IS NULL OR n1.nspname = ANY($1) OR n2.nspname = ANY($1))";
265 let schema_filter_nt = r#"
266 AND (
267 $1::text[] IS NULL
268 OR n_t.nspname = ANY($1)
269 OR t.oid IN (
270 SELECT conrelid FROM pg_constraint cst
271 JOIN pg_class c2 ON c2.oid = cst.confrelid
272 JOIN pg_namespace n2 ON n2.oid = c2.relnamespace
273 WHERE n2.nspname = ANY($1)
274 )
275 OR t.oid IN (
276 SELECT confrelid FROM pg_constraint cst
277 JOIN pg_class c2 ON c2.oid = cst.conrelid
278 JOIN pg_namespace n2 ON n2.oid = c2.relnamespace
279 WHERE n2.nspname = ANY($1)
280 )
281 )
282 "#;
283
284 let version_row = client.query_one("SHOW server_version_num;", &[])?;
286 let version_str: String = version_row.get(0);
287 cache.pg_version_num = version_str.parse::<u32>().ok();
288
289 let provenance_row = client.query_one(
290 "SELECT current_database(), current_user, session_user, current_setting('search_path');",
291 &[],
292 )?;
293 cache.metadata.source_database = Some(provenance_row.get(0));
294 cache.metadata.source_role = Some(provenance_row.get(1));
295 cache.metadata.source_session_role = Some(provenance_row.get(2));
296 let search_path_setting: String = provenance_row.get(3);
297 cache.metadata.source_search_path = Some(parse_search_path_setting(&search_path_setting));
298
299 let search_path_row = client.query_one("SELECT current_schemas(false);", &[])?;
304 cache.search_path = cache_search_path(search_path_row.get(0), schemas);
305
306 let schema_query = format!(
309 "SELECT n.nspname, pg_catalog.pg_get_userbyid(n.nspowner)
310 FROM pg_namespace n
311 WHERE n.nspname NOT LIKE 'pg\\_%' ESCAPE '\\'
312 AND n.nspname <> 'information_schema'
313 {schema_filter}
314 ORDER BY n.nspname;"
315 );
316 for row in client.query(&schema_query, &[&schema_values])? {
317 let name: String = row.get(0);
318 let owner: String = row.get(1);
319 cache.schemas.insert(
320 name.clone(),
321 crate::model::schema::SchemaState {
322 name,
323 owner: relation_owner_id(owner),
324 generation: 0,
325 },
326 );
327 }
328 cache
332 .search_path
333 .retain(|schema| cache.schemas.contains_key(schema));
334
335 let sequence_query = format!(
340 "SELECT
341 n.nspname AS sequence_schema,
342 s.relname AS sequence_name,
343 pg_catalog.pg_get_userbyid(s.relowner) AS owner_name,
344 tn.nspname AS table_schema,
345 t.relname AS table_name,
346 a.attname AS column_name,
347 d.deptype::text AS dependency_type,
348 CASE WHEN ad.adbin IS NULL THEN false
349 ELSE pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) LIKE '%nextval(%'
350 END AS has_nextval_default
351 FROM pg_class s
352 JOIN pg_namespace n ON n.oid = s.relnamespace
353 LEFT JOIN pg_depend d
354 ON d.classid = 'pg_class'::regclass
355 AND d.objid = s.oid
356 AND d.objsubid = 0
357 AND d.refclassid = 'pg_class'::regclass
358 AND d.deptype IN ('a', 'i')
359 LEFT JOIN pg_class t ON t.oid = d.refobjid
360 LEFT JOIN pg_namespace tn ON tn.oid = t.relnamespace
361 LEFT JOIN pg_attribute a
362 ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid
363 LEFT JOIN pg_attrdef ad
364 ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
365 WHERE s.relkind = 'S'
366 AND n.nspname NOT LIKE 'pg\\_%' ESCAPE '\\'
367 AND n.nspname <> 'information_schema'
368 {schema_filter}
369 ORDER BY n.nspname, s.relname;"
370 );
371 for row in client.query(&sequence_query, &[&schema_values])? {
372 let id = ObjectId::new(row.get::<_, String>(0), row.get::<_, String>(1));
373 let owner = relation_owner_id(row.get::<_, String>(2));
374 let table_schema: Option<String> = row.get(3);
375 let table_name: Option<String> = row.get(4);
376 let column_name: Option<String> = row.get(5);
377 let dependency_type: Option<String> = row.get(6);
378 let has_nextval_default: bool = row.get(7);
379 let owned_by = table_schema
380 .zip(table_name)
381 .zip(column_name)
382 .map(|((schema, table), column)| (ObjectId::new(schema, table), column));
383 let kind = match dependency_type.as_deref() {
384 Some("i") => crate::model::sequence::SequenceKind::Identity,
385 Some("a") if has_nextval_default => crate::model::sequence::SequenceKind::SerialLike,
386 Some("a") => crate::model::sequence::SequenceKind::Owned,
387 _ => crate::model::sequence::SequenceKind::Standalone,
388 };
389 cache.sequences.insert(
390 id.clone(),
391 crate::model::sequence::SequenceState {
392 id,
393 owner,
394 owned_by,
395 kind,
396 generation: 0,
397 },
398 );
399 }
400
401 let table_query = format!(
403 "
404 SELECT
405 n.nspname AS schema_name,
406 c.relname AS relation_name,
407 c.relkind AS relation_kind,
408 c.relpersistence AS persistence,
409 pg_catalog.pg_get_userbyid(c.relowner) AS owner_name,
410 CASE WHEN c.reltuples < 0 THEN -1 ELSE c.reltuples::bigint END AS estimated_rows,
411 c.relpages::bigint AS relpages,
412 to_char(s.last_analyze, 'YYYY-MM-DD HH24:MI:SS') AS last_analyze,
413 to_char(s.last_autoanalyze, 'YYYY-MM-DD HH24:MI:SS') AS last_autoanalyze,
414 p.partstrat::text AS partition_strategy
415 FROM pg_class c
416 JOIN pg_namespace n ON n.oid = c.relnamespace
417 LEFT JOIN pg_stat_user_tables s ON s.relid = c.oid
418 LEFT JOIN pg_partitioned_table p ON p.partrelid = c.oid
419 WHERE c.relkind IN ('r', 'p', 'v', 'm')
420 AND n.nspname NOT IN ('pg_catalog', 'information_schema')
421 {schema_filter_with_fk};
422 "
423 );
424
425 for row in client.query(&table_query, &[&schema_values])? {
426 let schema_name: String = row.get("schema_name");
427 let relation_name: String = row.get("relation_name");
428 let relkind: i8 = row.get("relation_kind");
429 let persistence_char: i8 = row.get("persistence");
430 let owner_name: String = row.get("owner_name");
431 let raw_rows: i64 = row.get("estimated_rows");
432 let relpages: i64 = row.get("relpages");
433
434 let last_analyze: Option<String> = row.get("last_analyze");
435 let last_autoanalyze: Option<String> = row.get("last_autoanalyze");
436
437 let object_id = ObjectId::new(&schema_name, &relation_name);
438
439 let kind = match relkind as u8 {
440 b'v' => RelationKind::View,
441 b'm' => RelationKind::MaterializedView,
442 _ => RelationKind::Table,
443 };
444
445 let persistence = match persistence_char as u8 {
446 b't' => Persistence::Temporary,
447 b'u' => Persistence::Unlogged,
448 _ => Persistence::Permanent,
449 };
450
451 let estimated_rows = if raw_rows < 0 {
452 None
453 } else {
454 Some(raw_rows as u64)
455 };
456
457 let mut state = RelationState::new(
458 object_id.clone(),
459 relation_owner_id(owner_name),
460 0,
461 estimated_rows,
462 kind,
463 persistence,
464 0,
465 );
466 state.relpages = Some(relpages as u64);
467 state.last_analyze = last_analyze;
468 state.last_autoanalyze = last_autoanalyze;
469
470 let partition_strategy: Option<String> = row.get("partition_strategy");
471 if let Some(ref strat) = partition_strategy {
472 state.partition_type = Some(match strat.as_str() {
473 "r" => "RANGE".to_string(),
474 "l" => "LIST".to_string(),
475 "h" => "HASH".to_string(),
476 _ => strat.to_uppercase(),
477 });
478 }
479
480 if let Some(s) = schemas
481 && !s.contains(&schema_name)
482 {
483 state.mark_fk_dependency();
484 }
485
486 cache.insert_baseline(object_id, state);
487 }
488
489 let col_query = format!("
491 SELECT
492 n.nspname AS schema_name,
493 c.relname AS relation_name,
494 a.attname AS column_name,
495 pg_catalog.format_type(a.atttypid, a.atttypmod) AS type_name,
496 a.attnotnull AS not_null,
497 s.avg_width AS avg_width,
498 pg_get_expr(ad.adbin, ad.adrelid) AS default_expr_text,
499 a.atttypmod AS type_modifier
500 FROM pg_attribute a
501 JOIN pg_class c ON a.attrelid = c.oid
502 JOIN pg_namespace n ON n.oid = c.relnamespace
503 LEFT JOIN pg_stats s ON s.schemaname = n.nspname AND s.tablename = c.relname AND s.attname = a.attname
504 LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
505 WHERE a.attnum > 0 AND NOT a.attisdropped
506 AND c.relkind IN ('r', 'p', 'v', 'm')
507 AND n.nspname NOT IN ('pg_catalog', 'information_schema')
508 {schema_filter_with_fk}
509 ORDER BY n.nspname, c.relname;
510 ");
511
512 for row in client.query(&col_query, &[&schema_values])? {
513 let schema_name: String = row.get("schema_name");
514 let relation_name: String = row.get("relation_name");
515 let column_name: String = row.get("column_name");
516 let type_name: String = row.get("type_name");
517 let not_null: bool = row.get("not_null");
518 let avg_width: Option<i32> = row.get("avg_width");
519 let default_expr_text: Option<String> = row.get("default_expr_text");
520 let type_modifier: Option<i32> = row.get("type_modifier");
521
522 let relation_id = ObjectId::new(&schema_name, &relation_name);
523 if let Some(rel) = cache.relations.get_mut(&relation_id) {
524 rel.columns.push(crate::model::column::Column {
525 name: column_name,
526 data_type: Some(type_name),
527 is_nullable: !not_null,
528 default: None,
529 avg_width,
530 default_expr_text,
531 type_modifier,
532 });
533 }
534 }
535
536 let tp_query = format!("
538 SELECT
539 n.nspname AS schema_name,
540 c.relname AS relation_name,
541 COALESCE(array_agg(DISTINCT t.tgname) FILTER (WHERE t.tgname IS NOT NULL AND t.tgisinternal = false), '{{}}') as triggers,
542 COALESCE(array_agg(DISTINCT p.polname) FILTER (WHERE p.polname IS NOT NULL), '{{}}') as policies
543 FROM pg_class c
544 JOIN pg_namespace n ON n.oid = c.relnamespace
545 LEFT JOIN pg_trigger t ON t.tgrelid = c.oid
546 LEFT JOIN pg_policy p ON p.polrelid = c.oid
547 WHERE c.relkind IN ('r', 'p', 'v', 'm') AND n.nspname NOT IN ('pg_catalog', 'information_schema')
548 {schema_filter_with_fk}
549 GROUP BY n.nspname, c.relname;
550 ");
551
552 for row in client.query(&tp_query, &[&schema_values])? {
553 let schema_name: String = row.get("schema_name");
554 let relation_name: String = row.get("relation_name");
555 let triggers: Vec<String> = row.get("triggers");
556 let policies: Vec<String> = row.get("policies");
557
558 let object_id = ObjectId::new(&schema_name, &relation_name);
559
560 if let Some(rel) = cache.relations.get_mut(&object_id) {
561 rel.triggers.extend(triggers);
562 rel.policies.extend(policies);
563 }
564 }
565
566 let acl_query = format!(
568 "
569 SELECT
570 n.nspname AS schema_name,
571 c.relname AS relation_name,
572 CASE
573 WHEN acl.grantee = 0 THEN 'public'
574 ELSE pg_catalog.pg_get_userbyid(acl.grantee)
575 END AS grantee,
576 acl.privilege_type
577 FROM pg_class c
578 JOIN pg_namespace n ON n.oid = c.relnamespace
579 CROSS JOIN LATERAL pg_catalog.aclexplode(c.relacl) acl
580 WHERE c.relkind IN ('r', 'p', 'v', 'm')
581 AND n.nspname NOT IN ('pg_catalog', 'information_schema')
582 AND acl.grantee <> c.relowner
583 {schema_filter_with_fk};
584 "
585 );
586
587 for row in client.query(&acl_query, &[&schema_values])? {
588 let schema_name: String = row.get("schema_name");
589 let relation_name: String = row.get("relation_name");
590 let grantee: String = row.get("grantee");
591 let privilege_type: String = row.get("privilege_type");
592 let privilege = match privilege_type.as_str() {
593 "SELECT" => crate::model::relation::Privilege::Select,
594 "INSERT" => crate::model::relation::Privilege::Insert,
595 "UPDATE" => crate::model::relation::Privilege::Update,
596 "DELETE" => crate::model::relation::Privilege::Delete,
597 "TRUNCATE" => crate::model::relation::Privilege::Truncate,
598 "REFERENCES" => crate::model::relation::Privilege::References,
599 "TRIGGER" => crate::model::relation::Privilege::Trigger,
600 _ => continue,
601 };
602 if let Some(relation) = cache
603 .relations
604 .get_mut(&ObjectId::new(&schema_name, &relation_name))
605 {
606 relation.privileges.grant(
607 ObjectId::new("", grantee),
608 [privilege].into_iter().collect(),
609 );
610 }
611 }
612
613 let trig_query = format!(
615 "
616 SELECT
617 n.nspname AS table_schema,
618 c.relname AS table_name,
619 t.tgname AS trigger_name,
620 t.tgenabled::text AS enabled_mode,
621 fn.nspname AS function_schema,
622 f.proname || '()' AS function_name
623 FROM pg_trigger t
624 JOIN pg_class c ON c.oid = t.tgrelid
625 JOIN pg_namespace n ON n.oid = c.relnamespace
626 JOIN pg_proc f ON f.oid = t.tgfoid
627 JOIN pg_namespace fn ON fn.oid = f.pronamespace
628 WHERE t.tgisinternal = false
629 AND n.nspname NOT IN ('pg_catalog', 'information_schema')
630 {schema_filter_with_fk};
631 "
632 );
633
634 for row in client.query(&trig_query, &[&schema_values])? {
635 let table_schema: String = row.get("table_schema");
636 let table_name: String = row.get("table_name");
637 let trigger_name: String = row.get("trigger_name");
638 let enabled_mode: String = row.get("enabled_mode");
639 let function_schema: String = row.get("function_schema");
640 let function_name: String = row.get("function_name");
641
642 cache.triggers.push(crate::db::cache::TriggerCache {
643 trigger_id: ObjectId::new(&table_schema, &trigger_name),
644 table_id: ObjectId::new(&table_schema, &table_name),
645 function_id: ObjectId::new(&function_schema, &function_name),
646 enabled_mode: crate::model::trigger::TriggerEnableMode::from_pg_code(&enabled_mode)
647 .ok_or_else(|| {
648 anyhow::anyhow!("unknown pg_trigger.tgenabled value {enabled_mode}")
649 })?,
650 });
651 }
652
653 let constraint_query = format!(
655 "
656 SELECT
657 n.nspname AS table_schema,
658 c.relname AS table_name,
659 con.conname AS constraint_name,
660 con.contype::text AS constraint_type,
661 con.convalidated AS validated
662 FROM pg_constraint con
663 JOIN pg_class c ON c.oid = con.conrelid
664 JOIN pg_namespace n ON n.oid = c.relnamespace
665 WHERE con.contype IN ('c', 'f', 'p', 'u', 'x')
666 AND n.nspname NOT IN ('pg_catalog', 'information_schema')
667 {schema_filter};
668 "
669 );
670
671 for row in client.query(&constraint_query, &[&schema_values])? {
672 let table_schema: String = row.get("table_schema");
673 let table_name: String = row.get("table_name");
674 let constraint_name: String = row.get("constraint_name");
675 let constraint_type: String = row.get("constraint_type");
676 let validated: bool = row.get("validated");
677 let kind = match constraint_type.as_str() {
678 "c" => crate::model::constraint::ConstraintKind::Check,
679 "f" => crate::model::constraint::ConstraintKind::ForeignKey,
680 "p" => crate::model::constraint::ConstraintKind::PrimaryKey,
681 "u" => crate::model::constraint::ConstraintKind::Unique,
682 "x" => crate::model::constraint::ConstraintKind::Exclusion,
683 _ => continue,
684 };
685 cache
686 .constraints
687 .push(crate::model::constraint::ConstraintState {
688 table_id: ObjectId::new(&table_schema, &table_name),
689 name: constraint_name,
690 kind,
691 validated,
692 });
693 }
694
695 let fk_query = format!(
697 "
698 SELECT
699 c.conname AS constraint_name,
700 n1.nspname AS from_schema, t1.relname AS from_table,
701 n2.nspname AS to_schema, t2.relname AS to_table
702 FROM pg_constraint c
703 JOIN pg_class t1 ON t1.oid = c.conrelid
704 JOIN pg_namespace n1 ON n1.oid = t1.relnamespace
705 JOIN pg_class t2 ON t2.oid = c.confrelid
706 JOIN pg_namespace n2 ON n2.oid = t2.relnamespace
707 WHERE c.contype = 'f'
708 {schema_filter_n1_or_n2};
709 "
710 );
711
712 for row in client.query(&fk_query, &[&schema_values])? {
713 let constraint_name: String = row.get("constraint_name");
714 let from_schema: String = row.get("from_schema");
715 let from_table: String = row.get("from_table");
716 let to_schema: String = row.get("to_schema");
717 let to_table: String = row.get("to_table");
718
719 if let Some(s) = schemas
720 && (!s.contains(&from_schema) || !s.contains(&to_schema))
721 {
722 let out_of_scope_schema = if !s.contains(&from_schema) {
724 &from_schema
725 } else {
726 &to_schema
727 };
728 let out_of_scope_table = if !s.contains(&from_schema) {
729 &from_table
730 } else {
731 &to_table
732 };
733 eprintln!(
734 "[WARN] Foreign key '{}' crosses schema boundary. Table '{}.{}' was pulled into cache as a dependency to evaluate cross-team locks.",
735 constraint_name, out_of_scope_schema, out_of_scope_table
736 );
737 }
738
739 cache.foreign_keys.push(ForeignKeyCache {
740 constraint_name,
741 from_table: ObjectId::new(&from_schema, &from_table),
742 to_table: ObjectId::new(&to_schema, &to_table),
743 });
744 }
745
746 let idx_query = format!(
748 "
749 SELECT
750 n_i.nspname AS index_schema, i.relname AS index_name,
751 n_t.nspname AS table_schema, t.relname AS table_name
752 FROM pg_index x
753 JOIN pg_class i ON i.oid = x.indexrelid
754 JOIN pg_namespace n_i ON n_i.oid = i.relnamespace
755 JOIN pg_class t ON t.oid = x.indrelid
756 JOIN pg_namespace n_t ON n_t.oid = t.relnamespace
757 WHERE x.indisvalid = true
758 AND n_i.nspname !~ '^pg_'
759 AND n_i.nspname <> 'information_schema'
760 AND n_t.nspname !~ '^pg_'
761 AND n_t.nspname <> 'information_schema'
762 {schema_filter_nt};
763 "
764 );
765
766 for row in client.query(&idx_query, &[&schema_values])? {
767 let index_schema: String = row.get("index_schema");
768 let index_name: String = row.get("index_name");
769 let table_schema: String = row.get("table_schema");
770 let table_name: String = row.get("table_name");
771
772 if is_system_schema(&index_schema) || is_system_schema(&table_schema) {
773 continue;
774 }
775
776 cache.indexes.push(IndexCache {
777 index_id: ObjectId::new(&index_schema, &index_name),
778 table_id: ObjectId::new(&table_schema, &table_name),
779 });
780 }
781
782 let func_query = format!(
784 "
785 SELECT
786 n.nspname AS schema_name,
787 p.proname AS func_name,
788 COALESCE(
789 (SELECT string_agg(pg_catalog.format_type(t, NULL), ',' ORDER BY n)
790 FROM unnest(p.proargtypes::int[]) WITH ORDINALITY AS u(t, n)),
791 ''
792 ) AS arg_types,
793 pg_catalog.pg_get_function_result(p.oid) AS return_type,
794 p.provolatile::text AS volatility,
795 l.lanname AS language,
796 p.prosecdef AS security_definer
797 FROM pg_proc p
798 JOIN pg_namespace n ON n.oid = p.pronamespace
799 JOIN pg_language l ON l.oid = p.prolang
800 WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
801 AND p.prokind = 'f'
802 {schema_filter};
803 "
804 );
805
806 for row in client.query(&func_query, &[&schema_values])? {
807 let schema_name: String = row.get("schema_name");
808 let func_name: String = row.get("func_name");
809 let arg_types_str: String = row.get("arg_types");
810 let return_type: Option<String> = row.get("return_type");
811 let volatility_char: String = row.get("volatility");
812 let language: String = row.get("language");
813 let security_definer: bool = row.get("security_definer");
814
815 let volatility = match volatility_char.as_str() {
816 "v" => crate::model::function::Volatility::Volatile,
817 "s" => crate::model::function::Volatility::Stable,
818 "i" => crate::model::function::Volatility::Immutable,
819 _ => crate::model::function::Volatility::Volatile,
820 };
821
822 let security = if security_definer {
823 crate::model::function::SecurityMode::Definer
824 } else {
825 crate::model::function::SecurityMode::Invoker
826 };
827
828 let arg_types_str = arg_types_str
830 .split(',')
831 .map(|s| s.trim().to_lowercase())
832 .collect::<Vec<_>>()
833 .join(",");
834
835 let id = ObjectId::new(&schema_name, format!("{}({})", func_name, arg_types_str));
836
837 let arg_types = if arg_types_str.is_empty() {
838 Vec::new()
839 } else {
840 arg_types_str.split(',').map(|s| s.to_string()).collect()
841 };
842
843 cache.functions.insert(
844 id.clone(),
845 crate::model::function::FunctionState {
846 id,
847 arg_types,
848 return_type: return_type.unwrap_or_default(),
849 volatility,
850 language,
851 security,
852 },
853 );
854 }
855
856 let type_query = format!(
858 "
859 SELECT
860 n.nspname AS schema_name,
861 t.typname AS type_name,
862 t.typtype::text AS type_kind,
863 CASE WHEN t.typtype = 'd'
864 THEN pg_catalog.format_type(t.typbasetype, t.typtypmod)
865 ELSE NULL
866 END AS domain_base_type,
867 COALESCE(
868 array_agg(e.enumlabel ORDER BY e.enumsortorder)
869 FILTER (WHERE e.enumlabel IS NOT NULL),
870 ARRAY[]::text[]
871 ) AS enum_labels
872 FROM pg_type t
873 JOIN pg_namespace n ON n.oid = t.typnamespace
874 LEFT JOIN pg_enum e ON e.enumtypid = t.oid
875 WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
876 AND t.typtype IN ('e', 'd')
877 {schema_filter}
878 GROUP BY n.nspname, t.typname, t.typtype, t.typbasetype, t.typtypmod;
879 "
880 );
881
882 for row in client.query(&type_query, &[&schema_values])? {
883 let schema_name: String = row.get("schema_name");
884 let type_name: String = row.get("type_name");
885 let type_kind: String = row.get("type_kind");
886 let domain_base_type: Option<String> = row.get("domain_base_type");
887 let enum_labels: Vec<String> = row.get("enum_labels");
888 let kind = match type_kind.as_str() {
889 "e" => crate::model::types::TypeKind::Enum {
890 variants: enum_labels,
891 },
892 "d" => crate::model::types::TypeKind::Domain {
893 base_type: domain_base_type.unwrap_or_default(),
894 },
895 _ => continue,
896 };
897 let id = ObjectId::new(&schema_name, &type_name);
898 cache.types.insert(
899 id.clone(),
900 crate::model::types::TypeState {
901 id,
902 generation: 0,
903 kind,
904 },
905 );
906 }
907
908 let depend_query = r#"
910 SELECT
911 d.classid, d.objid, d.objsubid,
912 d.refclassid, d.refobjid, d.refobjsubid,
913 d.deptype::text,
914 COALESCE(n1.nspname, n1p.nspname, n1t.nspname) AS obj_schema,
915 COALESCE(c1.relname, p1.proname, t1.typname) AS obj_name,
916 COALESCE(n2.nspname, n2p.nspname, n2t.nspname) AS ref_schema,
917 COALESCE(c2.relname, p2.proname, t2.typname) AS ref_name
918 FROM pg_depend d
919 LEFT JOIN pg_class c1 ON c1.oid = d.objid AND d.classid = 'pg_class'::regclass
920 LEFT JOIN pg_namespace n1 ON n1.oid = c1.relnamespace
921 LEFT JOIN pg_proc p1 ON p1.oid = d.objid AND d.classid = 'pg_proc'::regclass
922 LEFT JOIN pg_namespace n1p ON n1p.oid = p1.pronamespace
923 LEFT JOIN pg_type t1 ON t1.oid = d.objid AND d.classid = 'pg_type'::regclass
924 LEFT JOIN pg_namespace n1t ON n1t.oid = t1.typnamespace
925 LEFT JOIN pg_class c2 ON c2.oid = d.refobjid AND d.refclassid = 'pg_class'::regclass
926 LEFT JOIN pg_namespace n2 ON n2.oid = c2.relnamespace
927 LEFT JOIN pg_proc p2 ON p2.oid = d.refobjid AND d.refclassid = 'pg_proc'::regclass
928 LEFT JOIN pg_namespace n2p ON n2p.oid = p2.pronamespace
929 LEFT JOIN pg_type t2 ON t2.oid = d.refobjid AND d.refclassid = 'pg_type'::regclass
930 LEFT JOIN pg_namespace n2t ON n2t.oid = t2.typnamespace
931 WHERE d.deptype IN ('n', 'a', 'i')
932 AND COALESCE(n1.nspname, n1p.nspname, n1t.nspname) IS NOT NULL
933 AND COALESCE(n1.nspname, n1p.nspname, n1t.nspname)
934 NOT IN ('pg_catalog', 'information_schema')
935 AND (
936 $1::text[] IS NULL
937 OR COALESCE(n1.nspname, n1p.nspname, n1t.nspname) = ANY($1)
938 )
939 "#;
940
941 for row in client.query(depend_query, &[&schema_values])? {
942 let classid: u32 = row.get(0);
943 let objid: u32 = row.get(1);
944 let objsubid: i32 = row.get(2);
945 let refclassid: u32 = row.get(3);
946 let refobjid: u32 = row.get(4);
947 let refobjsubid: i32 = row.get(5);
948 let deptype: String = row.get(6);
949 let obj_schema: Option<String> = row.get(7);
950 let obj_name: Option<String> = row.get(8);
951 let ref_schema: Option<String> = row.get(9);
952 let ref_name: Option<String> = row.get(10);
953
954 cache.dependencies.push(crate::db::cache::DependencyCache {
955 classid,
956 objid,
957 objsubid,
958 refclassid,
959 refobjid,
960 refobjsubid,
961 deptype,
962 obj_schema,
963 obj_name,
964 ref_schema,
965 ref_name,
966 });
967 }
968
969 let view_depend_query = r#"
972 SELECT DISTINCT
973 'pg_class'::regclass::oid AS classid,
974 vc.oid AS objid,
975 0 AS objsubid,
976 'pg_class'::regclass::oid AS refclassid,
977 tc.oid AS refobjid,
978 0 AS refobjsubid,
979 vn.nspname AS obj_schema,
980 vc.relname AS obj_name,
981 tn.nspname AS ref_schema,
982 tc.relname AS ref_name
983 FROM pg_rewrite rw
984 JOIN pg_class vc ON vc.oid = rw.ev_class
985 JOIN pg_namespace vn ON vn.oid = vc.relnamespace
986 JOIN pg_depend d ON d.objid = rw.oid
987 JOIN pg_class tc ON tc.oid = d.refobjid
988 JOIN pg_namespace tn ON tn.oid = tc.relnamespace
989 WHERE vc.relkind IN ('v', 'm')
990 AND d.deptype = 'n'
991 -- PostgreSQL 14/15 expose an internal rewrite-rule self-edge. It is
992 -- not a dependency of the view definition and must not enter the
993 -- modeled dependency graph.
994 AND tc.oid <> vc.oid
995 AND (
996 $1::text[] IS NULL
997 OR (vn.nspname = ANY($1) AND tn.nspname = ANY($1))
998 )
999 "#;
1000
1001 for row in client.query(view_depend_query, &[&schema_values])? {
1002 cache.dependencies.push(crate::db::cache::DependencyCache {
1003 classid: row.get(0),
1004 objid: row.get(1),
1005 objsubid: row.get(2),
1006 refclassid: row.get(3),
1007 refobjid: row.get(4),
1008 refobjsubid: row.get(5),
1009 deptype: "view".to_string(),
1010 obj_schema: Some(row.get(6)),
1011 obj_name: Some(row.get(7)),
1012 ref_schema: Some(row.get(8)),
1013 ref_name: Some(row.get(9)),
1014 });
1015 }
1016
1017 for row in client.query(
1021 "SELECT rolname, rolcanlogin, rolsuper FROM pg_roles ORDER BY rolname;",
1022 &[],
1023 )? {
1024 let name: String = row.get(0);
1025 let id = ObjectId::new("", &name);
1026 cache.roles.insert(
1027 id.clone(),
1028 crate::model::role::RoleState {
1029 id,
1030 can_login: row.get(1),
1031 is_superuser: row.get(2),
1032 member_of: Vec::new(),
1033 can_set_role_to: Vec::new(),
1034 granted_privileges: Vec::new(),
1035 },
1036 );
1037 }
1038
1039 let membership_query = if cache.pg_version_num.unwrap_or_default() >= 160_000 {
1040 "SELECT member.rolname, parent.rolname, membership.set_option
1041 FROM pg_auth_members membership
1042 JOIN pg_roles member ON member.oid = membership.member
1043 JOIN pg_roles parent ON parent.oid = membership.roleid;"
1044 } else {
1045 "SELECT member.rolname, parent.rolname, true AS set_option
1046 FROM pg_auth_members membership
1047 JOIN pg_roles member ON member.oid = membership.member
1048 JOIN pg_roles parent ON parent.oid = membership.roleid;"
1049 };
1050 for row in client.query(membership_query, &[])? {
1051 let member = ObjectId::new("", row.get::<_, String>(0));
1052 let parent = ObjectId::new("", row.get::<_, String>(1));
1053 let set_option: bool = row.get(2);
1054 if let Some(role) = cache.roles.get_mut(&member) {
1055 role.member_of.push(parent.clone());
1056 if set_option {
1057 role.can_set_role_to.push(parent);
1058 }
1059 }
1060 }
1061
1062 Ok(cache)
1063}
1064
1065#[cfg(test)]
1066mod atomic_write_tests {
1067 use super::*;
1068 use crate::db::cache::DbCacheVersioned;
1069 use std::fs;
1070 use std::io::Read;
1071
1072 #[test]
1073 fn production_cache_writer_atomically_replaces_and_decodes() {
1074 let temp_dir = tempfile::tempdir().unwrap();
1075 let cache_path = temp_dir.path().join("baseline.cache");
1076 fs::write(&cache_path, b"old-cache").unwrap();
1077
1078 let mut cache = DbCache::new();
1079 cache.pg_version_num = Some(180002);
1080 write_cache(&cache_path, cache, false).unwrap();
1081
1082 let encoded = fs::read(&cache_path).unwrap();
1083 assert_ne!(encoded, b"old-cache");
1084 let reader = std::io::Cursor::new(encoded);
1085 let mut decoder = zstd::stream::Decoder::new(reader).unwrap();
1086 let mut payload = Vec::new();
1087 decoder.read_to_end(&mut payload).unwrap();
1088 let payload = payload
1089 .strip_prefix(CACHE_V5_MAGIC)
1090 .expect("writer must prefix V5 cache payloads");
1091 let config = bincode::config::standard().with_variable_int_encoding();
1092 let versioned: DbCacheVersioned = bincode::serde::decode_from_slice(payload, config)
1093 .unwrap()
1094 .0;
1095 assert_eq!(versioned.into_cache().unwrap().pg_version_num, Some(180002));
1096 assert_eq!(fs::read_dir(temp_dir.path()).unwrap().count(), 1);
1097 }
1098
1099 #[test]
1100 fn production_cache_writer_preserves_old_bytes_after_pre_install_failure() {
1101 let temp_dir = tempfile::tempdir().unwrap();
1102 let cache_path = temp_dir.path().join("baseline.cache");
1103 fs::write(&cache_path, b"known-good-cache").unwrap();
1104
1105 let error = write_cache_with_protection(&cache_path, DbCache::new(), |_| {
1106 Err(anyhow::anyhow!("injected payload-protection failure"))
1107 })
1108 .unwrap_err();
1109
1110 assert!(
1111 error
1112 .to_string()
1113 .contains("injected payload-protection failure")
1114 );
1115 assert_eq!(fs::read(&cache_path).unwrap(), b"known-good-cache");
1116 assert_eq!(fs::read_dir(temp_dir.path()).unwrap().count(), 1);
1117 }
1118}