From ce80cfaa2eebd6bca790c6c7d7d937310710eb6f Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sat, 28 Nov 2026 19:15:12 +0100 Subject: [PATCH] fix: have the server quote copied schema comments, keep a copy whose inherited schema it cannot comment Co-Authored-By: Claude Opus 5.5 --- .../src/datatable_extensions.rs | 42 +++++++++++-------- backend/windmill-common/src/lib.rs | 38 ++++++++++------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/backend/windmill-api-workspaces/src/datatable_extensions.rs b/backend/windmill-api-workspaces/src/datatable_extensions.rs index 89e6d5a821..953e214172 100644 --- a/backend/windmill-api-workspaces/src/datatable_extensions.rs +++ b/backend/windmill-api-workspaces/src/datatable_extensions.rs @@ -592,8 +592,8 @@ impl ExtensionPlan { } /// TimescaleDB refuses `ALTER TABLE ONLY` on a hypertable, which the post-data section, - /// replayed once they are hypertables, adds constraints with. Without `ONLY` the constraint goes - /// on the hypertable, and TimescaleDB gives each chunk its own. + /// replayed once they are hypertables, adds constraints with. Without `ONLY` the constraint + /// goes on the hypertable, and TimescaleDB gives each chunk its own. async fn drop_only_on_hypertables(&self, post_data: &DumpFile) -> Result<()> { let io_err = |e: std::io::Error| Error::internal_err(format!("Failed to patch dump file: {e}")); @@ -654,29 +654,35 @@ impl ExtensionPlan { .await .map_err(target_err)?; } - // The replay leaves these to the copy, as a cluster administrator may have set them. + // The replay leaves these to the copy, as a cluster administrator may have set them. The + // server quotes the comment, so no `standard_conforming_strings` lets it end the literal. 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], + let statement: Option = client + .query_opt( + "SELECT format('COMMENT ON SCHEMA %I IS %L', $1::text, $2::text) + FROM pg_namespace + WHERE nspname = $1::text + AND obj_description(oid, 'pg_namespace') IS DISTINCT FROM $2::text", + &[&extension.schema, comment], ) .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)?; + .map(|row| row.get(0)); + let Some(statement) = statement else { + continue; + }; + // A schema the database already had can be another role's: the copy goes without the + // comment rather than not at all. + if let Err(e) = client.batch_execute(&statement).await { + tracing::warn!( + "Not copying the comment on schema '{}' to '{}': {}", + extension.schema, + target.dbname, + pg_error_message(&e) + ); } } // Creating an extension the database has would still need the rights to. diff --git a/backend/windmill-common/src/lib.rs b/backend/windmill-common/src/lib.rs index 6821e4f18b..5b4c6df3d0 100644 --- a/backend/windmill-common/src/lib.rs +++ b/backend/windmill-common/src/lib.rs @@ -2272,28 +2272,36 @@ pub(crate) async fn provision_extensions( .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. + // comment on a schema. The server quotes the comment, the source's text, so no setting of + // `standard_conforming_strings` lets it end the literal. 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], + let statement: Option = client + .query_opt( + "SELECT format('COMMENT ON SCHEMA %I IS %L', $1::text, $2::text) + FROM pg_namespace + WHERE nspname = $1::text + AND obj_description(oid, 'pg_namespace') IS DISTINCT FROM $2::text", + &[&extension.schema, comment], ) .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))?; + .map(|row| row.get(0)); + let Some(statement) = statement else { + continue; + }; + match client.batch_execute(&statement).await { + Ok(()) => {} + // A schema the database already had can be another superuser's: the copy goes without + // the comment rather than not at all. + Err(e) if !schemas.contains(extension.schema.as_str()) => tracing::warn!( + "Not copying the comment on schema '{}' to '{dbname}': {}", + extension.schema, + crate::error::pg_error_message(&e) + ), + Err(e) => return Err(err("Commenting the extension schemas of", e)), } } for schema in schemas {