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(oracle): keep NOT NULL when adding a column

Jeff849d831ae25e
diff --git a/crates/dbx-sql-schema/src/table_structure_sql/column_format.rs b/crates/dbx-sql-schema/src/table_structure_sql/column_format.rs
index 014f07f5d6..984ecf74d0 100644
--- a/crates/dbx-sql-schema/src/table_structure_sql/column_format.rs
+++ b/crates/dbx-sql-schema/src/table_structure_sql/column_format.rs
@@ -76,6 +76,13 @@ pub(super) fn column_definition(dialect: StructureDialect, column: &EditableStru
if mysql_generated_clause.is_none() && !default_value.is_empty() {
parts.push(format!("DEFAULT {}", format_default_for_sql(dialect, &column.data_type, &default_value)));
}
+ // Oracle's column grammar is `col type [DEFAULT expr] [NOT NULL]`, so NOT NULL is skipped
+ // above and appended here, after DEFAULT, matching `build_create_table_sql` (t8y2/dbx#9477).
+ // Without it `ADD (...)` silently created a nullable column (t8y2/dbx#11234). A primary key
+ // column stays without it because the follow-up `ADD PRIMARY KEY` already enforces NOT NULL.
+ if dialect == StructureDialect::Oracle && !column.is_nullable && !column.is_primary_key {
+ parts.push("NOT NULL".to_string());
+ }
if mysql_generated_clause.is_none() {
if let Some(on_update) = column.extra.as_ref().and_then(|e| e.on_update_current_timestamp).filter(|v| *v) {
if on_update && dialect == StructureDialect::Mysql {
diff --git a/crates/dbx-sql-schema/src/table_structure_sql/tests.rs b/crates/dbx-sql-schema/src/table_structure_sql/tests.rs
index 8bea751776..994d2252c5 100644
--- a/crates/dbx-sql-schema/src/table_structure_sql/tests.rs
+++ b/crates/dbx-sql-schema/src/table_structure_sql/tests.rs
@@ -1436,6 +1436,40 @@ fn oracle_create_table_places_default_before_not_null() {
assert!(!result.statements[0].contains("NOT NULL DEFAULT"), "ddl: {}", result.statements[0]);
}
+#[test]
+fn oracle_add_column_keeps_not_null_after_default() {
+ // `ADD (...)` used to drop NOT NULL entirely for Oracle, so a column the user marked
+ // not nullable was created nullable without any error (t8y2/dbx#11234).
+ let cases = [
+ (
+ "QTY",
+ "NUMBER(18,2)",
+ false,
+ "0",
+ "ALTER TABLE \"HR\".\"orders\" ADD (\"QTY\" NUMBER(18,2) DEFAULT 0 NOT NULL);",
+ ),
+ ("CODE", "VARCHAR2(20)", false, "", "ALTER TABLE \"HR\".\"orders\" ADD (\"CODE\" VARCHAR2(20) NOT NULL);"),
+ ("NOTE", "VARCHAR2(20)", true, "", "ALTER TABLE \"HR\".\"orders\" ADD (\"NOTE\" VARCHAR2(20));"),
+ ];
+
+ for (name, data_type, is_nullable, default_value, expected) in cases {
+ let mut col = column(name);
+ col.data_type = data_type.to_string();
+ col.is_nullable = is_nullable;
+ col.default_value = default_value.to_string();
+
+ let result = build_table_structure_change_sql(structure_change_options(
+ DatabaseType::Oracle,
+ Some("HR"),
+ "orders",
+ vec![col],
+ ));
+
+ assert_eq!(result.warnings, Vec::<String>::new(), "{name}");
+ assert_eq!(result.statements, vec![expected], "{name}");
+ }
+}
+
#[test]
fn oracle_create_table_preserves_character_length_units() {
let mut byte_col = column("BYTE_COL");