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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(postgres): defer backup export foreign keys to file end
YingNingc9e8ee53f705
diff --git a/crates/dbx-core/src/data/database_export.rs b/crates/dbx-core/src/data/database_export.rsindex 8b1b69e6c8..89984402ae 100644--- a/crates/dbx-core/src/data/database_export.rs+++ b/crates/dbx-core/src/data/database_export.rs@@ -1859,6 +1859,44 @@ fn split_postgres_export_table_triggers(ddl: &str, database_type: DatabaseType)(table_statements.join("\n"), trigger_statements)}+/// PostgreSQL database export: split inline `FOREIGN KEY` constraints out of a+/// relation's `CREATE TABLE` and return them as deferred+/// `ALTER TABLE ... ADD CONSTRAINT ...` statements.+///+/// Inline foreign keys tie the script's replay to table creation order. The+/// exporter sorts tables by dependency, but a foreign key cycle can never+/// satisfy that order, so a backup that inlines its foreign keys cannot be+/// restored (`ERROR: relation ... does not exist` — issue #10575). Emitting+/// the constraints after every table and row exists (pg_dump's post-data+/// position) removes the ordering requirement entirely.+fn extract_postgres_deferred_foreign_keys(ddl: &str, schema: &str, table: &str) -> (String, Vec<String>) {+ let fallback_qualified = crate::transfer::qualified_table(table, schema, &DatabaseType::Postgres, None);+ let mut statements = Vec::new();+ let mut deferred = Vec::new();+ for range in crate::db::ddl_scan::top_level_statement_ranges(ddl) {+ let statement = ddl[range].trim();+ if statement.is_empty() {+ continue;+ }+ let (stripped, removed) = crate::transfer::strip_inline_foreign_key_constraint_lines_collecting(statement);+ if !removed.is_empty() {+ // Qualify the ALTER from the statement's own relation name: whole-+ // database exports leave `schema` empty and rely on each statement+ // to carry its schema, so a mismatch here would fail the replay.+ let qualified = crate::db::ddl_scan::parse_create_table_relation(statement)+ .map(|(statement_schema, statement_table)| {+ crate::transfer::qualified_table(&statement_table, &statement_schema, &DatabaseType::Postgres, None)+ })+ .unwrap_or_else(|| fallback_qualified.clone());+ for clause in removed {+ deferred.push(format!("ALTER TABLE {qualified} ADD {clause};"));+ }+ }+ statements.push(stripped);+ }+ (statements.join("\n"), deferred)+}+fn postgres_sequence_qualified_name(schema: &str, sequence_name: &str) -> String {let db_type = DatabaseType::Postgres;if schema.trim().is_empty() {@@ -3718,6 +3756,9 @@ async fn export_database_sql_core_inner(let mut object_index: usize = 0;let mut total_rows_exported = 0_u64;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`).+ let mut deferred_postgres_foreign_keys: Vec<String> = Vec::new();// total_objects is known later for the write phase; preparing updates stay// presence-only so the UI does not show a counter that later resets.emit_database_export_running(&on_progress, &request.export_id, "", 0, 0, 0, true);@@ -3994,6 +4035,16 @@ async fn export_database_sql_core_inner(Ok(ddl) => {let (ddl, triggers) = split_postgres_export_table_triggers(&ddl, db_type);deferred_postgres_triggers.extend(triggers);+ // Defer inline foreign keys so restore never has to satisfy+ // the dependency graph's order (cycles included).+ let ddl = if db_type == DatabaseType::Postgres {+ let (stripped, deferred) =+ extract_postgres_deferred_foreign_keys(&ddl, &request.schema, table_name);+ deferred_postgres_foreign_keys.extend(deferred);+ stripped+ } else {+ ddl+ };let ddl = format_export_table_ddl(&ddl,Some(db_type),@@ -4502,6 +4553,13 @@ async fn export_database_sql_core_inner(writeln!(file, "{trigger}\n").map_err(|e| format!("Failed to write file: {e}"))?;}+ // PostgreSQL foreign keys come after every table and row exists, so the+ // script replays into an empty database no matter what order (or cycle)+ // the schema's foreign key graph has (issue #10575).+ for statement in deferred_postgres_foreign_keys {+ writeln!(file, "{statement}\n").map_err(|e| format!("Failed to write file: {e}"))?;+ }+// For MySQL: re-enable foreign key checksif matches!(db_type, DatabaseType::Mysql) {writeln!(file, "SET FOREIGN_KEY_CHECKS = 1;").map_err(|e| format!("Failed to write file: {e}"))?;@@ -4584,19 +4642,19 @@ mod tests {build_export_insert_statements_excluding, build_export_object_source_sql, build_export_sql_insert,create_database_export_writer, database_export_query_options_for_timeout, database_export_select_sql,database_export_total_objects, drop_table_if_exists_sql, ensure_export_destination_dir,- export_destination_identity_mismatch, filter_export_table_infos, format_export_sql_literal,- format_export_table_ddl, format_mysql_spatial_export_literal, format_xugu_spatial_export_literal,- generate_postgres_enum_ddl, generate_postgres_extension_ddl, generate_postgres_sequence_create_ddl,- generate_postgres_sequence_owner_ddl, 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, BuildDatabaseSqlExportOptions,- BuildExportInsertStatementsOptions, BuildExportSqlInsertOptions, DatabaseExportObjectCounts,- DatabaseExportRequest, DatabaseExportWriter, DdlNormalizeOptions, ExportedTableSql, PostgresExportEnum,- PostgresExportExtension, PostgresExportSequence, PostgresExtensionMembers, SqlInsertDialect,- DATABASE_EXPORT_INSERT_BATCH_SIZE, DATABASE_EXPORT_ROW_LIMIT, POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL,- POSTGRES_EXPORT_SEQUENCES_SQL,+ export_destination_identity_mismatch, extract_postgres_deferred_foreign_keys, filter_export_table_infos,+ format_export_sql_literal, format_export_table_ddl, format_mysql_spatial_export_literal,+ format_xugu_spatial_export_literal, generate_postgres_enum_ddl, generate_postgres_extension_ddl,+ generate_postgres_sequence_create_ddl, generate_postgres_sequence_owner_ddl,+ 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,+ BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions, BuildExportSqlInsertOptions,+ DatabaseExportObjectCounts, DatabaseExportRequest, DatabaseExportWriter, DdlNormalizeOptions, ExportedTableSql,+ PostgresExportEnum, PostgresExportExtension, PostgresExportSequence, PostgresExtensionMembers,+ SqlInsertDialect, DATABASE_EXPORT_INSERT_BATCH_SIZE, DATABASE_EXPORT_ROW_LIMIT,+ POSTGRES_EXPORT_SEQUENCES_COMPAT_SQL, POSTGRES_EXPORT_SEQUENCES_SQL,};use super::{ExportProgress, LenientExportErrors};use crate::connection::AppState;@@ -5080,6 +5138,85 @@ mod tests {assert!(mysql_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);";++ let (stripped, deferred) = extract_postgres_deferred_foreign_keys(ddl, "public", "child_versions");++ assert!(!stripped.contains("FOREIGN KEY"), "stripped: {stripped}");+ // Removing the trailing foreign key must also drop the comma it left behind.+ assert!(stripped.contains("CONSTRAINT \"child_versions_pkey\" PRIMARY KEY (id)\n)"), "stripped: {stripped}");+ assert_eq!(+ deferred,+ vec![+ "ALTER TABLE \"public\".\"child_versions\" ADD CONSTRAINT \"fk_child_parent\" FOREIGN KEY (\"parent_id\") REFERENCES \"public\".\"parent_entities\"(\"id\");".to_string()+ ]+ );+ }++ #[test]+ fn postgres_export_defers_foreign_keys_and_keeps_other_constraints() {+ let ddl = "CREATE TABLE \"public\".\"assignments\" (\n \"id\" integer NOT NULL,\n \"owner_id\" integer NOT NULL,\n \"reviewer_id\" integer NOT NULL,\n CONSTRAINT \"assignments_owner_fk\" FOREIGN KEY (\"owner_id\") REFERENCES \"users\"(\"id\"),\n CONSTRAINT \"assignments_owner_check\" CHECK (owner_id > 0),\n CONSTRAINT \"assignments_reviewer_fk\" FOREIGN KEY (\"reviewer_id\") REFERENCES \"users\"(\"id\"),\n CONSTRAINT \"assignments_pair_unique\" UNIQUE (\"owner_id\", \"reviewer_id\")\n);";++ let (stripped, deferred) = extract_postgres_deferred_foreign_keys(ddl, "public", "assignments");++ assert!(!stripped.contains("FOREIGN KEY"), "stripped: {stripped}");+ assert!(+ stripped.contains("CONSTRAINT \"assignments_owner_check\" CHECK (owner_id > 0),"),+ "stripped: {stripped}"+ );+ assert!(+ stripped.contains("CONSTRAINT \"assignments_pair_unique\" UNIQUE (\"owner_id\", \"reviewer_id\")"),+ "stripped: {stripped}"+ );+ assert_eq!(deferred.len(), 2);+ assert!(deferred[0]+ .starts_with("ALTER TABLE \"public\".\"assignments\" ADD CONSTRAINT \"assignments_owner_fk\" FOREIGN KEY"));+ assert!(deferred[1].starts_with(+ "ALTER TABLE \"public\".\"assignments\" ADD CONSTRAINT \"assignments_reviewer_fk\" FOREIGN KEY"+ ));+ }++ #[test]+ fn postgres_export_defers_foreign_keys_across_multiple_statements() {+ let no_foreign_keys = "CREATE TABLE \"public\".\"parent_entities\" (\n \"id\" integer NOT NULL,\n CONSTRAINT \"parent_entities_pkey\" PRIMARY KEY (id)\n);\nALTER TABLE ONLY \"public\".\"events_2027_h1\" ALTER COLUMN \"status\" DROP DEFAULT;";+ let (stripped, deferred) = extract_postgres_deferred_foreign_keys(no_foreign_keys, "public", "parent_entities");+ assert_eq!(stripped, no_foreign_keys);+ assert!(deferred.is_empty());++ let with_cycle = "CREATE TABLE \"public\".\"cyc_x\" (\n \"id\" integer NOT NULL,\n CONSTRAINT \"cyc_x_pkey\" PRIMARY KEY (id),\n CONSTRAINT \"cyc_x_y_fk\" FOREIGN KEY (\"id\") REFERENCES \"public\".\"cyc_y\"(\"id\")\n);\nALTER TABLE ONLY \"public\".\"cyc_x\" ALTER COLUMN \"id\" DROP DEFAULT;";+ let (stripped, deferred) = extract_postgres_deferred_foreign_keys(with_cycle, "public", "cyc_x");+ assert!(!stripped.contains("cyc_x_y_fk"), "stripped: {stripped}");+ assert!(+ stripped.contains("ALTER TABLE ONLY \"public\".\"cyc_x\" ALTER COLUMN \"id\" DROP DEFAULT;"),+ "stripped: {stripped}"+ );+ assert_eq!(+ deferred,+ vec![+ "ALTER TABLE \"public\".\"cyc_x\" ADD CONSTRAINT \"cyc_x_y_fk\" FOREIGN KEY (\"id\") REFERENCES \"public\".\"cyc_y\"(\"id\");".to_string()+ ]+ );+ }++ #[test]+ fn postgres_export_deferred_foreign_keys_use_the_statement_schema() {+ // Whole-database exports leave the request schema empty; the ALTER must+ // still target the schema the CREATE TABLE statement itself carries.+ let ddl = "CREATE TABLE \"inventory\".\"orders\" (\n \"id\" integer NOT NULL,\n \"product_id\" integer NOT NULL,\n CONSTRAINT \"orders_product_fk\" FOREIGN KEY (\"product_id\") REFERENCES \"inventory\".\"products\"(\"id\")\n);";++ let (stripped, deferred) = extract_postgres_deferred_foreign_keys(ddl, "", "orders");++ assert!(!stripped.contains("FOREIGN KEY"), "stripped: {stripped}");+ assert_eq!(+ deferred,+ vec![+ "ALTER TABLE \"inventory\".\"orders\" ADD CONSTRAINT \"orders_product_fk\" FOREIGN KEY (\"product_id\") REFERENCES \"inventory\".\"products\"(\"id\");".to_string()+ ]+ );+ }+#[test]fn concurrent_prefetch_only_allowed_for_multi_connection_pools() {use crate::connection::PoolKind;diff --git a/crates/dbx-core/src/data/transfer.rs b/crates/dbx-core/src/data/transfer.rsindex 8aae6f38df..70ae0783e4 100644--- a/crates/dbx-core/src/data/transfer.rs+++ b/crates/dbx-core/src/data/transfer.rs@@ -6529,21 +6529,30 @@ fn transfer_ddl_statements(sql: &str, db_type: &DatabaseType) -> Vec<String> {/// KEY (` / bare `FOREIGN KEY (`); a column line whose COMMENT or DEFAULT text/// merely mentions the words must survive (#7660).fn strip_inline_foreign_key_constraint_lines(statement: &str) -> String {+ strip_inline_foreign_key_constraint_lines_collecting(statement).0+}++/// Same as [`strip_inline_foreign_key_constraint_lines`], but also returns every+/// removed `CONSTRAINT ... FOREIGN KEY ...` clause (whitespace-trimmed, trailing+/// comma dropped) so callers can re-create each constraint later — e.g. database+/// export defers PostgreSQL foreign keys to the end of the script, where restore+/// order no longer has to satisfy foreign key dependencies (issue #10575).+pub(crate) fn strip_inline_foreign_key_constraint_lines_collecting(statement: &str) -> (String, Vec<String>) {if !statement.trim_start().to_ascii_uppercase().starts_with("CREATE TABLE ") {- return statement.to_string();+ return (statement.to_string(), Vec::new());}let mut lines: Vec<String> = Vec::new();- let mut removed_foreign_key = false;+ let mut removed_foreign_keys: Vec<String> = Vec::new();for line in statement.lines() {if INLINE_FOREIGN_KEY_CONSTRAINT_LINE_RE.is_match(line) {- removed_foreign_key = true;+ removed_foreign_keys.push(line.trim().trim_end_matches(',').trim_end().to_string());continue;}lines.push(line.to_string());}- if removed_foreign_key {+ if !removed_foreign_keys.is_empty() {if let Some(closing_index) = lines.iter().rposition(|line| line.trim_start().starts_with(')')) {if let Some(previous) = lines[..closing_index].iter_mut().rfind(|line| !line.trim().is_empty()) {let trimmed_len = previous.trim_end_matches(char::is_whitespace).len();@@ -6554,7 +6563,7 @@ fn strip_inline_foreign_key_constraint_lines(statement: &str) -> String {}}- lines.join("\n")+ (lines.join("\n"), removed_foreign_keys)}fn is_postgres_post_table_index_statement(statement: &str) -> bool {diff --git a/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs b/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rsnew file mode 100644index 0000000000..dbe5dd62c6--- /dev/null+++ b/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs@@ -0,0 +1,236 @@+//! Live regression coverage for issue #10575: a PostgreSQL database export+//! (`.sql`/`.sql.gz` backup) must restore into an empty database even when the+//! schema's foreign key graph contains a cycle — a topology that no table+//! creation order can satisfy.+//!+//! The export writes foreign keys as deferred `ALTER TABLE ... ADD CONSTRAINT`+//! statements after every table and row (pg_dump's post-data position), so the+//! script replays in file order regardless of dependency direction.+//!+//! 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_deferred_foreign_keys -- --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_with_foreign_key_cycle_restores_into_empty_database() {+ let suffix = uuid::Uuid::new_v4().simple().to_string();+ let source_database = format!("dbx_fk_export_src_{}", &suffix[..10]);+ let target_database = format!("dbx_fk_export_dst_{}", &suffix[..10]);+ let admin_connection_id = format!("pg-fk-export-admin-{suffix}");+ let source_connection_id = format!("pg-fk-export-source-{suffix}");+ let target_connection_id = format!("pg-fk-export-target-{suffix}");+ let dir = std::env::temp_dir().join(format!("dbx-fk-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));++ // A parent/child pair, a three-level dependency chain, and a mutual+ // foreign key cycle — the cycle is what no creation order can satisfy.+ // The cycle columns stay nullable so the source rows can be inserted one+ // at a time; the update closes the cycle afterwards.+ for statement in [+ "CREATE TABLE public.parent_entities (id integer PRIMARY KEY)",+ "CREATE TABLE public.child_versions (id integer PRIMARY KEY, parent_id integer NOT NULL, \+ CONSTRAINT fk_child_parent FOREIGN KEY (parent_id) REFERENCES public.parent_entities(id))",+ "CREATE TABLE public.base_c (id integer PRIMARY KEY)",+ "CREATE TABLE public.parent_a (id integer PRIMARY KEY, base_id integer NOT NULL REFERENCES public.base_c(id))",+ "CREATE TABLE public.child_a (id integer PRIMARY KEY, parent_id integer NOT NULL REFERENCES public.parent_a(id))",+ "CREATE TABLE public.cyc_x (id integer PRIMARY KEY, peer_id integer)",+ "CREATE TABLE public.cyc_y (id integer PRIMARY KEY, peer_id integer)",+ "ALTER TABLE public.cyc_x ADD CONSTRAINT cyc_x_y_fk FOREIGN KEY (peer_id) REFERENCES public.cyc_y(id)",+ "ALTER TABLE public.cyc_y ADD CONSTRAINT cyc_y_x_fk FOREIGN KEY (peer_id) REFERENCES public.cyc_x(id)",+ "INSERT INTO public.parent_entities VALUES (1), (2)",+ "INSERT INTO public.child_versions VALUES (10, 1)",+ "INSERT INTO public.base_c VALUES (100)",+ "INSERT INTO public.parent_a VALUES (200, 100)",+ "INSERT INTO public.child_a VALUES (300, 200)",+ "INSERT INTO public.cyc_x VALUES (1000, NULL)",+ "INSERT INTO public.cyc_y VALUES (2000, 1000)",+ "UPDATE public.cyc_x SET peer_id = 2000 WHERE id = 1000",+ ] {+ 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-fk-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: false,+ include_create_database: false,+ drop_table_if_exists: false,+ omit_auto_increment: false,+ fail_on_error: true,+ prevent_overwrite: false,+ output_compression: Default::default(),+ insert_dialect: Default::default(),+ snapshot_session_id: None,+ batch_size: 1000,+ split_max_mb: None,+ },+ |_| {},+ )+ .await+ .expect("export PostgreSQL database with a foreign key cycle");++ let exported = std::fs::read_to_string(&export_path).expect("read exported SQL");++ // No foreign key may stay inline in a CREATE TABLE body: every constraint+ // line must be one of the deferred ALTER statements written at the end.+ for line in exported.lines().filter(|line| line.to_ascii_uppercase().contains("FOREIGN KEY")) {+ assert!(+ line.trim_start().to_ascii_uppercase().starts_with("ALTER TABLE "),+ "inline foreign key left in export: {line}\n\n{exported}"+ );+ }+ for expected in [+ "ALTER TABLE \"public\".\"child_versions\" ADD CONSTRAINT \"fk_child_parent\" FOREIGN KEY (\"parent_id\") REFERENCES \"public\".\"parent_entities\"(\"id\");",+ "ALTER TABLE \"public\".\"cyc_x\" ADD CONSTRAINT \"cyc_x_y_fk\" FOREIGN KEY (\"peer_id\") REFERENCES \"public\".\"cyc_y\"(\"id\");",+ "ALTER TABLE \"public\".\"cyc_y\" ADD CONSTRAINT \"cyc_y_x_fk\" FOREIGN KEY (\"peer_id\") REFERENCES \"public\".\"cyc_x\"(\"id\");",+ ] {+ assert!(exported.contains(expected), "missing deferred foreign key: {expected}\n\n{exported}");+ }+ // Deferred statements must come after every CREATE TABLE, i.e. after all+ // data as well, so replay never depends on table or row order.+ let last_create = exported.rfind("CREATE TABLE ").expect("export contains CREATE TABLE");+ let first_foreign_key_alter =+ exported.find("ALTER TABLE").expect("export contains deferred ALTER TABLE statements");+ assert!(last_create < first_foreign_key_alter, "foreign keys must be deferred after all tables:\n{exported}");++ // The actual regression: replay the backup into an empty database.+ let restore = dbx_core::sql_file_import::execute_sql_file_path(+ &state,+ &SqlFileRequest {+ txn_session_id: None,+ execution_id: format!("pg-fk-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,+ },+ &export_path,+ tokio_util::sync::CancellationToken::new(),+ std::time::Instant::now(),+ |_| {},+ )+ .await;+ assert!(restore.is_ok(), "restoring the exported backup failed: {:?}", restore.err());++ let fk_count = execute_sql_statement(+ &state,+ &target_connection_id,+ &target_database,+ "SELECT count(*) FROM information_schema.table_constraints \+ WHERE constraint_type = 'FOREIGN KEY' AND table_schema = 'public'",+ None,+ None,+ )+ .await+ .expect("count restored foreign keys");+ assert_eq!(fk_count.rows, vec![vec![serde_json::json!(5)]]);++ let child_rows = execute_sql_statement(+ &state,+ &target_connection_id,+ &target_database,+ "SELECT count(*) FROM public.child_a",+ None,+ None,+ )+ .await+ .expect("count restored child rows");+ assert_eq!(child_rows.rows, vec![vec![serde_json::json!(1)]]);++ 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);+}