feat(oracle): support adding primary keys to existing tables
Closes #6922
This commit is contained in:
1 parent
946677a579
commit
a1e94d8228
8 files changed
+252
-23
No files matched your search
+63
-4
@@ -263,7 +263,7 @@ function draft(isPrimaryKey = false, identity?: { seed: number; increment: numbe
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function mountEditor(databaseType: "sqlserver" | "postgres" | "sqlite" | "oracle" | "dameng" | "duckdb" | "informix", isPrimaryKey = false, options: { database?: string; dynamicTypes?: string[]; identity?: { seed: number; increment: number } } = {}) {
|
async function mountEditor(databaseType: "sqlserver" | "postgres" | "sqlite" | "oracle" | "oceanbase-oracle" | "iris" | "dameng" | "duckdb" | "informix", isPrimaryKey = false, options: { database?: string; dynamicTypes?: string[]; identity?: { seed: number; increment: number } } = {}) {
|
||||||
mocks.connection.db_type = databaseType;
|
mocks.connection.db_type = databaseType;
|
||||||
mocks.connection.name = databaseType;
|
mocks.connection.name = databaseType;
|
||||||
mocks.connection.driver_label = databaseType;
|
mocks.connection.driver_label = databaseType;
|
||||||
@@ -315,10 +315,10 @@ async function mountLoadingEditor(initialTab: "columns" | "indexes" | "foreignKe
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
function columnCheckbox(root: HTMLElement, header: string): HTMLInputElement {
|
function columnCheckbox(root: HTMLElement, header: string, rowIndex = 0): HTMLInputElement {
|
||||||
const headerIndex = Array.from(root.querySelectorAll("thead th")).findIndex((cell) => cell.textContent?.trim() === header);
|
const headerIndex = Array.from(root.querySelectorAll("thead th")).findIndex((cell) => cell.textContent?.trim() === header);
|
||||||
if (headerIndex < 0) throw new Error(`Missing ${header} column`);
|
if (headerIndex < 0) throw new Error(`Missing ${header} column`);
|
||||||
const row = root.querySelector<HTMLElement>('[data-column-row-index="0"]');
|
const row = root.querySelector<HTMLElement>(`[data-column-row-index="${rowIndex}"]`);
|
||||||
const cell = row?.querySelectorAll("td")[headerIndex];
|
const cell = row?.querySelectorAll("td")[headerIndex];
|
||||||
const checkbox = cell?.querySelector<HTMLInputElement>('input[type="checkbox"]');
|
const checkbox = cell?.querySelector<HTMLInputElement>('input[type="checkbox"]');
|
||||||
if (!checkbox) throw new Error(`Missing ${header} checkbox`);
|
if (!checkbox) throw new Error(`Missing ${header} checkbox`);
|
||||||
@@ -428,8 +428,67 @@ describe("TableStructureEditor primary key editing", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps the primary-key checkbox disabled for an existing Oracle column", async () => {
|
it("enables a primary key on an existing keyless Oracle column and makes it not null", async () => {
|
||||||
const root = await mountEditor("oracle");
|
const root = await mountEditor("oracle");
|
||||||
|
const primaryKey = columnCheckbox(root, "structureEditor.primaryKey");
|
||||||
|
const nullable = columnCheckbox(root, "structureEditor.nullable");
|
||||||
|
|
||||||
|
expect(primaryKey.disabled).toBe(false);
|
||||||
|
expect(nullable.checked).toBe(true);
|
||||||
|
|
||||||
|
primaryKey.checked = true;
|
||||||
|
primaryKey.dispatchEvent(new Event("change", { bubbles: true }));
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(primaryKey.checked).toBe(true);
|
||||||
|
expect(nullable.checked).toBe(false);
|
||||||
|
await vi.waitFor(() => expect(mocks.buildTableStructureChangeSql).toHaveBeenCalled());
|
||||||
|
expect(mocks.buildTableStructureChangeSql).toHaveBeenLastCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
columns: [expect.objectContaining({ isPrimaryKey: true, isNullable: false })],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps all primary-key checkboxes enabled while composing a new Oracle key", async () => {
|
||||||
|
const root = await mountEditor("oracle");
|
||||||
|
buttonWithText(root, "structureEditor.addColumn").click();
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
const firstPrimaryKey = columnCheckbox(root, "structureEditor.primaryKey", 0);
|
||||||
|
const secondPrimaryKey = columnCheckbox(root, "structureEditor.primaryKey", 1);
|
||||||
|
expect(firstPrimaryKey.disabled).toBe(false);
|
||||||
|
expect(secondPrimaryKey.disabled).toBe(false);
|
||||||
|
|
||||||
|
firstPrimaryKey.checked = true;
|
||||||
|
firstPrimaryKey.dispatchEvent(new Event("change", { bubbles: true }));
|
||||||
|
await nextTick();
|
||||||
|
expect(secondPrimaryKey.disabled).toBe(false);
|
||||||
|
|
||||||
|
secondPrimaryKey.checked = true;
|
||||||
|
secondPrimaryKey.dispatchEvent(new Event("change", { bubbles: true }));
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
await vi.waitFor(() =>
|
||||||
|
expect(mocks.buildTableStructureChangeSql).toHaveBeenLastCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
columns: [expect.objectContaining({ isPrimaryKey: true }), expect.objectContaining({ isPrimaryKey: true })],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps an existing Oracle primary key and all replacement choices disabled", async () => {
|
||||||
|
const root = await mountEditor("oracle", true);
|
||||||
|
buttonWithText(root, "structureEditor.addColumn").click();
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
expect(columnCheckbox(root, "structureEditor.primaryKey", 0).disabled).toBe(true);
|
||||||
|
expect(columnCheckbox(root, "structureEditor.primaryKey", 1).disabled).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(["oceanbase-oracle", "iris"] as const)("keeps primary-key creation disabled for existing %s tables", async (databaseType) => {
|
||||||
|
const root = await mountEditor(databaseType);
|
||||||
|
|
||||||
expect(columnCheckbox(root, "structureEditor.primaryKey").disabled).toBe(true);
|
expect(columnCheckbox(root, "structureEditor.primaryKey").disabled).toBe(true);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2433,8 +2433,9 @@ function isColumnCharsetDisabled(column: EditableStructureColumn): boolean {
|
|||||||
|
|
||||||
function isPrimaryKeyDisabled(column: EditableStructureColumn): boolean {
|
function isPrimaryKeyDisabled(column: EditableStructureColumn): boolean {
|
||||||
if (column.markedForDrop) return true;
|
if (column.markedForDrop) return true;
|
||||||
if (!column.original) return false;
|
if (isCreateMode.value || structureCapabilities.value.alterPrimaryKey) return false;
|
||||||
return !structureCapabilities.value.alterPrimaryKey;
|
if (!structureCapabilities.value.addPrimaryKey) return true;
|
||||||
|
return columns.value.some((candidate) => candidate.original?.is_primary_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
function canDropColumn(column: EditableStructureColumn): boolean {
|
function canDropColumn(column: EditableStructureColumn): boolean {
|
||||||
|
|||||||
@@ -93,10 +93,21 @@ describe("tableStructureCapabilities", () => {
|
|||||||
expect(getTableStructureCapabilities("dameng", "dameng").comment).toBe(true);
|
expect(getTableStructureCapabilities("dameng", "dameng").comment).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("enables alter primary key for Dameng without enabling it for Oracle", () => {
|
it("separates adding a primary key from replacing an existing one", () => {
|
||||||
expect(getTableStructureCapabilities("dameng", "dameng").alterPrimaryKey).toBe(true);
|
expect(getTableStructureCapabilities("dameng", "dameng")).toMatchObject({
|
||||||
expect(getTableStructureCapabilities("oracle", "oracle").alterPrimaryKey).toBe(false);
|
addPrimaryKey: true,
|
||||||
expect(getTableStructureCapabilities("oceanbase-oracle", "oceanbase-oracle").alterPrimaryKey).toBe(false);
|
alterPrimaryKey: true,
|
||||||
|
});
|
||||||
|
expect(getTableStructureCapabilities("oracle", "oracle")).toMatchObject({
|
||||||
|
addPrimaryKey: true,
|
||||||
|
alterPrimaryKey: false,
|
||||||
|
});
|
||||||
|
for (const databaseType of ["oceanbase-oracle", "iris"] as const) {
|
||||||
|
expect(getTableStructureCapabilities(databaseType, databaseType)).toMatchObject({
|
||||||
|
addPrimaryKey: false,
|
||||||
|
alterPrimaryKey: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses local-only column reordering for editable databases without physical reorder support", () => {
|
it("uses local-only column reordering for editable databases without physical reorder support", () => {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export interface TableStructureCapabilities {
|
|||||||
alterType: boolean;
|
alterType: boolean;
|
||||||
alterNullability: boolean;
|
alterNullability: boolean;
|
||||||
alterDefault: boolean;
|
alterDefault: boolean;
|
||||||
|
addPrimaryKey: boolean;
|
||||||
alterPrimaryKey: boolean;
|
alterPrimaryKey: boolean;
|
||||||
reorderColumn: boolean;
|
reorderColumn: boolean;
|
||||||
comment: boolean;
|
comment: boolean;
|
||||||
@@ -40,6 +41,7 @@ const unsupportedCapabilities: TableStructureCapabilities = {
|
|||||||
alterType: false,
|
alterType: false,
|
||||||
alterNullability: false,
|
alterNullability: false,
|
||||||
alterDefault: false,
|
alterDefault: false,
|
||||||
|
addPrimaryKey: false,
|
||||||
alterPrimaryKey: false,
|
alterPrimaryKey: false,
|
||||||
reorderColumn: false,
|
reorderColumn: false,
|
||||||
comment: false,
|
comment: false,
|
||||||
@@ -59,6 +61,9 @@ function capabilities(overrides: Partial<TableStructureCapabilities>): TableStru
|
|||||||
if (overrides.alterStrategy === undefined && resolved.alterExistingColumn) {
|
if (overrides.alterStrategy === undefined && resolved.alterExistingColumn) {
|
||||||
resolved.alterStrategy = "direct";
|
resolved.alterStrategy = "direct";
|
||||||
}
|
}
|
||||||
|
if (resolved.alterPrimaryKey) {
|
||||||
|
resolved.addPrimaryKey = true;
|
||||||
|
}
|
||||||
return resolved;
|
return resolved;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,6 +134,7 @@ const redshiftCapabilities = capabilities({
|
|||||||
indexInclude: false,
|
indexInclude: false,
|
||||||
indexFilter: false,
|
indexFilter: false,
|
||||||
indexComment: false,
|
indexComment: false,
|
||||||
|
addPrimaryKey: false,
|
||||||
alterPrimaryKey: false,
|
alterPrimaryKey: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -182,7 +188,7 @@ const sqlserverCapabilities = capabilities({
|
|||||||
indexComment: true,
|
indexComment: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const oracleCapabilities = capabilities({
|
const oracleCompatibleCapabilities = capabilities({
|
||||||
dialect: "oracle",
|
dialect: "oracle",
|
||||||
createTable: true,
|
createTable: true,
|
||||||
addColumn: true,
|
addColumn: true,
|
||||||
@@ -199,15 +205,20 @@ const oracleCapabilities = capabilities({
|
|||||||
indexType: true,
|
indexType: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const oracleCapabilities = capabilities({
|
||||||
|
...oracleCompatibleCapabilities,
|
||||||
|
addPrimaryKey: true,
|
||||||
|
});
|
||||||
|
|
||||||
// Dameng (DM8): ALTER TABLE ... DROP PRIMARY KEY / ADD PRIMARY KEY is official DDL.
|
// Dameng (DM8): ALTER TABLE ... DROP PRIMARY KEY / ADD PRIMARY KEY is official DDL.
|
||||||
// Keep separate from oracleCapabilities so UI cannot enable PK edit without BE drop SQL.
|
// Keep separate from Oracle-compatible engines so UI cannot enable PK edits without verified DDL.
|
||||||
const damengCapabilities = capabilities({
|
const damengCapabilities = capabilities({
|
||||||
...oracleCapabilities,
|
...oracleCompatibleCapabilities,
|
||||||
alterPrimaryKey: true,
|
alterPrimaryKey: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const irisCapabilities = capabilities({
|
const irisCapabilities = capabilities({
|
||||||
...oracleCapabilities,
|
...oracleCompatibleCapabilities,
|
||||||
// IRIS exposes %DESCRIPTION at definition time but cannot alter persisted descriptions.
|
// IRIS exposes %DESCRIPTION at definition time but cannot alter persisted descriptions.
|
||||||
comment: false,
|
comment: false,
|
||||||
});
|
});
|
||||||
@@ -340,10 +351,10 @@ const capabilityByType: Partial<Record<DatabaseType, TableStructureCapabilities>
|
|||||||
sqlserver: sqlserverCapabilities,
|
sqlserver: sqlserverCapabilities,
|
||||||
oracle: oracleCapabilities,
|
oracle: oracleCapabilities,
|
||||||
dameng: damengCapabilities,
|
dameng: damengCapabilities,
|
||||||
"oceanbase-oracle": oracleCapabilities,
|
"oceanbase-oracle": oracleCompatibleCapabilities,
|
||||||
iris: irisCapabilities,
|
iris: irisCapabilities,
|
||||||
yashandb: oracleCapabilities,
|
yashandb: oracleCompatibleCapabilities,
|
||||||
xugu: oracleCapabilities,
|
xugu: oracleCompatibleCapabilities,
|
||||||
h2: h2Capabilities,
|
h2: h2Capabilities,
|
||||||
access: accessCapabilities,
|
access: accessCapabilities,
|
||||||
clickhouse: clickhouseCapabilities,
|
clickhouse: clickhouseCapabilities,
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ pub use types::*;
|
|||||||
|
|
||||||
use crate::models::connection::DatabaseType;
|
use crate::models::connection::DatabaseType;
|
||||||
|
|
||||||
use columns::build_column_sql;
|
use columns::{build_column_sql, validate_primary_key_change_scope};
|
||||||
use comments::build_table_comment_sql;
|
use comments::build_table_comment_sql;
|
||||||
use foreign_keys::build_foreign_key_sql;
|
use foreign_keys::build_foreign_key_sql;
|
||||||
use indexes::build_index_sql;
|
use indexes::build_index_sql;
|
||||||
@@ -35,6 +35,10 @@ pub fn build_table_structure_change_sql(mut options: TableStructureSqlOptions) -
|
|||||||
if options.is_gaussdb_m_mode {
|
if options.is_gaussdb_m_mode {
|
||||||
options.database_type = Some(DatabaseType::Mysql);
|
options.database_type = Some(DatabaseType::Mysql);
|
||||||
}
|
}
|
||||||
|
let primary_key_errors = validate_primary_key_change_scope(&options);
|
||||||
|
if !primary_key_errors.is_empty() {
|
||||||
|
return TableStructureSqlResult { statements: Vec::new(), warnings: primary_key_errors };
|
||||||
|
}
|
||||||
// Fail closed: an unsupported concurrent-index request (existing index, or
|
// Fail closed: an unsupported concurrent-index request (existing index, or
|
||||||
// partitioned parent table) must never degrade into blocking index DDL
|
// partitioned parent table) must never degrade into blocking index DDL
|
||||||
// behind the caller's back, so the whole plan is refused up front.
|
// behind the caller's back, so the whole plan is refused up front.
|
||||||
|
|||||||
@@ -346,6 +346,32 @@ fn primary_key_change(options: &TableStructureSqlOptions) -> Option<PrimaryKeyCh
|
|||||||
Some(PrimaryKeyChange { old_ids, new_ids, new_names })
|
Some(PrimaryKeyChange { old_ids, new_ids, new_names })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn unsupported_primary_key_change_warning(options: &TableStructureSqlOptions, change: &PrimaryKeyChange<'_>) -> String {
|
||||||
|
let action = if change.old_ids.is_empty() { "Adding" } else { "Changing" };
|
||||||
|
format!("{action} primary keys is not supported for {} from this editor.", database_label(options.database_type))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn validate_primary_key_change_scope(options: &TableStructureSqlOptions) -> Vec<String> {
|
||||||
|
if let Some(original) = options
|
||||||
|
.columns
|
||||||
|
.iter()
|
||||||
|
.find(|column| column.marked_for_drop && was_primary_key_column(column))
|
||||||
|
.and_then(|column| column.original.as_ref())
|
||||||
|
{
|
||||||
|
return vec![format!("Primary key column \"{}\" cannot be dropped from this editor.", original.name)];
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(change) = primary_key_change(options) else { return Vec::new() };
|
||||||
|
let capabilities = capabilities_for(options.database_type);
|
||||||
|
let supported =
|
||||||
|
if change.old_ids.is_empty() { capabilities.add_primary_key } else { capabilities.alter_primary_key };
|
||||||
|
if supported {
|
||||||
|
Vec::new()
|
||||||
|
} else {
|
||||||
|
vec![unsupported_primary_key_change_warning(options, &change)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn mysql_auto_increment_touches_primary_key(column: &EditableStructureColumn, change: &PrimaryKeyChange<'_>) -> bool {
|
fn mysql_auto_increment_touches_primary_key(column: &EditableStructureColumn, change: &PrimaryKeyChange<'_>) -> bool {
|
||||||
column.extra.as_ref().is_some_and(|extra| extra.auto_increment.unwrap_or(false))
|
column.extra.as_ref().is_some_and(|extra| extra.auto_increment.unwrap_or(false))
|
||||||
&& (change.old_ids.contains(column.id.as_str()) || change.new_ids.contains(column.id.as_str()))
|
&& (change.old_ids.contains(column.id.as_str()) || change.new_ids.contains(column.id.as_str()))
|
||||||
@@ -363,11 +389,10 @@ pub(super) fn build_primary_key_sql(
|
|||||||
// columns is not a PK change. A draft that drops a PK column is also rejected here.
|
// columns is not a PK change. A draft that drops a PK column is also rejected here.
|
||||||
let Some(change) = primary_key_change(options) else { return Vec::new() };
|
let Some(change) = primary_key_change(options) else { return Vec::new() };
|
||||||
|
|
||||||
if !capabilities.alter_primary_key {
|
let supported =
|
||||||
warnings.push(format!(
|
if change.old_ids.is_empty() { capabilities.add_primary_key } else { capabilities.alter_primary_key };
|
||||||
"Changing primary keys is not supported for {} from this editor.",
|
if !supported {
|
||||||
database_label(options.database_type)
|
warnings.push(unsupported_primary_key_change_warning(options, &change));
|
||||||
));
|
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ pub(super) struct TableStructureCapabilities {
|
|||||||
pub(super) index_filter: bool,
|
pub(super) index_filter: bool,
|
||||||
pub(super) index_comment: bool,
|
pub(super) index_comment: bool,
|
||||||
pub(super) index_concurrent: bool,
|
pub(super) index_concurrent: bool,
|
||||||
|
pub(super) add_primary_key: bool,
|
||||||
pub(super) alter_primary_key: bool,
|
pub(super) alter_primary_key: bool,
|
||||||
pub(super) foreign_key: bool,
|
pub(super) foreign_key: bool,
|
||||||
}
|
}
|
||||||
@@ -65,6 +66,7 @@ impl Default for TableStructureCapabilities {
|
|||||||
index_filter: false,
|
index_filter: false,
|
||||||
index_comment: false,
|
index_comment: false,
|
||||||
index_concurrent: false,
|
index_concurrent: false,
|
||||||
|
add_primary_key: false,
|
||||||
alter_primary_key: false,
|
alter_primary_key: false,
|
||||||
foreign_key: false,
|
foreign_key: false,
|
||||||
}
|
}
|
||||||
@@ -85,6 +87,7 @@ pub(super) fn gaussdb_m_capabilities() -> TableStructureCapabilities {
|
|||||||
rebuild_index: true,
|
rebuild_index: true,
|
||||||
index_type: true,
|
index_type: true,
|
||||||
index_comment: true,
|
index_comment: true,
|
||||||
|
add_primary_key: true,
|
||||||
alter_primary_key: true,
|
alter_primary_key: true,
|
||||||
..TableStructureCapabilities::default()
|
..TableStructureCapabilities::default()
|
||||||
}
|
}
|
||||||
@@ -112,6 +115,7 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
|
|||||||
rebuild_index: true,
|
rebuild_index: true,
|
||||||
index_type: true,
|
index_type: true,
|
||||||
index_comment: true,
|
index_comment: true,
|
||||||
|
add_primary_key: true,
|
||||||
alter_primary_key: true,
|
alter_primary_key: true,
|
||||||
foreign_key: true,
|
foreign_key: true,
|
||||||
..base
|
..base
|
||||||
@@ -158,6 +162,7 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
|
|||||||
index_include: true,
|
index_include: true,
|
||||||
index_filter: true,
|
index_filter: true,
|
||||||
index_comment: true,
|
index_comment: true,
|
||||||
|
add_primary_key: true,
|
||||||
alter_primary_key: true,
|
alter_primary_key: true,
|
||||||
foreign_key: true,
|
foreign_key: true,
|
||||||
..base
|
..base
|
||||||
@@ -188,6 +193,7 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
|
|||||||
index_filter: false,
|
index_filter: false,
|
||||||
index_comment: false,
|
index_comment: false,
|
||||||
index_concurrent: false,
|
index_concurrent: false,
|
||||||
|
add_primary_key: false,
|
||||||
alter_primary_key: false,
|
alter_primary_key: false,
|
||||||
foreign_key: false,
|
foreign_key: false,
|
||||||
},
|
},
|
||||||
@@ -249,6 +255,7 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
|
|||||||
drop_index: true,
|
drop_index: true,
|
||||||
rebuild_index: true,
|
rebuild_index: true,
|
||||||
index_type: true,
|
index_type: true,
|
||||||
|
add_primary_key: true,
|
||||||
alter_primary_key: true,
|
alter_primary_key: true,
|
||||||
..base
|
..base
|
||||||
},
|
},
|
||||||
@@ -265,6 +272,7 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
|
|||||||
drop_index: true,
|
drop_index: true,
|
||||||
rebuild_index: true,
|
rebuild_index: true,
|
||||||
index_type: true,
|
index_type: true,
|
||||||
|
add_primary_key: true,
|
||||||
alter_primary_key: true,
|
alter_primary_key: true,
|
||||||
..base
|
..base
|
||||||
},
|
},
|
||||||
@@ -293,6 +301,7 @@ pub(super) fn capabilities_for(database_type: Option<DatabaseType>) -> TableStru
|
|||||||
drop_index: true,
|
drop_index: true,
|
||||||
rebuild_index: true,
|
rebuild_index: true,
|
||||||
index_type: true,
|
index_type: true,
|
||||||
|
add_primary_key: true,
|
||||||
foreign_key: true,
|
foreign_key: true,
|
||||||
..base
|
..base
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3522,6 +3522,115 @@ fn dameng_blocks_dropping_former_primary_key_column() {
|
|||||||
assert!(result.warnings.iter().any(|warning| warning.contains("Primary key column")));
|
assert!(result.warnings.iter().any(|warning| warning.contains("Primary key column")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oracle_sets_not_null_before_adding_primary_key() {
|
||||||
|
let mut id = existing_pk_column("id", "NUMBER", false, true);
|
||||||
|
id.original.as_mut().unwrap().is_nullable = true;
|
||||||
|
|
||||||
|
let result =
|
||||||
|
build_table_structure_change_sql(structure_change_options(DatabaseType::Oracle, Some("HR"), "users", vec![id]));
|
||||||
|
|
||||||
|
assert_eq!(result.warnings, Vec::<String>::new());
|
||||||
|
assert_eq!(
|
||||||
|
result.statements,
|
||||||
|
vec![
|
||||||
|
"ALTER TABLE \"HR\".\"users\" MODIFY (\"id\" NUMBER NOT NULL);",
|
||||||
|
"ALTER TABLE \"HR\".\"users\" ADD PRIMARY KEY (\"id\");",
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oracle_adds_composite_primary_key_in_draft_order() {
|
||||||
|
let mut tenant_id = existing_pk_column("tenant_id", "NUMBER", false, true);
|
||||||
|
tenant_id.id = "tenant_id".to_string();
|
||||||
|
let mut code = existing_pk_column("code", "VARCHAR2(50)", false, true);
|
||||||
|
code.id = "code".to_string();
|
||||||
|
|
||||||
|
let result = build_table_structure_change_sql(structure_change_options(
|
||||||
|
DatabaseType::Oracle,
|
||||||
|
Some("HR"),
|
||||||
|
"users",
|
||||||
|
vec![code, tenant_id],
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(result.warnings, Vec::<String>::new());
|
||||||
|
assert_eq!(result.statements, vec!["ALTER TABLE \"HR\".\"users\" ADD PRIMARY KEY (\"code\", \"tenant_id\");"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oracle_adds_new_primary_key_column_before_adding_constraint() {
|
||||||
|
let mut code = column("code");
|
||||||
|
code.data_type = "VARCHAR2(50)".to_string();
|
||||||
|
code.is_nullable = false;
|
||||||
|
code.is_primary_key = true;
|
||||||
|
|
||||||
|
let result = build_table_structure_change_sql(structure_change_options(
|
||||||
|
DatabaseType::Oracle,
|
||||||
|
Some("HR"),
|
||||||
|
"users",
|
||||||
|
vec![code],
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(result.warnings, Vec::<String>::new());
|
||||||
|
assert_eq!(
|
||||||
|
result.statements,
|
||||||
|
vec![
|
||||||
|
"ALTER TABLE \"HR\".\"users\" ADD (\"code\" VARCHAR2(50));",
|
||||||
|
"ALTER TABLE \"HR\".\"users\" ADD PRIMARY KEY (\"code\");",
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oracle_rejects_existing_primary_key_changes_without_partial_sql() {
|
||||||
|
let uncheck =
|
||||||
|
vec![existing_pk_column("id", "NUMBER", true, false), existing_pk_column("name", "VARCHAR2(50)", false, false)];
|
||||||
|
let replacement = vec![
|
||||||
|
existing_pk_column("id", "NUMBER", true, false),
|
||||||
|
existing_pk_column("code", "VARCHAR2(50)", false, true),
|
||||||
|
existing_pk_column("name", "VARCHAR2(50)", false, false),
|
||||||
|
];
|
||||||
|
let second_key = vec![
|
||||||
|
existing_pk_column("id", "NUMBER", true, true),
|
||||||
|
existing_pk_column("code", "VARCHAR2(50)", false, true),
|
||||||
|
existing_pk_column("name", "VARCHAR2(50)", false, false),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (case, columns) in [("uncheck", uncheck), ("replacement", replacement), ("second key", second_key)] {
|
||||||
|
let mut options = structure_change_options(DatabaseType::Oracle, Some("HR"), "users", columns);
|
||||||
|
options.indexes = vec![index("idx_users_name", &["name"])];
|
||||||
|
|
||||||
|
let result = build_table_structure_change_sql(options);
|
||||||
|
|
||||||
|
assert!(result.statements.is_empty(), "{case} must not emit partial SQL: {:?}", result.statements);
|
||||||
|
assert_eq!(result.warnings.len(), 1, "unexpected {case} warnings: {:?}", result.warnings);
|
||||||
|
assert!(
|
||||||
|
result.warnings[0].contains("Changing primary keys"),
|
||||||
|
"unexpected {case} warning: {:?}",
|
||||||
|
result.warnings
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oracle_compatible_engines_do_not_inherit_oracle_primary_key_add() {
|
||||||
|
for database_type in [DatabaseType::OceanbaseOracle, DatabaseType::Iris] {
|
||||||
|
let columns = vec![
|
||||||
|
existing_pk_column("id", "NUMBER", false, true),
|
||||||
|
existing_pk_column("name", "VARCHAR2(50)", false, false),
|
||||||
|
];
|
||||||
|
let mut options = structure_change_options(database_type, Some("APP"), "users", columns);
|
||||||
|
options.indexes = vec![index("idx_users_name", &["name"])];
|
||||||
|
|
||||||
|
let result = build_table_structure_change_sql(options);
|
||||||
|
|
||||||
|
assert!(result.statements.is_empty(), "{database_type:?} must not emit partial SQL: {:?}", result.statements);
|
||||||
|
assert_eq!(result.warnings.len(), 1, "unexpected {database_type:?} warnings: {:?}", result.warnings);
|
||||||
|
assert!(result.warnings[0].contains("Adding primary keys"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn oracle_uncheck_primary_key_and_drop_column_does_not_emit_drop_column() {
|
fn oracle_uncheck_primary_key_and_drop_column_does_not_emit_drop_column() {
|
||||||
// alter_primary_key is false for Oracle: unchecking PK must not unlock DROP COLUMN without a PK drop.
|
// alter_primary_key is false for Oracle: unchecking PK must not unlock DROP COLUMN without a PK drop.
|
||||||
|
|||||||
Reference in new issue
Block a user