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(transfer): preserve explicit auto-increment as native identity

ordinary beingd219f2b8ba92
diff --git a/crates/dbx-core/src/data/transfer.rs b/crates/dbx-core/src/data/transfer.rs
index ec347a25a7..388e1fb7c2 100644
--- a/crates/dbx-core/src/data/transfer.rs
+++ b/crates/dbx-core/src/data/transfer.rs
@@ -41,6 +41,10 @@ use crate::query::{
StreamProgressClock, AGENT_PROTOCOL_MAX_ROWS,
};
use crate::sql::{split_sql_statements, split_sql_statements_for_database};
+use crate::sql_dialect::ddl_profile::{profile_for, AutoIncSyntax};
+use crate::sql_dialect::type_rewrite::{
+ apply_auto_inc_to_column_def, column_is_auto_increment, type_looks_integer, AutoIncColumnBuild,
+};
use crate::sql_dialect::{
build_iris_table_select_sql, normalize_len_params, qualified_transfer_table, quote_transfer_identifier,
transfer_column_identifier,
@@ -4310,47 +4314,109 @@ fn generate_create_table_ddl_with_column_quoting(
| DatabaseType::Sundb
);
+ // Auto-increment beyond the MySQL family goes through the shared dialect
+ // profile (the same facility the structure-diff generator uses), so the target
+ // spells it natively: Dameng and SQL Server `IDENTITY(1,1)`, Oracle
+ // `GENERATED BY DEFAULT AS IDENTITY`, and so on.
+ //
+ // Only an explicit source auto-increment qualifies. A transfer must not invent
+ // identity the source never declared, and both MySQL (one AUTO_INCREMENT
+ // column per table) and DM8 (one identity column per table) cap the clause at
+ // one column, which the explicit-only rule keeps true for composite primary
+ // keys as well.
+ //
+ // MySQL-family targets keep the inline clause below. Targets that stay on the
+ // plain path:
+ // - The profile-level MySQL family beyond the transfer's own list (Databend,
+ // Gbase, ManticoreSearch): they reject the `AUTO_INCREMENT` spelling.
+ // - Postgres family (`PostgresSequence`): the target needs a created sequence
+ // plus `ALTER TABLE ... SET DEFAULT`, which this generator does not emit;
+ // those transfers keep their own sequence handling.
+ // - SQLite family: `AUTOINCREMENT` is only valid as part of an inline
+ // `INTEGER PRIMARY KEY`, and primary keys here are a table-level clause —
+ // SQLite's rowid alias already generates ids for `INTEGER PRIMARY KEY`.
+ let target_profile = profile_for(*target_db);
+ let inline_identity = !is_mysql_family
+ && !matches!(
+ target_db,
+ DatabaseType::Sqlite | DatabaseType::Rqlite | DatabaseType::Turso | DatabaseType::CloudflareD1
+ )
+ && match target_profile.auto_inc {
+ // `profile_for` puts Databend/Gbase/ManticoreSearch in the MySQL family
+ // too, but their grammars reject MySQL's `AUTO_INCREMENT` spelling; only
+ // the transfer's own MySQL-family targets above keep it.
+ AutoIncSyntax::Suffix(suffix) => suffix != " AUTO_INCREMENT",
+ AutoIncSyntax::ReplaceTypeWith(_) => true,
+ _ => false,
+ };
+
let mut col_lines = Vec::with_capacity(columns.len());
for c in columns {
col_lines.push({
let mapped_type = postgres_column_type_sql(c, source_schema, schema, source_db, target_db);
- let mut line = format!(
- " {} {}",
- transfer_column_identifier(&c.name, target_db, quote_target_column_names),
- mapped_type
- );
- if let Some(default_clause) = column_default_clause(c, source_schema, schema, source_db, target_db) {
- line.push(' ');
- line.push_str(&default_clause);
- }
- if !c.is_nullable
- && !matches!(
- target_db,
- DatabaseType::Hive
- | DatabaseType::Kyuubi
- | DatabaseType::Impala
- | DatabaseType::Argo
- | DatabaseType::Transwarp
+ let column_name = transfer_column_identifier(&c.name, target_db, quote_target_column_names);
+ let identity_build = if inline_identity && c.is_primary_key && column_is_auto_increment(c) {
+ apply_auto_inc_to_column_def(
+ &target_profile,
+ &column_name,
+ &mapped_type,
+ c,
+ type_looks_integer(&mapped_type),
)
- {
- line.push_str(" NOT NULL");
- }
- if is_mysql_family {
- let extra_clauses = parse_mysql_extra_clauses(c.extra.as_deref());
- if extra_clauses.auto_increment {
- line.push_str(" AUTO_INCREMENT");
- }
- if let Some(on_update_expr) = extra_clauses.on_update {
- line.push_str(&format!(" ON UPDATE {on_update_expr}"));
- }
- if let Some(ref comment) = c.comment {
- let trimmed = comment.trim();
- if !trimmed.is_empty() {
- line.push_str(&format!(" COMMENT '{}'", trimmed.replace('\'', "''")));
+ } else {
+ AutoIncColumnBuild::Normal { skip_default: false }
+ };
+ match identity_build {
+ // Profiles that replace the whole definition (e.g. Access `COUNTER`)
+ // already include everything the column needs.
+ AutoIncColumnBuild::Complete { def, .. } => format!(" {def}"),
+ build => {
+ let mut line = format!(" {column_name} {mapped_type}");
+ let skip_default = match build {
+ AutoIncColumnBuild::AppendSuffix { suffix, skip_default, .. } => {
+ line.push_str(suffix);
+ skip_default
+ }
+ _ => false,
+ };
+ if !skip_default {
+ if let Some(default_clause) =
+ column_default_clause(c, source_schema, schema, source_db, target_db)
+ {
+ line.push(' ');
+ line.push_str(&default_clause);
+ }
}
+ if !c.is_nullable
+ && !matches!(
+ target_db,
+ DatabaseType::Hive
+ | DatabaseType::Kyuubi
+ | DatabaseType::Impala
+ | DatabaseType::Argo
+ | DatabaseType::Transwarp
+ )
+ {
+ line.push_str(" NOT NULL");
+ }
+ if is_mysql_family {
+ let extra_clauses = parse_mysql_extra_clauses(c.extra.as_deref());
+ if extra_clauses.auto_increment {
+ line.push_str(" AUTO_INCREMENT");
+ }
+ if let Some(on_update_expr) = extra_clauses.on_update {
+ line.push_str(&format!(" ON UPDATE {on_update_expr}"));
+ }
+ if let Some(ref comment) = c.comment {
+ let trimmed = comment.trim();
+ if !trimmed.is_empty() {
+ line.push_str(&format!(" COMMENT '{}'", trimmed.replace('\'', "''")));
+ }
+ }
+ }
+ line
}
}
- line
});
}
@@ -21190,6 +21256,165 @@ SELECT 1 FROM dual"#
generate_create_table_ddl(&cols, "users", "", "", &DatabaseType::Sqlite, &DatabaseType::Mysql, None, None);
assert!(!ddl.contains("AUTO_INCREMENT"), "non-mysql target should not emit AUTO_INCREMENT: {ddl}");
+ // SQLite's `AUTOINCREMENT` is only valid inside an inline
+ // `INTEGER PRIMARY KEY`, which this generator cannot emit (primary keys are
+ // a table-level clause); the rowid alias already generates ids.
+ assert!(!ddl.contains("AUTOINCREMENT"), "sqlite: {ddl}");
+ }
+
+ #[test]
+ fn transfer_create_table_preserves_explicit_auto_increment_for_dameng() {
+ let cols = vec![db::ColumnInfo {
+ is_primary_key: true,
+ is_nullable: false,
+ extra: Some("auto_increment".to_string()),
+ ..test_column("id", "int")
+ }];
+
+ let ddl = generate_create_table_ddl(
+ &cols,
+ "users",
+ "source",
+ "SYSDBA",
+ &DatabaseType::Dameng,
+ &DatabaseType::Mysql,
+ None,
+ None,
+ );
+
+ assert!(ddl.contains("\"id\" INTEGER IDENTITY(1,1) NOT NULL"), "ddl: {ddl}");
+ assert!(ddl.contains("PRIMARY KEY (\"id\")"), "ddl: {ddl}");
+ }
+
+ #[test]
+ fn transfer_create_table_preserves_explicit_auto_increment_for_sqlserver_and_oracle() {
+ let cols = vec![db::ColumnInfo {
+ is_primary_key: true,
+ is_nullable: false,
+ extra: Some("auto_increment".to_string()),
+ ..test_column("id", "int")
+ }];
+
+ let sqlserver = generate_create_table_ddl(
+ &cols,
+ "users",
+ "source",
+ "dbo",
+ &DatabaseType::SqlServer,
+ &DatabaseType::Mysql,
+ None,
+ None,
+ );
+ assert!(sqlserver.contains("[id] INT IDENTITY(1,1) NOT NULL"), "ddl: {sqlserver}");
+
+ for target in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
+ let ddl =
+ generate_create_table_ddl(&cols, "users", "source", "SALES", &target, &DatabaseType::Mysql, None, None);
+ assert!(ddl.contains("GENERATED BY DEFAULT AS IDENTITY"), "{target:?}: {ddl}");
+ assert!(!ddl.contains("IDENTITY(1,1)"), "{target:?} must keep the Oracle spelling: {ddl}");
+ }
+ }
+
+ #[test]
+ fn transfer_create_table_does_not_invent_identity_for_plain_integer_primary_keys() {
+ // A source column without an auto-increment flag keeps its plain definition,
+ // and since DM8 allows only one identity column per table, a composite
+ // integer primary key must not collect an implicit clause either.
+ let cols = vec![
+ db::ColumnInfo { is_primary_key: true, is_nullable: false, ..test_column("tenant", "int") },
+ db::ColumnInfo { is_primary_key: true, is_nullable: false, ..test_column("code", "int") },
+ ];
+
+ let ddl = generate_create_table_ddl(
+ &cols,
+ "users",
+ "source",
+ "SYSDBA",
+ &DatabaseType::Dameng,
+ &DatabaseType::Mysql,
+ None,
+ None,
+ );
+
+ assert!(!ddl.contains("IDENTITY"), "ddl: {ddl}");
+ }
+
+ #[test]
+ fn transfer_create_table_emits_at_most_one_identity_clause() {
+ let cols = vec![
+ db::ColumnInfo { is_primary_key: true, is_nullable: false, ..test_column("tenant", "int") },
+ db::ColumnInfo {
+ is_primary_key: true,
+ is_nullable: false,
+ extra: Some("auto_increment".to_string()),
+ ..test_column("id", "int")
+ },
+ ];
+
+ let ddl = generate_create_table_ddl(
+ &cols,
+ "users",
+ "source",
+ "SYSDBA",
+ &DatabaseType::Dameng,
+ &DatabaseType::Mysql,
+ None,
+ None,
+ );
+
+ assert_eq!(ddl.matches("IDENTITY(").count(), 1, "ddl: {ddl}");
+ }
+
+ #[test]
+ fn transfer_create_table_keeps_postgres_and_sqlite_auto_increment_outs() {
+ let cols = vec![db::ColumnInfo {
+ is_primary_key: true,
+ is_nullable: false,
+ extra: Some("auto_increment".to_string()),
+ ..test_column("id", "int")
+ }];
+
+ for target in [DatabaseType::Postgres, DatabaseType::Kingbase] {
+ let ddl = generate_create_table_ddl(
+ &cols,
+ "users",
+ "source",
+ "public",
+ &target,
+ &DatabaseType::Mysql,
+ None,
+ None,
+ );
+ assert!(!ddl.contains("IDENTITY"), "{target:?}: {ddl}");
+ assert!(!ddl.contains("nextval"), "{target:?}: {ddl}");
+ }
+
+ let sqlite =
+ generate_create_table_ddl(&cols, "users", "", "", &DatabaseType::Sqlite, &DatabaseType::Mysql, None, None);
+ assert!(!sqlite.contains("AUTOINCREMENT"), "sqlite: {sqlite}");
+ assert!(!sqlite.contains("AUTO_INCREMENT"), "sqlite: {sqlite}");
+ }
+
+ #[test]
+ fn transfer_create_table_keeps_databend_gbase_and_manticore_auto_increment_outs() {
+ // Databend spells auto-increment `AUTOINCREMENT`/`IDENTITY` and rejects
+ // MySQL's `AUTO_INCREMENT`; Gbase and ManticoreSearch stay on the plain
+ // path until their transfer DDL is verified. Only the transfer's own
+ // MySQL-family targets keep the clause.
+ let cols = vec![db::ColumnInfo {
+ is_primary_key: true,
+ is_nullable: false,
+ extra: Some("auto_increment".to_string()),
+ ..test_column("id", "int")
+ }];
+
+ for target in [DatabaseType::Databend, DatabaseType::Gbase, DatabaseType::ManticoreSearch] {
+ let ddl =
+ generate_create_table_ddl(&cols, "users", "source", "app", &target, &DatabaseType::Mysql, None, None);
+ assert!(!ddl.contains("AUTO_INCREMENT"), "{target:?}: {ddl}");
+ assert!(!ddl.contains("AUTOINCREMENT"), "{target:?}: {ddl}");
+ assert!(!ddl.contains("IDENTITY"), "{target:?}: {ddl}");
+ }
}
#[test]
diff --git a/crates/dbx-sql-dialect/src/sql_dialect/ddl_profile.rs b/crates/dbx-sql-dialect/src/sql_dialect/ddl_profile.rs
index dc2efd051c..1c456e3648 100644
--- a/crates/dbx-sql-dialect/src/sql_dialect/ddl_profile.rs
+++ b/crates/dbx-sql-dialect/src/sql_dialect/ddl_profile.rs
@@ -847,6 +847,11 @@ pub fn profile_for(db_type: DatabaseType) -> DdlDialectProfile {
Dameng => {
let mut profile = oracle_family(db_type);
profile.alter_uses_modify_column = true;
+ // DM8 spells the auto-increment clause `IDENTITY [(seed, increment)]`
+ // (or `AUTO_INCREMENT`), not Oracle's `GENERATED BY DEFAULT AS IDENTITY`.
+ // The Dameng agent and the table-structure editor already read and
+ // write `IDENTITY(1, 1)`.
+ profile.auto_inc = AutoIncSyntax::Suffix(" IDENTITY(1,1)");
profile
}
@@ -945,6 +950,19 @@ mod tests {
assert_eq!(oracle.lock_timeout_sql, Some("ALTER SESSION SET DDL_LOCK_TIMEOUT = 3;"));
}
+ #[test]
+ fn dameng_profile_uses_native_identity_clause() {
+ let dameng = profile_for(DatabaseType::Dameng);
+ // DM8 documents `IDENTITY [(seed, increment)]` (or `AUTO_INCREMENT`) as the
+ // auto-increment clause; Oracle's `GENERATED BY DEFAULT AS IDENTITY` is not
+ // part of that grammar. Dameng otherwise keeps the Oracle-family profile.
+ assert!(matches!(
+ dameng.auto_inc,
+ AutoIncSyntax::Suffix(s) if s.contains("IDENTITY(") && !s.contains("GENERATED")
+ ));
+ assert!(dameng.alter_uses_modify_column);
+ }
+
#[test]
fn quote_styles() {
assert_eq!(profile_for(DatabaseType::Mysql).quote_ident("a`b"), "`a``b`");
diff --git a/crates/dbx-sql-schema/src/schema_diff.rs b/crates/dbx-sql-schema/src/schema_diff.rs
index 5401e9f2e8..ebf69303d8 100644
--- a/crates/dbx-sql-schema/src/schema_diff.rs
+++ b/crates/dbx-sql-schema/src/schema_diff.rs
@@ -13723,6 +13723,40 @@ mod tests {
assert!(sql.contains("CREATE INDEX"), "index: {sql}");
}
+ #[test]
+ fn mysql_to_dameng_create_table_uses_native_identity_clause() {
+ let columns = vec![ColumnDiff {
+ diff_type: "added".into(),
+ name: "id".into(),
+ source: Some(ColumnInfo {
+ name: "id".into(),
+ data_type: "int".into(),
+ is_nullable: false,
+ is_primary_key: true,
+ extra: Some("auto_increment".into()),
+ ..Default::default()
+ }),
+ target: None,
+ changes: vec![],
+ add_position: None,
+ }];
+ let (sql, missing) = generate_create_table_sql(
+ "tb_user",
+ &columns,
+ &[],
+ &[],
+ None,
+ DatabaseType::Dameng,
+ None,
+ Some(DialectKind::Mysql),
+ &[],
+ &[],
+ );
+ assert!(missing.is_empty(), "{missing:?}");
+ assert!(sql.contains("IDENTITY(1,1)"), "Dameng must use its native clause: {sql}");
+ assert!(!sql.contains("GENERATED BY DEFAULT AS IDENTITY"), "Dameng must not use Oracle syntax: {sql}");
+ }
+
fn check_auto_increment(sql: &str, tgt: DialectKind) {
match tgt {
DialectKind::Mysql | DialectKind::ManticoreSearch => {