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): preserve partition child DDL
t8y269be081a6f08
diff --git a/crates/dbx-core/src/db/postgres.rs b/crates/dbx-core/src/db/postgres.rsindex 0b1df8eb40..a39d1d3d59 100644--- a/crates/dbx-core/src/db/postgres.rs+++ b/crates/dbx-core/src/db/postgres.rs@@ -8,6 +8,7 @@ use rustls::client::verify_server_cert_signed_by_trust_anchor;use rustls::crypto::{verify_tls12_signature, verify_tls13_signature, CryptoProvider};use rustls::pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime};use rustls::server::ParsedCertificate;+use std::collections::BTreeSet;use std::fs::File;use std::future::Future;use std::io::BufReader;@@ -65,6 +66,22 @@ pub struct PostgresTableAccessInfo {pub privileges: Vec<PostgresTablePrivilegeInfo>,}+#[derive(Debug, Clone, Default, PartialEq, Eq)]+pub struct PostgresTablePartitionInfo {+ pub is_partition: bool,+ pub parent_schema: Option<String>,+ pub parent_table: Option<String>,+ pub bound: Option<String>,+ pub key: Option<String>,+}++#[derive(Debug, Clone, Default, PartialEq, Eq)]+pub struct PostgresTablePartitionLocalObjects {+ pub has_primary_key: bool,+ pub foreign_keys: BTreeSet<String>,+ pub indexes: BTreeSet<String>,+}+fn pg_temporal_to_json_value(row: &Row, idx: usize) -> Option<serde_json::Value> {if let Ok(v) = row.try_get::<_, DateTime<Local>>(idx) {return Some(serde_json::Value::String(format_pg_timestamptz(v)));@@ -2235,40 +2252,115 @@ pub async fn get_table_comment(pool: &Pool, schema: &str, table: &str) -> ResultOk(rows.first().and_then(|row| row.try_get::<_, Option<String>>(0).ok().flatten()).filter(|s| !s.is_empty()))}-pub async fn get_table_partition_key(pool: &Pool, schema: &str, table: &str) -> Result<Option<String>, String> {+pub async fn get_table_partition_info(+ pool: &Pool,+ schema: &str,+ table: &str,+) -> Result<PostgresTablePartitionInfo, String> {let schema = if schema.is_empty() { "public" } else { schema };let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;- // Probe relkind first so PostgreSQL-compatible servers without the PG10- // pg_get_partkeydef function keep ordinary-table DDL unchanged.- if postgres_query_cached(&client, postgres_partitioned_parent_sql(), &[&schema, &table])+ let relation_rows = postgres_query_cached(&client, postgres_table_partition_relation_sql(), &[&schema, &table]).await- .map_err(|e| e.to_string())?- .is_empty()- {- return Ok(None);+ .map_err(|e| e.to_string())?;+ let Some(relation) = relation_rows.first() else {+ return Ok(PostgresTablePartitionInfo::default());+ };+ let relkind = relation.try_get::<_, String>(0).unwrap_or_default();+ let is_partition = relation.try_get::<_, bool>(1).unwrap_or(false);+ if relkind != "p" && !is_partition {+ return Ok(PostgresTablePartitionInfo::default());}- let rows = postgres_query_cached(&client, postgres_table_partition_key_sql(), &[&schema, &table])++ let rows = postgres_query_cached(&client, postgres_table_partition_info_sql(), &[&schema, &table]).await.map_err(|e| e.to_string())?;- Ok(rows.first().and_then(|row| row.try_get::<_, Option<String>>(0).ok().flatten()).filter(|s| !s.is_empty()))+ let Some(row) = rows.first() else {+ return Ok(PostgresTablePartitionInfo { is_partition, ..Default::default() });+ };+ Ok(PostgresTablePartitionInfo {+ is_partition,+ parent_schema: row.try_get::<_, Option<String>>(0).ok().flatten().filter(|value| !value.is_empty()),+ parent_table: row.try_get::<_, Option<String>>(1).ok().flatten().filter(|value| !value.is_empty()),+ bound: row.try_get::<_, Option<String>>(2).ok().flatten().filter(|value| !value.is_empty()),+ key: row.try_get::<_, Option<String>>(3).ok().flatten().filter(|value| !value.is_empty()),+ })+}++pub async fn get_table_partition_key(pool: &Pool, schema: &str, table: &str) -> Result<Option<String>, String> {+ Ok(get_table_partition_info(pool, schema, table).await?.key)+}++pub async fn get_table_partition_local_objects(+ pool: &Pool,+ schema: &str,+ table: &str,+) -> Result<PostgresTablePartitionLocalObjects, String> {+ let schema = if schema.is_empty() { "public" } else { schema };+ let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;+ let rows = postgres_query_cached(&client, postgres_table_partition_local_objects_sql(), &[&schema, &table])+ .await+ .map_err(|e| e.to_string())?;+ let mut result = PostgresTablePartitionLocalObjects::default();+ for row in rows {+ let object_kind = row.try_get::<_, String>(0).unwrap_or_default();+ let object_name = row.try_get::<_, String>(1).unwrap_or_default();+ let object_type = row.try_get::<_, Option<String>>(2).ok().flatten().unwrap_or_default();+ match object_kind.as_str() {+ "constraint" if object_type == "p" => result.has_primary_key = true,+ "constraint" if object_type == "f" && !object_name.is_empty() => {+ result.foreign_keys.insert(object_name);+ }+ "index" if !object_name.is_empty() => {+ result.indexes.insert(object_name);+ }+ _ => {}+ }+ }+ Ok(result)}-fn postgres_partitioned_parent_sql() -> &'static str {- "SELECT 1 \+fn postgres_table_partition_relation_sql() -> &'static str {+ "SELECT c.relkind::text, \+ COALESCE((pg_catalog.row_to_json(c)->>'relispartition')::boolean, false) AS is_partition \FROM pg_catalog.pg_class c \JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \- WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind = 'p' \+ WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind IN ('r','p') \LIMIT 1"}-fn postgres_table_partition_key_sql() -> &'static str {- "SELECT pg_catalog.pg_get_partkeydef(c.oid) AS partition_key \+fn postgres_table_partition_info_sql() -> &'static str {+ "SELECT CASE WHEN c.relispartition THEN pn.nspname ELSE NULL END AS parent_schema, \+ CASE WHEN c.relispartition THEN pc.relname ELSE NULL END AS parent_table, \+ CASE WHEN c.relispartition THEN pg_catalog.pg_get_expr(c.relpartbound, c.oid, true) ELSE NULL END AS partition_bound, \+ CASE WHEN c.relkind = 'p' THEN pg_catalog.pg_get_partkeydef(c.oid) ELSE NULL END AS partition_key \FROM pg_catalog.pg_class c \JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \- WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind = 'p' \+ LEFT JOIN pg_catalog.pg_inherits i ON i.inhrelid = c.oid \+ LEFT JOIN pg_catalog.pg_class pc ON pc.oid = i.inhparent \+ LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid = pc.relnamespace \+ WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind IN ('r','p') \+ ORDER BY i.inhseqno NULLS LAST \LIMIT 1"}+fn postgres_table_partition_local_objects_sql() -> &'static str {+ "SELECT 'constraint'::text AS object_kind, con.conname AS object_name, con.contype::text AS object_type \+ FROM pg_catalog.pg_constraint con \+ JOIN pg_catalog.pg_class c ON c.oid = con.conrelid \+ JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \+ WHERE n.nspname = $1 AND c.relname = $2 AND con.contype IN ('p','f') \+ AND COALESCE(NULLIF(pg_catalog.row_to_json(con)->>'conparentid', '')::oid, 0) = 0 \+ UNION ALL \+ SELECT 'index'::text AS object_kind, idx.relname AS object_name, NULL::text AS object_type \+ FROM pg_catalog.pg_index ix \+ JOIN pg_catalog.pg_class c ON c.oid = ix.indrelid \+ JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \+ JOIN pg_catalog.pg_class idx ON idx.oid = ix.indexrelid \+ WHERE n.nspname = $1 AND c.relname = $2 \+ AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhrelid = idx.oid) \+ ORDER BY object_kind, object_name"+}+fn postgres_table_comment_sql() -> &'static str {"SELECT obj_description(c.oid) AS table_comment \FROM pg_catalog.pg_class c \@@ -5448,18 +5540,21 @@ mod tests {}#[test]- fn postgres_table_partition_key_sql_targets_partitioned_parents() {- let parent_sql = postgres_partitioned_parent_sql();- let sql = postgres_table_partition_key_sql();-- assert!(parent_sql.contains("n.nspname = $1"));- assert!(parent_sql.contains("c.relname = $2"));- assert!(parent_sql.contains("c.relkind = 'p'"));- assert!(sql.contains("pg_catalog.pg_get_partkeydef(c.oid)"));- assert!(sql.contains("n.nspname = $1"));- assert!(sql.contains("c.relname = $2"));- assert!(sql.contains("c.relkind = 'p'"));- assert!(!sql.contains("pg_get_expr(c.relpartbound"));+ fn postgres_table_partition_sql_tracks_parents_bounds_and_local_objects() {+ let relation_sql = postgres_table_partition_relation_sql();+ let info_sql = postgres_table_partition_info_sql();+ let local_objects_sql = postgres_table_partition_local_objects_sql();++ assert!(relation_sql.contains("row_to_json(c)->>'relispartition'"));+ assert!(relation_sql.contains("c.relkind IN ('r','p')"));+ assert!(info_sql.contains("pg_catalog.pg_get_expr(c.relpartbound, c.oid, true)"));+ assert!(info_sql.contains("pg_catalog.pg_get_partkeydef(c.oid)"));+ assert!(info_sql.contains("pg_catalog.pg_inherits"));+ assert!(info_sql.contains("parent_schema"));+ assert!(info_sql.contains("parent_table"));+ assert!(local_objects_sql.contains("row_to_json(con)->>'conparentid'"));+ assert!(local_objects_sql.contains("con.contype IN ('p','f')"));+ assert!(local_objects_sql.contains("i.inhrelid = idx.oid"));}#[test]@@ -5605,34 +5700,93 @@ mod tests {#[tokio::test]#[ignore = "requires DBX_TEST_POSTGRES_URL pointing at a writable PostgreSQL database"]- async fn postgres_partition_key_metadata_reads_parent_only() {+ async fn postgres_partition_metadata_renders_replayable_children_and_subpartitions() {let url = std::env::var("DBX_TEST_POSTGRES_URL").expect("DBX_TEST_POSTGRES_URL");let pool = connect(&url, Duration::from_secs(5)).await.expect("connect postgres");let schema = format!("dbx_partition_meta_{}", std::process::id());let schema_ident = pg_quote_ident(&schema);+ let replay_schema = format!("{schema}_replay");+ let replay_schema_ident = pg_quote_ident(&replay_schema);let parent = format!("{schema_ident}.parent");let child = format!("{schema_ident}.child");+ let default_child = format!("{schema_ident}.child_default");+ let subpartition = format!("{schema_ident}.subpartition");+ let inherited_parent = format!("{schema_ident}.inherited_parent");+ let inherited_child = format!("{schema_ident}.inherited_child");execute_query(&pool, &format!("CREATE SCHEMA {schema_ident}")).await.expect("create schema");let client = pool.get().await.expect("get postgres client");client.batch_execute(&format!("CREATE TABLE {parent} (id integer, payload text) PARTITION BY RANGE (id); \- CREATE TABLE {child} PARTITION OF {parent} FOR VALUES FROM (1) TO (10)"+ CREATE TABLE {child} PARTITION OF {parent} (PRIMARY KEY (id)) FOR VALUES FROM (1) TO (10); \+ CREATE INDEX child_payload_idx ON {child} (payload); \+ CREATE TABLE {default_child} PARTITION OF {parent} DEFAULT; \+ CREATE TABLE {subpartition} PARTITION OF {parent} FOR VALUES FROM (10) TO (20) PARTITION BY HASH (payload); \+ CREATE TABLE {inherited_parent} (id integer, bucket integer, PRIMARY KEY (id, bucket)) PARTITION BY RANGE (bucket); \+ CREATE TABLE {inherited_child} PARTITION OF {inherited_parent} FOR VALUES FROM (1) TO (10)")).await.expect("create partitioned tables");- let parent_key = get_table_partition_key(&pool, &schema, "parent").await.expect("parent metadata");- let child_key = get_table_partition_key(&pool, &schema, "child").await.expect("child metadata");+ let parent_info = get_table_partition_info(&pool, &schema, "parent").await.expect("parent metadata");+ let child_info = get_table_partition_info(&pool, &schema, "child").await.expect("child metadata");+ let default_info = get_table_partition_info(&pool, &schema, "child_default").await.expect("default metadata");+ let subpartition_info =+ get_table_partition_info(&pool, &schema, "subpartition").await.expect("subpartition metadata");+ let child_local_objects =+ get_table_partition_local_objects(&pool, &schema, "child").await.expect("child local objects");+ let inherited_child_local_objects = get_table_partition_local_objects(&pool, &schema, "inherited_child")+ .await+ .expect("inherited child local objects");let parent_ddl = crate::schema::pg_ddl(&pool, &schema, "parent").await.expect("parent ddl");+ let child_ddl = crate::schema::pg_ddl(&pool, &schema, "child").await.expect("child ddl");+ let default_ddl = crate::schema::pg_ddl(&pool, &schema, "child_default").await.expect("default ddl");+ let subpartition_ddl = crate::schema::pg_ddl(&pool, &schema, "subpartition").await.expect("subpartition ddl");+ let inherited_parent_ddl =+ crate::schema::pg_ddl(&pool, &schema, "inherited_parent").await.expect("inherited parent ddl");+ let inherited_child_ddl =+ crate::schema::pg_ddl(&pool, &schema, "inherited_child").await.expect("inherited child ddl");++ execute_query(&pool, &format!("CREATE SCHEMA {replay_schema_ident}")).await.expect("create replay schema");+ for ddl in+ [&parent_ddl, &child_ddl, &default_ddl, &subpartition_ddl, &inherited_parent_ddl, &inherited_child_ddl]+ {+ client+ .batch_execute(&ddl.replace(&schema_ident, &replay_schema_ident))+ .await+ .unwrap_or_else(|error| panic!("replay partition ddl failed: {error}; ddl: {ddl}"));+ }- execute_query(&pool, &format!("DROP SCHEMA {schema_ident} CASCADE")).await.expect("drop schema");-- assert_eq!(parent_key, Some("RANGE (id)".to_string()));- assert_eq!(child_key, None);+ client+ .batch_execute(&format!("DROP SCHEMA {schema_ident} CASCADE; DROP SCHEMA {replay_schema_ident} CASCADE"))+ .await+ .expect("drop schemas");++ assert_eq!(parent_info.key.as_deref(), Some("RANGE (id)"));+ assert!(!parent_info.is_partition);+ assert_eq!(child_info.parent_schema.as_deref(), Some(schema.as_str()));+ assert_eq!(child_info.parent_table.as_deref(), Some("parent"));+ assert_eq!(child_info.bound.as_deref(), Some("FOR VALUES FROM (1) TO (10)"));+ assert!(child_local_objects.has_primary_key);+ assert!(child_local_objects.indexes.contains("child_payload_idx"));+ assert!(!inherited_child_local_objects.has_primary_key);+ assert!(inherited_child_local_objects.indexes.is_empty());+ assert_eq!(default_info.bound.as_deref(), Some("DEFAULT"));+ assert_eq!(subpartition_info.key.as_deref(), Some("HASH (payload)"));assert!(parent_ddl.contains(") PARTITION BY RANGE (id);"), "ddl: {parent_ddl}");- assert!(!parent_ddl.contains("PARTITION OF"));+ assert!(child_ddl.contains("CREATE TABLE"), "ddl: {child_ddl}");+ assert!(child_ddl.contains("PARTITION OF"), "ddl: {child_ddl}");+ assert!(child_ddl.contains("PRIMARY KEY (\"id\")"), "ddl: {child_ddl}");+ assert!(child_ddl.contains("FOR VALUES FROM (1) TO (10);"), "ddl: {child_ddl}");+ assert!(child_ddl.contains("CREATE INDEX \"child_payload_idx\""), "ddl: {child_ddl}");+ assert!(default_ddl.contains(" DEFAULT;"), "ddl: {default_ddl}");+ assert!(+ subpartition_ddl.contains("FOR VALUES FROM (10) TO (20) PARTITION BY HASH (payload);"),+ "ddl: {subpartition_ddl}"+ );+ assert!(!inherited_child_ddl.contains("PRIMARY KEY"), "ddl: {inherited_child_ddl}");+ assert!(!inherited_child_ddl.contains("CREATE INDEX"), "ddl: {inherited_child_ddl}");}#[tokio::test]diff --git a/crates/dbx-core/src/schema.rs b/crates/dbx-core/src/schema.rsindex 480355eaba..ea9f927db7 100644--- a/crates/dbx-core/src/schema.rs+++ b/crates/dbx-core/src/schema.rs@@ -7674,14 +7674,18 @@ mod ddl_tests {"HASH ((lower(code)))","RANGE (date_trunc('month'::text, created_at))",] {- let ddl = render_postgres_table_ddl_with_partition_key(+ let ddl = render_postgres_table_ddl_with_partition_info("public","events",&[column("created_at", "timestamp without time zone")],&[],&[],None,- Some(partition_key),+ &db::postgres::PostgresTablePartitionInfo {+ key: Some(partition_key.to_string()),+ ..Default::default()+ },+ &db::postgres::PostgresTablePartitionLocalObjects::default(),);assert!(ddl.ends_with(&format!(") PARTITION BY {partition_key};\n")), "ddl: {ddl}");@@ -7691,20 +7695,103 @@ mod ddl_tests {#[test]fn postgres_table_ddl_keeps_ordinary_table_unchanged() {- let ddl = render_postgres_table_ddl_with_partition_key(+ let ddl = render_postgres_table_ddl_with_partition_info("public","users",&[column("id", "integer")],&[],&[],None,- None,+ &db::postgres::PostgresTablePartitionInfo::default(),+ &db::postgres::PostgresTablePartitionLocalObjects::default(),);assert!(ddl.ends_with(");\n"), "ddl: {ddl}");assert!(!ddl.contains("PARTITION BY"));}+ #[test]+ fn postgres_table_ddl_renders_partition_children_and_subpartitions() {+ let mut id = column("id", "integer");+ id.is_primary_key = true;+ let indexes = vec![db::IndexInfo {+ name: "events_payload_idx".to_string(),+ columns: vec!["payload".to_string()],+ is_unique: false,+ is_primary: false,+ filter: None,+ index_type: Some("btree".to_string()),+ included_columns: None,+ comment: None,+ }];+ let partition_info = db::postgres::PostgresTablePartitionInfo {+ is_partition: true,+ parent_schema: Some("public".to_string()),+ parent_table: Some("events".to_string()),+ bound: Some("FOR VALUES FROM ('2026-01-01') TO ('2027-01-01')".to_string()),+ key: Some("HASH (payload)".to_string()),+ };+ let partition_local_objects = db::postgres::PostgresTablePartitionLocalObjects {+ has_primary_key: true,+ foreign_keys: BTreeSet::new(),+ indexes: BTreeSet::from(["events_payload_idx".to_string()]),+ };++ let ddl = render_postgres_table_ddl_with_partition_info(+ "public",+ "events_2026",+ &[id, column("payload", "text")],+ &indexes,+ &[],+ None,+ &partition_info,+ &partition_local_objects,+ );++ assert!(ddl.starts_with(+ "CREATE TABLE \"public\".\"events_2026\" PARTITION OF \"public\".\"events\" (\n PRIMARY KEY (\"id\")\n)"+ ));+ assert!(ddl.contains("FOR VALUES FROM ('2026-01-01') TO ('2027-01-01') PARTITION BY HASH (payload);"));+ assert!(ddl.contains("CREATE INDEX \"events_payload_idx\""));+ assert!(!ddl.contains("\"payload\" text"));+ }++ #[test]+ fn postgres_partition_ddl_skips_inherited_constraints_and_indexes() {+ let mut id = column("id", "integer");+ id.is_primary_key = true;+ let indexes = vec![db::IndexInfo {+ name: "events_2026_pkey".to_string(),+ columns: vec!["id".to_string()],+ is_unique: true,+ is_primary: true,+ filter: None,+ index_type: Some("btree".to_string()),+ included_columns: None,+ comment: None,+ }];+ let partition_info = db::postgres::PostgresTablePartitionInfo {+ is_partition: true,+ parent_schema: Some("public".to_string()),+ parent_table: Some("events".to_string()),+ bound: Some("DEFAULT".to_string()),+ key: None,+ };++ let ddl = render_postgres_table_ddl_with_partition_info(+ "public",+ "events_default",+ &[id],+ &indexes,+ &[],+ None,+ &partition_info,+ &db::postgres::PostgresTablePartitionLocalObjects::default(),+ );++ assert_eq!(ddl, "CREATE TABLE \"public\".\"events_default\" PARTITION OF \"public\".\"events\" DEFAULT;\n");+ }+#[test]fn postgres_table_ddl_keeps_composite_foreign_key_together() {let columns = vec![column("a", "integer"), column("b", "integer"), column("c", "integer")];@@ -7939,24 +8026,30 @@ pub async fn sqlite_ddl(pool: &db::sqlite::SqliteHandle, schema: &str, table: &s}pub async fn pg_ddl(pool: &deadpool_postgres::Pool, schema: &str, table: &str) -> Result<String, String> {- let (columns, indexes, fkeys, table_comment, partition_key, trigger_definitions) = tokio::try_join!(+ let (columns, indexes, fkeys, table_comment, partition_info, trigger_definitions) = tokio::try_join!(db::postgres::get_columns(pool, schema, table),db::postgres::list_indexes(pool, schema, table),db::postgres::list_foreign_keys(pool, schema, table),async { db::postgres::get_table_comment(pool, schema, table).await },- db::postgres::get_table_partition_key(pool, schema, table),+ db::postgres::get_table_partition_info(pool, schema, table),db::postgres::list_trigger_definitions(pool, schema, table),)?;+ let partition_local_objects = if partition_info.is_partition {+ db::postgres::get_table_partition_local_objects(pool, schema, table).await?+ } else {+ db::postgres::PostgresTablePartitionLocalObjects::default()+ };Ok(append_postgres_trigger_definitions(- render_postgres_table_ddl_with_partition_key(+ render_postgres_table_ddl_with_partition_info(schema,table,&columns,&indexes,&fkeys,table_comment.as_deref(),- partition_key.as_deref(),+ &partition_info,+ &partition_local_objects,),&trigger_definitions,))@@ -8248,68 +8341,110 @@ pub fn render_postgres_table_ddl(fkeys: &[db::ForeignKeyInfo],table_comment: Option<&str>,) -> String {- render_postgres_table_ddl_with_partition_key(schema, table, columns, indexes, fkeys, table_comment, None)+ render_postgres_table_ddl_with_partition_info(+ schema,+ table,+ columns,+ indexes,+ fkeys,+ table_comment,+ &db::postgres::PostgresTablePartitionInfo::default(),+ &db::postgres::PostgresTablePartitionLocalObjects::default(),+ )}-fn render_postgres_table_ddl_with_partition_key(+fn render_postgres_table_ddl_with_partition_info(schema: &str,table: &str,columns: &[db::ColumnInfo],indexes: &[db::IndexInfo],fkeys: &[db::ForeignKeyInfo],table_comment: Option<&str>,- partition_key: Option<&str>,+ partition_info: &db::postgres::PostgresTablePartitionInfo,+ partition_local_objects: &db::postgres::PostgresTablePartitionLocalObjects,) -> String {let table_name = format!("{}.{}", pg_ident(schema), pg_ident(table));- let mut ddl = format!("CREATE TABLE {table_name} (\n");- let col_lines: Vec<String> = columns- .iter()- .map(|c| {- let mut line = format!(" {} {}", pg_ident(&c.name), c.data_type);- let generated_clause = c- .extra- .as_deref()- .map(str::trim)- .filter(|value| !value.is_empty() && value.to_ascii_lowercase().starts_with("generated "));- if let Some(extra) = generated_clause {- line.push_str(&format!(" {extra}"));- }- if !c.is_nullable {- line.push_str(" NOT NULL");- }- if generated_clause.is_none() {- if let Some(ref def) = c.column_default {- line.push_str(&format!(" DEFAULT {def}"));- }- }- line+ let partition_parent = partition_info+ .is_partition+ .then(|| {+ Some((+ partition_info.parent_schema.as_deref()?,+ partition_info.parent_table.as_deref()?,+ partition_info.bound.as_deref()?,+ ))})- .collect();- ddl.push_str(&col_lines.join(",\n"));+ .flatten();+ let is_partition = partition_parent.is_some();+ let mut definition_lines = if is_partition {+ Vec::new()+ } else {+ columns+ .iter()+ .map(|c| {+ let mut line = format!(" {} {}", pg_ident(&c.name), c.data_type);+ let generated_clause = c+ .extra+ .as_deref()+ .map(str::trim)+ .filter(|value| !value.is_empty() && value.to_ascii_lowercase().starts_with("generated "));+ if let Some(extra) = generated_clause {+ line.push_str(&format!(" {extra}"));+ }+ if !c.is_nullable {+ line.push_str(" NOT NULL");+ }+ if generated_clause.is_none() {+ if let Some(ref def) = c.column_default {+ line.push_str(&format!(" DEFAULT {def}"));+ }+ }+ line+ })+ .collect::<Vec<_>>()+ };- let pks: Vec<&str> = columns.iter().filter(|c| c.is_primary_key).map(|c| c.name.as_str()).collect();+ let pks: Vec<&str> = if !is_partition || partition_local_objects.has_primary_key {+ columns.iter().filter(|c| c.is_primary_key).map(|c| c.name.as_str()).collect()+ } else {+ Vec::new()+ };if !pks.is_empty() {- ddl.push_str(&format!(",\n PRIMARY KEY ({})", pks.iter().map(|k| pg_ident(k)).collect::<Vec<_>>().join(", ")));+ definition_lines+ .push(format!(" PRIMARY KEY ({})", pks.iter().map(|key| pg_ident(key)).collect::<Vec<_>>().join(", ")));}for fk_group in group_foreign_keys_by_name(fkeys) {let Some(first_fk) = fk_group.first() else {continue;};+ if is_partition && !partition_local_objects.foreign_keys.contains(&first_fk.name) {+ continue;+ }let columns = fk_group.iter().map(|fk| pg_ident(&fk.column)).collect::<Vec<_>>().join(", ");let ref_columns = fk_group.iter().map(|fk| pg_ident(&fk.ref_column)).collect::<Vec<_>>().join(", ");- ddl.push_str(&format!(- ",\n CONSTRAINT {} FOREIGN KEY ({}) REFERENCES {}({})",+ definition_lines.push(format!(+ " CONSTRAINT {} FOREIGN KEY ({}) REFERENCES {}({})",pg_ident(&first_fk.name),columns,pg_ident(&first_fk.ref_table),ref_columns));}- if let Some(partition_key) = partition_key.filter(|key| !key.trim().is_empty()) {- ddl.push_str(&format!("\n) PARTITION BY {partition_key};\n"));++ let mut ddl = if let Some((parent_schema, parent_table, bound)) = partition_parent {+ let parent_name = format!("{}.{}", pg_ident(parent_schema), pg_ident(parent_table));+ let definitions = if definition_lines.is_empty() {+ String::new()+ } else {+ format!(" (\n{}\n)", definition_lines.join(",\n"))+ };+ format!("CREATE TABLE {table_name} PARTITION OF {parent_name}{definitions} {bound}")} else {- ddl.push_str("\n);\n");+ format!("CREATE TABLE {table_name} (\n{}\n)", definition_lines.join(",\n"))+ };+ if let Some(partition_key) = partition_info.key.as_deref().filter(|key| !key.trim().is_empty()) {+ ddl.push_str(&format!(" PARTITION BY {partition_key}"));}+ ddl.push_str(";\n");if let Some(comment) = table_comment.filter(|comment| !comment.trim().is_empty()) {ddl.push_str(&format!("\nCOMMENT ON TABLE {table_name} IS {};", sql_string(comment)));@@ -8329,6 +8464,9 @@ fn render_postgres_table_ddl_with_partition_key(if idx.is_primary {continue;}+ if is_partition && !partition_local_objects.indexes.contains(&idx.name) {+ continue;+ }let unique = if idx.is_unique { "UNIQUE " } else { "" };let cols = idx.columns.iter().map(|c| pg_ident(c)).collect::<Vec<_>>().join(", ");let using = idx.index_type.as_deref().map(|t| format!(" USING {t}")).unwrap_or_default();