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(sql-schema): omit source sequence default in target DDL

Thanadon Kongkanund6c803b17bf5
diff --git a/crates/dbx-sql-dialect/src/sql_dialect/type_rewrite.rs b/crates/dbx-sql-dialect/src/sql_dialect/type_rewrite.rs
index 11accdf3b1..589eccc7c5 100644
--- a/crates/dbx-sql-dialect/src/sql_dialect/type_rewrite.rs
+++ b/crates/dbx-sql-dialect/src/sql_dialect/type_rewrite.rs
@@ -435,7 +435,14 @@ pub fn apply_auto_inc_to_column_def(
AutoIncColumnBuild::AppendSuffix { suffix, skip_default: false, postgres_sequence: false }
}
crate::sql_dialect::ddl_profile::AutoIncSyntax::PostgresSequence if col.is_primary_key && is_integer_like => {
- AutoIncColumnBuild::AppendSuffix { suffix: "", skip_default: false, postgres_sequence: true }
+ // The caller emits `ALTER TABLE ... SET DEFAULT nextval('<new>_<col>_seq')`
+ // right after creating that sequence (see `generate_create_table_sql`), so
+ // the inline default here must be skipped. `col.column_default` is the raw
+ // value read from the *source* column — typically `nextval('<old>_id_seq'
+ // ::regclass)` pointing at the source database's own sequence — and inlining
+ // it fails the CREATE TABLE outright on any target that doesn't already have
+ // that exact sequence name (#10086: "relation ... does not exist").
+ AutoIncColumnBuild::AppendSuffix { suffix: "", skip_default: true, postgres_sequence: true }
}
_ => AutoIncColumnBuild::Normal { skip_default: false },
}
diff --git a/crates/dbx-sql-schema/src/schema_diff.rs b/crates/dbx-sql-schema/src/schema_diff.rs
index 6a735ffe76..13e075b167 100644
--- a/crates/dbx-sql-schema/src/schema_diff.rs
+++ b/crates/dbx-sql-schema/src/schema_diff.rs
@@ -7139,6 +7139,56 @@ mod tests {
assert!(sql.contains("INT"), "integer→INT: {sql}");
}
+ #[test]
+ fn postgres_create_table_serial_pk_omits_source_sequence_default() {
+ // #10086: on a Postgres-family target (Kingbase in the report), the source
+ // column's raw `column_default` is a live `nextval('<old>_id_seq'::regclass)`
+ // cast pointing at the *source* database's sequence. Inlining that into
+ // `CREATE TABLE ... DEFAULT nextval(...)` fails at parse time on the target
+ // because that sequence doesn't exist there yet ("relation ... does not
+ // exist") — before the table is even created. The correct default is the
+ // `ALTER TABLE ... SET DEFAULT nextval('<name>_<col>_seq')` this function
+ // already emits further down, against the *new* sequence it just created.
+ let columns = vec![ColumnDiff {
+ diff_type: "added".into(),
+ name: "id".into(),
+ source: Some(ColumnInfo {
+ name: "id".into(),
+ data_type: "bigint".into(),
+ is_nullable: false,
+ is_primary_key: true,
+ column_default: Some("nextval('collection_abnormal_record_id_seq'::regclass)".into()),
+ ..Default::default()
+ }),
+ target: None,
+ changes: vec![],
+ add_position: None,
+ }];
+ let (sql, missing) = generate_create_table_sql(
+ "collection_abnormal_record",
+ &columns,
+ &[],
+ &[],
+ None,
+ DatabaseType::Postgres,
+ None,
+ Some(DialectKind::Postgres),
+ &[],
+ &[],
+ );
+ assert!(missing.is_empty(), "{missing:?}");
+ let create_table = sql.split("CREATE SEQUENCE").next().unwrap();
+ assert!(
+ !create_table.contains("nextval"),
+ "CREATE TABLE must not reference the source's sequence before the new one exists: {sql}"
+ );
+ assert!(sql.contains("CREATE SEQUENCE IF NOT EXISTS"), "new sequence: {sql}");
+ assert!(
+ sql.contains("ALTER TABLE \"collection_abnormal_record\" ALTER COLUMN \"id\" SET DEFAULT nextval("),
+ "default wired up after the sequence exists: {sql}"
+ );
+ }
+
#[test]
fn sync_to_sqlite_keeps_plain_integer_pk_without_autoincrement() {
let columns = vec![ColumnDiff {