fix: have the server quote copied schema comments, keep a copy whose inherited schema it cannot comment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-28 19:15:28 +01:00
co-authored by Claude Opus 5.5
parent 4442733cab
commit ce80cfaa2e
2 changed files with 47 additions and 33 deletions
@@ -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<String> = client
.query_one(
"SELECT obj_description(oid, 'pg_namespace') FROM pg_namespace
WHERE nspname = $1",
&[&extension.schema],
let statement: Option<String> = 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.
+23 -15
View File
@@ -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<String> = client
.query_one(
"SELECT obj_description(oid, 'pg_namespace') FROM pg_namespace WHERE nspname = $1",
&[&extension.schema],
let statement: Option<String> = 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 {