feat(sqlite): support autoincrement primary key in structure editor

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-13 02:11:17 +08:00
1 parent b8f39f9c24
commit 3767f4a83b
9 files changed
+285 -13

No files matched your search

+133 -2
View File
@@ -5104,7 +5104,14 @@ fn generate_create_table_sql(
continue;
}
AutoIncColumnBuild::AppendSuffix { suffix, skip_default, postgres_sequence } => {
let mut def = format!("{} {}", col_name, mapped_type);
// SQLite accepts AUTOINCREMENT only on an exact INTEGER
// PRIMARY KEY, so integer aliases must be normalized here too.
let effective_type = if db_type == DatabaseType::Sqlite && suffix.contains("AUTOINCREMENT") {
"INTEGER"
} else {
mapped_type.as_str()
};
let mut def = format!("{} {}", col_name, effective_type);
if !col.is_nullable {
def.push_str(" NOT NULL");
}
@@ -5127,6 +5134,12 @@ fn generate_create_table_sql(
}
}
if !suffix.is_empty() {
// SQLite requires AUTOINCREMENT to be part of an inline
// INTEGER PRIMARY KEY declaration; it cannot be combined
// with the table-level PRIMARY KEY clause below.
if db_type == DatabaseType::Sqlite && col.is_primary_key && suffix.contains("AUTOINCREMENT") {
def.push_str(" PRIMARY KEY");
}
def.push_str(suffix);
}
if postgres_sequence {
@@ -5134,7 +5147,7 @@ fn generate_create_table_sql(
auto_col_name = Some(col.name.clone());
}
col_defs.push(def);
if col.is_primary_key {
if col.is_primary_key && !(db_type == DatabaseType::Sqlite && suffix.contains("AUTOINCREMENT")) {
pk_cols.push(quote_id(&col.name, db_type));
}
}
@@ -6563,6 +6576,124 @@ mod tests {
assert!(sql.contains("INT"), "integer→INT: {sql}");
}
#[test]
fn sync_to_sqlite_keeps_plain_integer_pk_without_autoincrement() {
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,
..Default::default()
}),
target: None,
changes: vec![],
add_position: None,
}];
let (sql, missing) = generate_create_table_sql(
"t",
&columns,
&[],
&[],
None,
DatabaseType::Sqlite,
None,
Some(DialectKind::Mysql),
&[],
&[],
);
assert!(missing.is_empty(), "{missing:?}");
assert!(!sql.contains("AUTOINCREMENT"), "plain integer PK must not gain AUTOINCREMENT: {sql}");
assert!(sql.contains("PRIMARY KEY"), "table-level PK must be preserved: {sql}");
}
#[test]
fn sync_to_sqlite_composite_integer_pk_stays_valid() {
let columns = vec![
ColumnDiff {
diff_type: "added".into(),
name: "a".into(),
source: Some(ColumnInfo {
name: "a".into(),
data_type: "int".into(),
is_nullable: false,
is_primary_key: true,
..Default::default()
}),
target: None,
changes: vec![],
add_position: None,
},
ColumnDiff {
diff_type: "added".into(),
name: "b".into(),
source: Some(ColumnInfo {
name: "b".into(),
data_type: "int".into(),
is_nullable: false,
is_primary_key: true,
..Default::default()
}),
target: None,
changes: vec![],
add_position: None,
},
];
let (sql, missing) = generate_create_table_sql(
"t",
&columns,
&[],
&[],
None,
DatabaseType::Sqlite,
None,
Some(DialectKind::Mysql),
&[],
&[],
);
assert!(missing.is_empty(), "{missing:?}");
assert!(!sql.contains("AUTOINCREMENT"), "composite PK must not gain AUTOINCREMENT: {sql}");
assert!(sql.matches("PRIMARY KEY").count() == 1, "single table-level PK expected: {sql}");
}
#[test]
fn sync_to_sqlite_explicit_autoincrement_preserved_and_normalized() {
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,
extra: Some("auto_increment".to_string()),
..Default::default()
}),
target: None,
changes: vec![],
add_position: None,
}];
let (sql, missing) = generate_create_table_sql(
"t",
&columns,
&[],
&[],
None,
DatabaseType::Sqlite,
None,
Some(DialectKind::Mysql),
&[],
&[],
);
assert!(missing.is_empty(), "{missing:?}");
assert!(sql.contains("PRIMARY KEY AUTOINCREMENT"), "explicit auto-increment preserved: {sql}");
assert!(sql.contains("INTEGER"), "bigint must normalize to exact INTEGER: {sql}");
assert!(!sql.contains("BIGINT"), "bigint must normalize to exact INTEGER: {sql}");
assert!(!sql.contains("PRIMARY KEY (\"id\")"), "no duplicate table-level PK: {sql}");
}
// -- 5. MySQL → SQLite type conversion --
#[test]
fn mysql_to_sqlite_type_conversion() {