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): infer serial types from nextval defaults
dienasof438dd8d79b9
diff --git a/crates/dbx-core/src/schema/mod.rs b/crates/dbx-core/src/schema/mod.rsindex 18175e2f93..989e908ee2 100644--- a/crates/dbx-core/src/schema/mod.rs+++ b/crates/dbx-core/src/schema/mod.rs@@ -9708,7 +9708,7 @@ pub async fn get_table_export_ddl_core(table: &str,object_type: Option<db::ObjectSourceKind>,) -> Result<String, String> {- get_table_ddl_core_with_options(+ let ddl = get_table_ddl_core_with_options(state,connection_id,database,@@ -9718,7 +9718,8 @@ pub async fn get_table_export_ddl_core(TableDdlOptions::EXPORT,None,)- .await+ .await?;+ Ok(rewrite_postgres_standard_serial_defaults(&ddl, schema))}/// Source DDL for a table transfer that reuses it on the target.@@ -9751,7 +9752,7 @@ pub(crate) async fn get_table_relation_export_ddl_core_for_session(object_type: Option<db::ObjectSourceKind>,client_session_id: Option<&str>,) -> Result<String, String> {- get_table_ddl_core_with_options(+ let ddl = get_table_ddl_core_with_options(state,connection_id,database,@@ -9761,7 +9762,11 @@ pub(crate) async fn get_table_relation_export_ddl_core_for_session(TableDdlOptions::RELATION_EXPORT,client_session_id,)- .await+ .await?;+ // Whole-database exports and scheduled backups rely on the table DDL to+ // recreate `created_by_table` sequences, so they need the same serial+ // rewrite as the single-table export (#13058).+ Ok(rewrite_postgres_standard_serial_defaults(&ddl, schema))}pub async fn get_table_display_ddl_core(@@ -13406,6 +13411,37 @@ mod ddl_tests {assert!(ddl.contains("\"id\" bigint DEFAULT nextval('shared.custom_id_source'::regclass)"), "ddl: {ddl}");}+ #[test]+ fn postgres_export_ddl_infers_standard_serial_defaults() {+ // #13058: a standalone export must not keep `bigint DEFAULT nextval(...)`+ // for the standard serial sequence, because restoring the SQL would+ // reference a sequence that was never created.+ for (data_type, serial_type) in [("smallint", "smallserial"), ("integer", "serial"), ("bigint", "bigserial")] {+ let ddl = format!(+ "CREATE TABLE \"public\".\"ceshi\" (\n \"ID\" {data_type} DEFAULT nextval('\"ceshi_ID_seq\"'::regclass) NOT NULL,\n \"name\" text\n)"+ );+ let rewritten = rewrite_postgres_standard_serial_defaults(&ddl, "public");+ assert!(rewritten.contains(&format!("\"ID\" {serial_type} NOT NULL")), "ddl: {rewritten}");+ assert!(!rewritten.contains("nextval("), "ddl: {rewritten}");+ }++ // A schema-qualified standard sequence name is also recognized.+ let ddl = "CREATE TABLE \"app\".\"ceshi\" (\n \"ID\" bigint DEFAULT nextval('\"app\".\"ceshi_ID_seq\"'::regclass)\n)";+ let rewritten = rewrite_postgres_standard_serial_defaults(ddl, "app");+ assert!(rewritten.contains("\"ID\" bigserial"), "ddl: {rewritten}");+ assert!(!rewritten.contains("nextval("), "ddl: {rewritten}");++ // Custom/shared sequences keep the explicit default.+ let custom = "CREATE TABLE \"public\".\"orders\" (\n \"id\" bigint DEFAULT nextval('shared.custom_id_source'::regclass) NOT NULL\n)";+ assert_eq!(rewrite_postgres_standard_serial_defaults(custom, "public"), custom);++ // A standard-looking sequence name that lives in another schema belongs to+ // a shared or manually attached sequence: keep the explicit default, or a+ // restore would re-bind the column to a fresh sequence.+ let shared = "CREATE TABLE \"public\".\"ceshi\" (\n \"ID\" bigint DEFAULT nextval('\"app\".\"ceshi_ID_seq\"'::regclass) NOT NULL\n)";+ assert_eq!(rewrite_postgres_standard_serial_defaults(shared, "public"), shared);+ }+#[test]fn postgres_table_ddl_keeps_generated_columns_distinct_from_serial_markers() {let mut generated = column("total", "numeric");@@ -15902,6 +15938,71 @@ pub async fn opentenbase_ddl(}}+/// Whether a `nextval` sequence is the standard `<table>_<column>_seq` serial+/// sequence for this column, and which serial spelling it maps to.+fn postgres_serial_type_for_sequence(+ table: &str,+ table_schema: &str,+ column: &str,+ data_type: &str,+ sequence: &str,+) -> Option<&'static str> {+ // A schema-qualified reference must point into the table's own schema: a+ // shared or manually attached sequence in another schema that merely matches+ // the standard name pattern must keep its explicit default, otherwise a+ // restore would re-bind the column to a freshly created sequence.+ if let Some((sequence_schema, _)) = sequence.split_once('.') {+ let sequence_schema = sequence_schema.trim().trim_matches('"').replace("\"\"", "\"");+ if !sequence_schema.eq_ignore_ascii_case(table_schema) {+ return None;+ }+ }+ let unqualified = sequence.rsplit('.').next().unwrap_or(sequence).trim().trim_matches('"').replace("\"\"", "\"");+ if !unqualified.eq_ignore_ascii_case(&format!("{table}_{column}_seq")) {+ return None;+ }+ match data_type.trim().to_ascii_lowercase().as_str() {+ "smallint" | "int2" => Some("smallserial"),+ "integer" | "int" | "int4" => Some("serial"),+ "bigint" | "int8" => Some("bigserial"),+ _ => None,+ }+}++/// Rewrites standard serial defaults in an exported PostgreSQL table DDL.+///+/// `BIGSERIAL` is stored as `bigint DEFAULT nextval('<table>_<column>_seq'::regclass)`.+/// The renderer keeps that spelling when the metadata does not carry the serial+/// marker, which is correct for transfers (the sequence is copied separately) but+/// not for a standalone export: restoring the exported SQL would reference a+/// sequence that was never created. Rewrite only the standard serial sequence+/// name; shared/custom sequences keep their explicit default.+fn rewrite_postgres_standard_serial_defaults(ddl: &str, table_schema: &str) -> String {+ let create_table = Regex::new(r#"(?i)^CREATE TABLE\s+(?:"(?:[^"]|"")*"\.)?"((?:[^"]|"")*)""#).unwrap();+ let serial_column = Regex::new(+ r#"(?i)^(\s*)"((?:[^"]|"")*)"\s+(smallint|integer|bigint)\s+DEFAULT\s+nextval\('([^']*)'::regclass\)(.*)$"#,+ )+ .unwrap();+ let mut current_table: Option<String> = None;+ let mut output = Vec::with_capacity(ddl.len());+ for line in ddl.split('\n') {+ let trimmed = line.trim_start();+ if trimmed.starts_with("CREATE TABLE ") {+ current_table = create_table.captures(trimmed).map(|caps| caps[1].replace("\"\"", "\""));+ } else if trimmed.starts_with(')') {+ current_table = None;+ }+ let rewritten = current_table.as_deref().and_then(|table| {+ let caps = serial_column.captures(line)?;+ let column = caps[2].replace("\"\"", "\"");+ let serial_type = postgres_serial_type_for_sequence(table, table_schema, &column, &caps[3], &caps[4])?;+ Some(format!("{}\"{}\" {}{}", &caps[1], &caps[2], serial_type, &caps[5]))+ });+ output.push(rewritten.unwrap_or_else(|| line.to_string()));+ }+ output.join("\n")+}+pub fn render_postgres_table_ddl(schema: &str,table: &str,