Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(postgres): defer export index ddl until after functions

Jeff1c33dcf5aa40
diff --git a/crates/dbx-core/src/data/database_export.rs b/crates/dbx-core/src/data/database_export.rs
index c51627b136..84bc6e09f5 100644
--- a/crates/dbx-core/src/data/database_export.rs
+++ b/crates/dbx-core/src/data/database_export.rs
@@ -2144,12 +2144,20 @@ fn format_export_table_ddl(ddl: &str, database_type: Option<DatabaseType>, opts:
format!("{ddl};")
}
-fn split_postgres_export_table_triggers(ddl: &str, database_type: DatabaseType) -> (String, Vec<String>) {
+/// PostgreSQL database export: split the statements that may depend on
+/// schema-wide routines out of a relation's DDL, returning
+/// `(table, indexes, triggers)`.
+///
+/// Routines are written after every table, so an index whose expression or
+/// predicate calls a user-defined function (#12156), or a trigger that calls
+/// its trigger function, can only replay once the routines exist.
+fn split_postgres_export_table_ddl(ddl: &str, database_type: DatabaseType) -> (String, Vec<String>, Vec<String>) {
if database_type != DatabaseType::Postgres {
- return (ddl.to_string(), Vec::new());
+ return (ddl.to_string(), Vec::new(), Vec::new());
}
let mut table_statements = Vec::new();
+ let mut index_statements = Vec::new();
let mut trigger_statements = Vec::new();
for range in crate::db::ddl_scan::top_level_statement_ranges(ddl) {
let statement = ddl[range].trim();
@@ -2165,11 +2173,19 @@ fn split_postgres_export_table_triggers(ddl: &str, database_type: DatabaseType)
|| (second.as_deref() == Some("CONSTRAINT") && third.as_deref() == Some("TRIGGER")))
{
trigger_statements.push(statement.to_string());
+ } else if (first.as_deref() == Some("CREATE")
+ && (second.as_deref() == Some("INDEX")
+ || (second.as_deref() == Some("UNIQUE") && third.as_deref() == Some("INDEX"))))
+ || (first.as_deref() == Some("COMMENT")
+ && second.as_deref() == Some("ON")
+ && third.as_deref() == Some("INDEX"))
+ {
+ index_statements.push(statement.to_string());
} else {
table_statements.push(statement.to_string());
}
}
- (table_statements.join("\n"), trigger_statements)
+ (table_statements.join("\n"), index_statements, trigger_statements)
}
/// PostgreSQL database export: split inline `FOREIGN KEY` constraints out of a
@@ -4165,6 +4181,7 @@ async fn export_database_sql_core_inner(
let mut object_index: usize = 0;
let mut total_rows_exported = 0_u64;
+ let mut deferred_postgres_indexes = Vec::new();
let mut deferred_postgres_triggers = Vec::new();
// PostgreSQL foreign keys collected from table DDL while writing structure,
// emitted at the end of the file (see `extract_postgres_deferred_foreign_keys`).
@@ -4493,7 +4510,8 @@ async fn export_database_sql_core_inner(
}
match ddl_result {
Ok(ddl) => {
- let (ddl, triggers) = split_postgres_export_table_triggers(&ddl, db_type);
+ let (ddl, indexes, triggers) = split_postgres_export_table_ddl(&ddl, db_type);
+ deferred_postgres_indexes.extend(indexes);
deferred_postgres_triggers.extend(triggers);
// Defer inline foreign keys so restore never has to satisfy
// the dependency graph's order (cycles included).
@@ -5008,11 +5026,14 @@ async fn export_database_sql_core_inner(
}
}
- // PostgreSQL trigger definitions reference their trigger functions. The
- // table DDL builder returns both statements together, while schema-wide
- // routines are exported below the tables. Keep triggers part of table
- // structure, but write them only after routines so the resulting script
- // is executable in file order.
+ // PostgreSQL index expressions and trigger definitions can reference
+ // user-defined functions. The table DDL builder returns them together with
+ // the table, while schema-wide routines are exported below the tables. Keep
+ // them part of table structure, but write them only after routines so the
+ // resulting script is executable in file order.
+ for index in deferred_postgres_indexes {
+ writeln!(file, "{index}\n").map_err(|e| format!("Failed to write file: {e}"))?;
+ }
for trigger in deferred_postgres_triggers {
writeln!(file, "{trigger}\n").map_err(|e| format!("Failed to write file: {e}"))?;
}
@@ -5114,7 +5135,7 @@ mod tests {
generate_postgres_sequence_setval_sql, is_postgres_extension_member_routine, mysql_database_export_preamble,
mysql_view_dependencies_from_rows, mysql_view_dependencies_sql, normalize_export_table_ddl,
record_export_destination_identity, record_export_error, replace_database_export_select_list,
- sort_export_views_by_dependencies, split_postgres_export_table_triggers, write_database_export_rows,
+ sort_export_views_by_dependencies, split_postgres_export_table_ddl, write_database_export_rows,
write_database_export_rows_with_mode, BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions,
BuildExportSqlInsertOptions, DatabaseExportObjectCounts, DatabaseExportRequest, DatabaseExportWriter,
DdlNormalizeOptions, ExportedTableSql, PostgresExportEnum, PostgresExportExtension, PostgresExportSequence,
@@ -5608,11 +5629,12 @@ mod tests {
fn postgres_database_export_defers_table_triggers_without_splitting_function_bodies() {
let ddl = "CREATE TABLE \"public\".\"work_log\" (\n \"id\" bigint,\n \"note\" text DEFAULT ';'::text\n);\n\nCREATE INDEX \"idx_work_log\" ON \"public\".\"work_log\" (\"id\");\n\nCREATE TRIGGER trg_work_log BEFORE INSERT OR UPDATE ON public.work_log FOR EACH ROW EXECUTE FUNCTION fn_work_log_update();";
- let (table_ddl, triggers) = split_postgres_export_table_triggers(ddl, DatabaseType::Postgres);
+ let (table_ddl, indexes, triggers) = split_postgres_export_table_ddl(ddl, DatabaseType::Postgres);
assert!(table_ddl.contains("CREATE TABLE"));
assert!(table_ddl.contains("DEFAULT ';'::text"));
- assert!(table_ddl.contains("CREATE INDEX"));
+ assert!(!table_ddl.contains("CREATE INDEX"));
+ assert_eq!(indexes, vec!["CREATE INDEX \"idx_work_log\" ON \"public\".\"work_log\" (\"id\");"]);
assert!(!table_ddl.contains("CREATE TRIGGER"));
assert_eq!(triggers.len(), 1);
assert!(triggers[0].starts_with("CREATE TRIGGER trg_work_log"));
@@ -5622,15 +5644,36 @@ mod tests {
fn postgres_database_export_defers_constraint_triggers_only_for_postgres() {
let ddl = "CREATE TABLE \"public\".\"items\" (\"id\" bigint);\nCREATE CONSTRAINT TRIGGER items_check AFTER INSERT ON public.items DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION check_items();";
- let (postgres_table, postgres_triggers) = split_postgres_export_table_triggers(ddl, DatabaseType::Postgres);
+ let (postgres_table, _, postgres_triggers) = split_postgres_export_table_ddl(ddl, DatabaseType::Postgres);
assert!(!postgres_table.contains("CREATE CONSTRAINT TRIGGER"));
assert_eq!(postgres_triggers.len(), 1);
- let (mysql_ddl, mysql_triggers) = split_postgres_export_table_triggers(ddl, DatabaseType::Mysql);
+ let (mysql_ddl, mysql_indexes, mysql_triggers) = split_postgres_export_table_ddl(ddl, DatabaseType::Mysql);
assert_eq!(mysql_ddl, ddl);
+ assert!(mysql_indexes.is_empty());
assert!(mysql_triggers.is_empty());
}
+ #[test]
+ fn postgres_database_export_defers_indexes_and_their_comments() {
+ let ddl = "CREATE TABLE \"public\".\"tagged_items\" (\n \"id\" integer NOT NULL,\n \"labels\" text[] NOT NULL\n);\n\nCOMMENT ON COLUMN \"public\".\"tagged_items\".\"labels\" IS 'index labels';\nCREATE UNIQUE INDEX \"tagged_items_labels_idx\" ON \"public\".\"tagged_items\" USING btree (immutable_array_to_string(labels) ASC NULLS LAST);\nCOMMENT ON INDEX \"public\".\"tagged_items_labels_idx\" IS 'CREATE INDEX; inside a comment';";
+
+ let (table_ddl, indexes, triggers) = split_postgres_export_table_ddl(ddl, DatabaseType::Postgres);
+
+ assert_eq!(
+ table_ddl,
+ "CREATE TABLE \"public\".\"tagged_items\" (\n \"id\" integer NOT NULL,\n \"labels\" text[] NOT NULL\n);\nCOMMENT ON COLUMN \"public\".\"tagged_items\".\"labels\" IS 'index labels';"
+ );
+ assert_eq!(
+ indexes,
+ vec![
+ "CREATE UNIQUE INDEX \"tagged_items_labels_idx\" ON \"public\".\"tagged_items\" USING btree (immutable_array_to_string(labels) ASC NULLS LAST);",
+ "COMMENT ON INDEX \"public\".\"tagged_items_labels_idx\" IS 'CREATE INDEX; inside a comment';",
+ ]
+ );
+ assert!(triggers.is_empty());
+ }
+
#[test]
fn postgres_export_defers_inline_foreign_keys_to_file_tail_statements() {
let ddl = "CREATE TABLE \"public\".\"child_versions\" (\n \"id\" uuid NOT NULL,\n \"parent_id\" uuid NOT NULL,\n CONSTRAINT \"child_versions_pkey\" PRIMARY KEY (id),\n CONSTRAINT \"fk_child_parent\" FOREIGN KEY (\"parent_id\") REFERENCES \"public\".\"parent_entities\"(\"id\")\n);";
diff --git a/crates/dbx-core/tests/live_postgres_export_function_order.rs b/crates/dbx-core/tests/live_postgres_export_function_order.rs
new file mode 100644
index 0000000000..8a49fed597
--- /dev/null
+++ b/crates/dbx-core/tests/live_postgres_export_function_order.rs
@@ -0,0 +1,238 @@
+//! Live regression coverage for issue #12156: a PostgreSQL database export
+//! must restore into an empty database when an index calls a user-defined
+//! `IMMUTABLE` function, while functions that depend on a table (a row-type
+//! return or a SQL body that reads the table) still replay after it.
+//!
+//! Run against a disposable/local endpoint:
+//! DBX_LIVE_POSTGRES_EXPORT_HOST=127.0.0.1 \
+//! DBX_LIVE_POSTGRES_EXPORT_PORT=5432 \
+//! DBX_LIVE_POSTGRES_EXPORT_USER=... \
+//! DBX_LIVE_POSTGRES_EXPORT_PASSWORD=... \
+//! cargo test -p dbx-core --no-default-features --features sqlite-bundled,duckdb-sidecar \
+//! --test live_postgres_export_function_order -- --include-ignored
+
+use dbx_core::connection::AppState;
+use dbx_core::database_export::{export_database_sql_core, DatabaseExportRequest};
+use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
+use dbx_core::query::execute_sql_statement;
+use dbx_core::sql::SqlFileRequest;
+use std::sync::Arc;
+
+fn live_postgres_config(id: &str, database: &str) -> ConnectionConfig {
+ let host = std::env::var("DBX_LIVE_POSTGRES_EXPORT_HOST").expect("DBX_LIVE_POSTGRES_EXPORT_HOST");
+ let port =
+ std::env::var("DBX_LIVE_POSTGRES_EXPORT_PORT").ok().and_then(|value| value.parse::<u16>().ok()).unwrap_or(5432);
+ let username = std::env::var("DBX_LIVE_POSTGRES_EXPORT_USER").expect("DBX_LIVE_POSTGRES_EXPORT_USER");
+ let password = std::env::var("DBX_LIVE_POSTGRES_EXPORT_PASSWORD").unwrap_or_default();
+
+ serde_json::from_value(serde_json::json!({
+ "id": id,
+ "name": id,
+ "db_type": DatabaseType::Postgres,
+ "host": host,
+ "port": port,
+ "username": username,
+ "password": password,
+ "database": database,
+ "ssl": false,
+ "connect_timeout_secs": 10,
+ "query_timeout_secs": 30,
+ "idle_timeout_secs": 60,
+ "keepalive_interval_secs": 0
+ }))
+ .expect("live PostgreSQL export config should deserialize")
+}
+
+#[tokio::test]
+#[ignore = "requires a live PostgreSQL endpoint"]
+async fn live_postgres_export_restores_tables_that_depend_on_functions() {
+ let suffix = uuid::Uuid::new_v4().simple().to_string();
+ let source_database = format!("dbx_eval_fn_src_{}", &suffix[..10]);
+ let target_database = format!("dbx_eval_fn_dst_{}", &suffix[..10]);
+ let admin_connection_id = format!("pg-fn-export-admin-{suffix}");
+ let source_connection_id = format!("pg-fn-export-source-{suffix}");
+ let target_connection_id = format!("pg-fn-export-target-{suffix}");
+ let dir = std::env::temp_dir().join(format!("dbx-fn-export-{suffix}"));
+ std::fs::create_dir_all(&dir).unwrap();
+ let storage = dbx_core::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
+ let state = Arc::new(AppState::new(storage));
+ state
+ .configs
+ .write()
+ .await
+ .insert(admin_connection_id.clone(), live_postgres_config(&admin_connection_id, "postgres"));
+
+ for database in [&source_database, &target_database] {
+ execute_sql_statement(
+ &state,
+ &admin_connection_id,
+ "postgres",
+ &format!("CREATE DATABASE \"{database}\""),
+ None,
+ None,
+ )
+ .await
+ .expect("create disposable PostgreSQL database");
+ }
+ state
+ .configs
+ .write()
+ .await
+ .insert(source_connection_id.clone(), live_postgres_config(&source_connection_id, &source_database));
+ state
+ .configs
+ .write()
+ .await
+ .insert(target_connection_id.clone(), live_postgres_config(&target_connection_id, &target_database));
+
+ // The expression and partial indexes are the reported case.
+ // `items_with_label` goes the other way: it returns the table's row type,
+ // so it can only be created after the table, and `count_items` is a SQL
+ // function whose body reads the table.
+ for statement in [
+ "CREATE FUNCTION public.immutable_array_to_string(text[]) RETURNS text \
+ LANGUAGE sql IMMUTABLE AS $$ SELECT array_to_string($1, ',') $$",
+ "CREATE TABLE public.tagged_items (id integer PRIMARY KEY, labels text[] NOT NULL)",
+ "CREATE INDEX tagged_items_labels_idx ON public.tagged_items (public.immutable_array_to_string(labels))",
+ "COMMENT ON INDEX public.tagged_items_labels_idx IS 'joined labels'",
+ "CREATE UNIQUE INDEX tagged_items_single_label_idx ON public.tagged_items (id) \
+ WHERE public.immutable_array_to_string(labels) NOT LIKE '%,%'",
+ "CREATE FUNCTION public.items_with_label(text) RETURNS SETOF public.tagged_items \
+ LANGUAGE sql STABLE AS $$ SELECT * FROM public.tagged_items WHERE $1 = ANY (labels) $$",
+ "CREATE FUNCTION public.count_items() RETURNS bigint \
+ LANGUAGE sql STABLE AS $$ SELECT count(*) FROM public.tagged_items $$",
+ "INSERT INTO public.tagged_items (id, labels) VALUES (1, ARRAY['a', 'b']), (2, ARRAY['c'])",
+ ] {
+ execute_sql_statement(&state, &source_connection_id, &source_database, statement, None, None)
+ .await
+ .expect("create source fixture");
+ }
+
+ let export_path = dir.join("backup.sql");
+ export_database_sql_core(
+ &state,
+ &DatabaseExportRequest {
+ export_id: format!("pg-fn-export-{suffix}"),
+ connection_id: source_connection_id.clone(),
+ database: source_database.clone(),
+ schema: "public".to_string(),
+ file_path: export_path.display().to_string(),
+ selected_tables: Vec::new(),
+ excluded_tables: Vec::new(),
+ include_structure: true,
+ include_data: true,
+ include_objects: true,
+ include_create_database: false,
+ drop_table_if_exists: false,
+ omit_auto_increment: false,
+ preserve_original_language: false,
+ fail_on_error: true,
+ prevent_overwrite: false,
+ output_compression: Default::default(),
+ insert_dialect: Default::default(),
+ insert_mode: Default::default(),
+ snapshot_session_id: None,
+ batch_size: 1000,
+ split_max_mb: None,
+ },
+ |_| {},
+ )
+ .await
+ .expect("export PostgreSQL database with function-dependent tables");
+
+ let exported = std::fs::read_to_string(&export_path).expect("read exported SQL");
+ // Index statements must follow the functions they call.
+ let function_position = exported.find("FUNCTION public.immutable_array_to_string");
+ let index_position = exported.find("CREATE INDEX \"tagged_items_labels_idx\"");
+
+ let restore = dbx_core::sql_file_import::execute_sql_file_path(
+ &state,
+ &SqlFileRequest {
+ txn_session_id: None,
+ execution_id: format!("pg-fn-export-restore-{suffix}"),
+ connection_id: target_connection_id.clone(),
+ database: target_database.clone(),
+ schema: Some("public".to_string()),
+ file_path: export_path.display().to_string(),
+ continue_on_error: false,
+ selected_tables: None,
+ part_cooldown_ms: 0,
+ skip_relational_constraints: false,
+ failure_log_enabled: true,
+ },
+ &export_path,
+ tokio_util::sync::CancellationToken::new(),
+ std::time::Instant::now(),
+ |_| {},
+ )
+ .await;
+
+ let mut catalog = Vec::new();
+ for sql in [
+ "SELECT indexdef, obj_description(format('public.%I', indexname)::regclass, 'pg_class') \
+ FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'tagged_items' ORDER BY indexname",
+ "SELECT p.proname FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace \
+ WHERE n.nspname = 'public' ORDER BY p.proname",
+ "SELECT id, public.immutable_array_to_string(labels) FROM public.tagged_items ORDER BY id",
+ "SELECT public.count_items()",
+ "SELECT count(*) FROM public.items_with_label('b')",
+ ] {
+ catalog.push(
+ execute_sql_statement(&state, &target_connection_id, &target_database, sql, None, None)
+ .await
+ .map(|result| result.rows),
+ );
+ }
+
+ for database in [&source_database, &target_database] {
+ let _ = execute_sql_statement(
+ &state,
+ &admin_connection_id,
+ "postgres",
+ &format!("DROP DATABASE \"{database}\" WITH (FORCE)"),
+ None,
+ None,
+ )
+ .await;
+ }
+ drop(state);
+ let _ = std::fs::remove_dir_all(&dir);
+
+ assert!(restore.is_ok(), "restoring the exported backup failed: {:?}\n\n{exported}", restore.err());
+ let function_position = function_position.expect("export contains the index function");
+ let index_position = index_position.expect("export contains the index");
+ assert!(function_position < index_position, "index exported before its function:\n{exported}");
+ let catalog = catalog.into_iter().collect::<Result<Vec<_>, _>>().expect("query restored catalog");
+ assert_eq!(
+ catalog[0],
+ vec![
+ vec![
+ serde_json::json!(
+ "CREATE INDEX tagged_items_labels_idx ON public.tagged_items USING btree (immutable_array_to_string(labels))"
+ ),
+ serde_json::json!("joined labels"),
+ ],
+ vec![serde_json::json!("CREATE UNIQUE INDEX tagged_items_pkey ON public.tagged_items USING btree (id)"), serde_json::Value::Null],
+ vec![
+ serde_json::json!(
+ "CREATE UNIQUE INDEX tagged_items_single_label_idx ON public.tagged_items USING btree (id) WHERE (immutable_array_to_string(labels) !~~ '%,%'::text)"
+ ),
+ serde_json::Value::Null,
+ ],
+ ]
+ );
+ assert_eq!(
+ catalog[1],
+ vec![
+ vec![serde_json::json!("count_items")],
+ vec![serde_json::json!("immutable_array_to_string")],
+ vec![serde_json::json!("items_with_label")],
+ ]
+ );
+ assert_eq!(
+ catalog[2],
+ vec![vec![serde_json::json!(1), serde_json::json!("a,b")], vec![serde_json::json!(2), serde_json::json!("c")]]
+ );
+ assert_eq!(catalog[3], vec![vec![serde_json::json!(2)]]);
+ assert_eq!(catalog[4], vec![vec![serde_json::json!(1)]]);
+}