fix(sqlserver): include IDENTITY in table DDL
This commit is contained in:
1 file changed
+32
@@ -5409,6 +5409,18 @@ mod ddl_tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_table_ddl_includes_identity_clause() {
|
||||
let mut id = column("FIDS", "int");
|
||||
id.is_nullable = false;
|
||||
id.is_primary_key = true;
|
||||
id.extra = Some("identity(1,1)".to_string());
|
||||
|
||||
let ddl = render_sqlserver_table_ddl("dbo", "ZHLSBS", &[id], &[], &[]);
|
||||
|
||||
assert!(ddl.contains("[FIDS] int IDENTITY(1,1) NOT NULL"), "ddl: {ddl}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opengauss_table_ddl_uses_native_tabledef_function() {
|
||||
assert_eq!(
|
||||
@@ -5598,6 +5610,23 @@ pub fn render_postgres_table_ddl(
|
||||
ddl
|
||||
}
|
||||
|
||||
fn sqlserver_identity_clause(extra: Option<&str>) -> Option<String> {
|
||||
let extra = extra?.trim();
|
||||
let lower = extra.to_ascii_lowercase();
|
||||
if !lower.starts_with("identity") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let rest = extra["identity".len()..].trim_start();
|
||||
if rest.is_empty() {
|
||||
return Some("IDENTITY".to_string());
|
||||
}
|
||||
|
||||
let args = rest.strip_prefix('(')?;
|
||||
let end = args.find(')')?;
|
||||
Some(format!("IDENTITY({})", args[..end].trim()))
|
||||
}
|
||||
|
||||
fn group_foreign_keys_by_name(fkeys: &[db::ForeignKeyInfo]) -> Vec<Vec<&db::ForeignKeyInfo>> {
|
||||
let mut groups: Vec<Vec<&db::ForeignKeyInfo>> = Vec::new();
|
||||
for fk in fkeys {
|
||||
@@ -5635,6 +5664,9 @@ pub fn render_sqlserver_table_ddl(
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let mut line = format!(" {} {}", sqlserver_ident(&c.name), c.data_type);
|
||||
if let Some(identity) = sqlserver_identity_clause(c.extra.as_deref()) {
|
||||
line.push_str(&format!(" {identity}"));
|
||||
}
|
||||
if !c.is_nullable {
|
||||
line.push_str(" NOT NULL");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user