diff --git a/backend/windmill-api-workspaces/src/datatable_extensions.rs b/backend/windmill-api-workspaces/src/datatable_extensions.rs index e71ae33288..951558a362 100644 --- a/backend/windmill-api-workspaces/src/datatable_extensions.rs +++ b/backend/windmill-api-workspaces/src/datatable_extensions.rs @@ -45,7 +45,8 @@ pub(crate) struct ExtensionPlan { /// In creation order, so an extension comes after those it requires. extensions: Vec, /// Schemas holding an extension that the dump creates too. The copy needs them before its - /// extensions, so they are made first and the dump's `CREATE SCHEMA` is not replayed. + /// extensions, so they are made first, with their comments, and the dump's `CREATE SCHEMA` and + /// `COMMENT ON SCHEMA` are not replayed: the copy's role may not own one it did not make. extension_schemas: Vec, /// The source objects whose archive entries are not replayed, as `(catalog oid, object oid)`. skipped_entries: BTreeSet<(u32, u32)>, @@ -176,7 +177,7 @@ async fn read_plan( SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_namespace'::regclass AND d.objid = n.oid AND d.deptype = 'e'), - 'pg_namespace'::regclass::oid, n.oid + 'pg_namespace'::regclass::oid, n.oid, obj_description(n.oid, 'pg_namespace') FROM pg_extension e JOIN pg_namespace n ON n.oid = e.extnamespace WHERE e.extname <> 'plpgsql' ORDER BY e.oid", @@ -187,12 +188,16 @@ async fn read_plan( let mut plan = ExtensionPlan::default(); let mut extension_schemas = BTreeSet::new(); for row in rows { - if row.get::<_, bool>(2) { + let dumped_schema = row.get::<_, bool>(2); + if dumped_schema { extension_schemas.insert(row.get::<_, String>(1)); plan.skipped_entries.insert((row.get(3), row.get(4))); } - plan.extensions - .push(DatabaseExtension { name: row.get(0), schema: row.get(1) }); + plan.extensions.push(DatabaseExtension { + name: row.get(0), + schema: row.get(1), + schema_comment: if dumped_schema { row.get(5) } else { None }, + }); } plan.extension_schemas = extension_schemas.into_iter().collect(); @@ -543,11 +548,11 @@ impl ExtensionPlan { /// The `--use-list` file replaying every entry of `archive` but the skipped ones, when any is. async fn restore_list(&self, archive: &DumpFile) -> Result> { - if self.skipped_entries.is_empty() { + if self.skipped_entries.is_empty() && self.extension_schemas.is_empty() { return Ok(None); } let listing = pg_restore_list(archive).await?; - let kept = skip_entries(&listing, &self.skipped_entries); + let kept = skip_entries(&listing, &self.skipped_entries, &self.extension_schemas); write_lines(&[kept]) } @@ -649,6 +654,31 @@ impl ExtensionPlan { .await .map_err(target_err)?; } + // The replay leaves these to the copy, as a cluster administrator may have set them. + for extension in &self.extensions { + let Some(comment) = &extension.schema_comment else { + continue; + }; + let current: Option = client + .query_one( + "SELECT obj_description(oid, 'pg_namespace') FROM pg_namespace + WHERE nspname = $1", + &[&extension.schema], + ) + .await + .map_err(target_err)? + .get(0); + if current.as_ref() != Some(comment) { + client + .batch_execute(&format!( + "COMMENT ON SCHEMA {} IS '{}'", + quote_ident(&extension.schema), + comment.replace('\'', "''") + )) + .await + .map_err(target_err)?; + } + } // Creating an extension the database has would still need the rights to. let present: BTreeSet = client .query("SELECT extname::text FROM pg_extension", &[]) @@ -849,19 +879,46 @@ fn write_lines(lines: &[String]) -> Result> { Ok(Some(file)) } -/// `listing`, as `pg_restore --list` prints it, with the entries for `skipped` objects commented -/// out. An entry reads `; ...`. -fn skip_entries(listing: &str, skipped: &BTreeSet<(u32, u32)>) -> String { - let object = |line: &str| -> Option<(u32, u32)> { - let (_, entry) = line.split_once("; ")?; +/// `listing`, as `pg_restore --list` prints it, with the entries for `skipped` objects and the +/// comments on `uncommented_schemas` commented out. An entry reads `; +/// `. A comment's entry carries no oids, and names the +/// schema it is on as pg_dump writes it, quoted only when it has to be. +fn skip_entries( + listing: &str, + skipped: &BTreeSet<(u32, u32)>, + uncommented_schemas: &[String], +) -> String { + let skip = |line: &str| -> bool { + let Some((_, entry)) = line.split_once("; ") else { + return false; + }; let mut oids = entry.split_whitespace(); - Some((oids.next()?.parse().ok()?, oids.next()?.parse().ok()?)) + let object = ( + oids.next().and_then(|o| o.parse().ok()), + oids.next().and_then(|o| o.parse().ok()), + ); + if let (Some(catalog), Some(oid)) = object { + if skipped.contains(&(catalog, oid)) { + return true; + } + } + let Some(commented) = entry.strip_prefix("0 0 COMMENT - SCHEMA ") else { + return false; + }; + uncommented_schemas.iter().any(|schema| { + [schema.clone(), quote_ident(schema)] + .iter() + .any(|name| commented == name || commented.starts_with(&format!("{name} "))) + }) }; listing .lines() - .map(|line| match object(line) { - Some(object) if skipped.contains(&object) => format!(";{line}"), - _ => line.to_string(), + .map(|line| { + if skip(line) { + format!(";{line}") + } else { + line.to_string() + } }) .collect::>() .join("\n") @@ -918,8 +975,16 @@ mod tests { let listing = ";\n; Archive created at 2026-11-28\n\ 278; 1259 17838 TABLE public m postgres\n\ 3711; 2620 17842 TRIGGER public m ts_insert_blocker postgres\n\ - 3712; 2620 17843 TRIGGER public m audit postgres"; - let kept = skip_entries(listing, &[(2620, 17842)].into_iter().collect()); + 3712; 2620 17843 TRIGGER public m audit postgres\n\ + 6459; 0 0 COMMENT - SCHEMA \"My Ext\" postgres\n\ + 6461; 0 0 COMMENT - SCHEMA extensions postgres\n\ + 6462; 0 0 COMMENT - SCHEMA extensions_old postgres\n\ + 6463; 0 0 COMMENT - EXTENSION extensions "; + let kept = skip_entries( + listing, + &[(2620, 17842)].into_iter().collect(), + &["My Ext".to_string(), "extensions".to_string()], + ); assert_eq!( kept.lines().collect::>(), [ @@ -928,6 +993,10 @@ mod tests { "278; 1259 17838 TABLE public m postgres", ";3711; 2620 17842 TRIGGER public m ts_insert_blocker postgres", "3712; 2620 17843 TRIGGER public m audit postgres", + ";6459; 0 0 COMMENT - SCHEMA \"My Ext\" postgres", + ";6461; 0 0 COMMENT - SCHEMA extensions postgres", + "6462; 0 0 COMMENT - SCHEMA extensions_old postgres", + "6463; 0 0 COMMENT - EXTENSION extensions ", ] ); } diff --git a/backend/windmill-common/src/lib.rs b/backend/windmill-common/src/lib.rs index e82dc7c9a4..6821e4f18b 100644 --- a/backend/windmill-common/src/lib.rs +++ b/backend/windmill-common/src/lib.rs @@ -2173,6 +2173,8 @@ pub(crate) fn instance_db_grants(dbname: &str) -> String { pub struct DatabaseExtension { pub name: String, pub schema: String, + /// The schema's comment, which the copy's own replay may not be allowed to set. + pub schema_comment: Option, } /// Create `extensions` in database `dbname`, just created, on `client`, the cluster @@ -2268,6 +2270,32 @@ pub(crate) async fn provision_extensions( .batch_execute(&format!("BEGIN;\n{setup}COMMIT;")) .await .map_err(|e| err("Creating the extensions of", e))?; + // While the administrator still owns the schemas it made: the copy's role may own neither one + // the database already had nor one the handover below falls back from, and only an owner may + // comment on a schema. + for extension in extensions { + let Some(comment) = &extension.schema_comment else { + continue; + }; + let current: Option = client + .query_one( + "SELECT obj_description(oid, 'pg_namespace') FROM pg_namespace WHERE nspname = $1", + &[&extension.schema], + ) + .await + .map_err(|e| err("Checking", e))? + .get(0); + if current.as_ref() != Some(comment) { + client + .batch_execute(&format!( + "COMMENT ON SCHEMA {} IS '{}'", + datatable_roles::quote_ident(&extension.schema), + comment.replace('\'', "''") + )) + .await + .map_err(|e| err("Commenting the extension schemas of", e))?; + } + } for schema in schemas { let schema = datatable_roles::quote_ident(schema); let handed = client