From 57c2cac84e5ffbcf898a7e626ce11368b7efb778 Mon Sep 17 00:00:00 2001 From: Diego Imbert Date: Thu, 12 Mar 2026 13:22:31 +0100 Subject: [PATCH] Fix import_datatable_dump --- .../windmill-api-workspaces/src/workspaces.rs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/backend/windmill-api-workspaces/src/workspaces.rs b/backend/windmill-api-workspaces/src/workspaces.rs index c360315286..7118a0dfba 100644 --- a/backend/windmill-api-workspaces/src/workspaces.rs +++ b/backend/windmill-api-workspaces/src/workspaces.rs @@ -1325,11 +1325,7 @@ async fn get_datatable_schema(db: &DB, w_id: &str, datatable_name: &str) -> Resu /// Export the schema of a datatable using pg_dump. /// Returns a list of SQL statements that recreate the entire schema (no data). -pub async fn dump_datatable( - db: &DB, - w_id: &str, - datatable_name: &str, -) -> Result { +pub async fn dump_datatable(db: &DB, w_id: &str, datatable_name: &str) -> Result { let db_resource = get_datatable_resource_from_db_unchecked(db, w_id, datatable_name).await?; let pg_db: PgDatabase = serde_json::from_value(db_resource) @@ -1501,13 +1497,26 @@ async fn fork_datatable( )) } -/// Import schema statements into a target database using tokio_postgres. +/// Import a pg_dump output into a target database using tokio_postgres. +/// Filters out psql meta-commands (lines starting with `\`) and comment-only lines +/// that are not valid SQL but are included in pg_dump's plain-text output. async fn import_datatable_dump(target_db: &PgDatabase, dump: &str) -> Result<()> { + let filtered: String = dump + .lines() + .filter(|line| { + let trimmed = line.trim(); + !trimmed.starts_with('\\') + && !trimmed.starts_with("--") + && !trimmed.starts_with("SELECT pg_catalog.set_config") + }) + .collect::>() + .join("\n"); + let (client, connection) = target_db.connect().await?; let join_handle = tokio::spawn(async move { connection.await }); client - .batch_execute(dump) + .batch_execute(&filtered) .await .map_err(|e| Error::internal_err(format!("Failed to import schema: {}", e)))?;