fix: can't adding new columns as primary key (#2310)

This commit is contained in:
dennis zhuang
2023-09-03 22:47:22 -05:00
committed by Ruihang Xia
parent 38697e0c4d
commit b1599ad3a5
4 changed files with 86 additions and 12 deletions

View File

@@ -270,6 +270,17 @@ fn parse_column_default_constraint(
}
}
/// Return true when the `ColumnDef` options contain primary key
pub fn has_primary_key_option(column_def: &ColumnDef) -> bool {
column_def
.options
.iter()
.any(|options| match options.option {
ColumnOption::Unique { is_primary } => is_primary,
_ => false,
})
}
// TODO(yingwen): Make column nullable by default, and checks invalid case like
// a column is not nullable but has a default value null.
/// Create a `ColumnSchema` from `ColumnDef`.
@@ -748,6 +759,28 @@ mod tests {
assert!(!grpc_column_def.is_nullable);
}
#[test]
pub fn test_has_primary_key_option() {
let column_def = ColumnDef {
name: "col".into(),
data_type: SqlDataType::Double,
collation: None,
options: vec![],
};
assert!(!has_primary_key_option(&column_def));
let column_def = ColumnDef {
name: "col".into(),
data_type: SqlDataType::Double,
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Unique { is_primary: true },
}],
};
assert!(has_primary_key_option(&column_def));
}
#[test]
pub fn test_column_def_to_schema() {
let column_def = ColumnDef {