Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(schema-editor): update default fields (#3945)

boojackc0febda10982
diff --git a/api/database.go b/api/database.go
index 64c1e800d6..5e6d1f73ae 100644
--- a/api/database.go
+++ b/api/database.go
@@ -172,27 +172,25 @@ type DropTableContext struct {
// AddColumnContext is the create/alter table context to add a column.
type AddColumnContext struct {
- Name string `json:"name"`
- Type string `json:"type"`
- CharacterSet string `json:"characterSet"`
- Collation string `json:"collation"`
- Comment string `json:"comment"`
- Nullable bool `json:"nullable"`
- HasDefault bool `json:"hasDefault"`
- Default string `json:"default"`
+ Name string `json:"name"`
+ Type string `json:"type"`
+ CharacterSet string `json:"characterSet"`
+ Collation string `json:"collation"`
+ Comment string `json:"comment"`
+ Nullable bool `json:"nullable"`
+ Default *string `json:"default"`
}
// ChangeColumnContext is the alter table context to change a column.
type ChangeColumnContext struct {
- OldName string `json:"oldName"`
- NewName string `json:"newName"`
- Type string `json:"type"`
- CharacterSet string `json:"characterSet"`
- Collation string `json:"collation"`
- Comment string `json:"comment"`
- Nullable bool `json:"nullable"`
- HasDefault bool `json:"hasDefault"`
- Default string `json:"default"`
+ OldName string `json:"oldName"`
+ NewName string `json:"newName"`
+ Type string `json:"type"`
+ CharacterSet string `json:"characterSet"`
+ Collation string `json:"collation"`
+ Comment string `json:"comment"`
+ Nullable bool `json:"nullable"`
+ Default *string `json:"default"`
}
// DropColumnContext is the alter table context to drop a column.
diff --git a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
index 21240bc6de..31b0a2f3f8 100644
--- a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
@@ -116,7 +116,7 @@
<input
v-model="column.default"
:disabled="disableAlterColumn(column)"
- :placeholder="column.hasDefault === false ? 'NULL' : ''"
+ :placeholder="column.default === undefined ? 'NULL' : ''"
class="column-field-input !pr-8"
type="text"
/>
@@ -390,8 +390,7 @@ const handleColumnDefaultFieldChange = (
defaultString: string
) => {
if (defaultString === "NULL") {
- column.hasDefault = false;
- column.default = "";
+ column.default = undefined;
}
};
diff --git a/frontend/src/types/schemaEditor/atomType.ts b/frontend/src/types/schemaEditor/atomType.ts
index a0651182cb..9c1607c90d 100644
--- a/frontend/src/types/schemaEditor/atomType.ts
+++ b/frontend/src/types/schemaEditor/atomType.ts
@@ -6,8 +6,7 @@ export interface Column {
type: string;
nullable: boolean;
comment: string;
- hasDefault: boolean;
- default: string;
+ default?: string;
status: TableOrColumnStatus;
}
diff --git a/frontend/src/types/schemaEditor/index.ts b/frontend/src/types/schemaEditor/index.ts
index 58186dc1f5..3802f61b84 100644
--- a/frontend/src/types/schemaEditor/index.ts
+++ b/frontend/src/types/schemaEditor/index.ts
@@ -85,8 +85,7 @@ export interface AddColumnContext {
collation: string;
comment: string;
nullable: boolean;
- hasDefault: boolean;
- default: string;
+ default?: string;
}
export interface ChangeColumnContext {
@@ -97,8 +96,7 @@ export interface ChangeColumnContext {
collation: string;
comment: string;
nullable: boolean;
- hasDefault: boolean;
- default: string;
+ default?: string;
}
export interface DropColumnContext {
diff --git a/frontend/src/utils/schemaEditor/diffColumn.test.ts b/frontend/src/utils/schemaEditor/diffColumn.test.ts
index c72b8ef442..6cb86878d2 100644
--- a/frontend/src/utils/schemaEditor/diffColumn.test.ts
+++ b/frontend/src/utils/schemaEditor/diffColumn.test.ts
@@ -26,8 +26,6 @@ it("diff add column list", () => {
type: "int",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
status: "created",
} as any as Column,
],
@@ -38,10 +36,9 @@ it("diff add column list", () => {
type: "int",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
characterSet: "",
collation: "",
+ default: undefined,
},
],
changeColumnList: [],
@@ -74,8 +71,6 @@ it("diff modify column list", () => {
type: "int",
comment: "",
nullable: true,
- hasDefault: false,
- default: "",
status: "normal",
} as any as Column,
],
@@ -86,8 +81,6 @@ it("diff modify column list", () => {
type: "varchar",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
status: "normal",
} as any as Column,
],
@@ -100,10 +93,9 @@ it("diff modify column list", () => {
type: "varchar",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
characterSet: "",
collation: "",
+ default: undefined,
},
],
dropColumnList: [],
@@ -135,8 +127,6 @@ it("diff drop column list", () => {
type: "int",
comment: "",
nullable: true,
- hasDefault: false,
- default: "",
} as any as Column,
],
columnList: [
@@ -146,8 +136,6 @@ it("diff drop column list", () => {
type: "int",
comment: "",
nullable: true,
- hasDefault: false,
- default: "",
status: "dropped",
} as any as Column,
],
@@ -187,8 +175,6 @@ it("diff column list", () => {
type: "int",
comment: "",
nullable: true,
- hasDefault: false,
- default: "",
status: "normal",
} as any as Column,
{
@@ -197,8 +183,6 @@ it("diff column list", () => {
type: "varchar",
comment: "",
nullable: true,
- hasDefault: false,
- default: "",
status: "normal",
} as any as Column,
{
@@ -207,8 +191,6 @@ it("diff column list", () => {
type: "varchar",
comment: "",
nullable: true,
- hasDefault: false,
- default: "",
status: "normal",
} as any as Column,
],
@@ -228,7 +210,6 @@ it("diff column list", () => {
type: "varchar",
comment: "",
nullable: false,
- hasDefault: true,
default: "",
status: "normal",
} as Column,
@@ -238,8 +219,6 @@ it("diff column list", () => {
type: "varchar",
comment: "",
nullable: true,
- hasDefault: false,
- default: "",
status: "dropped",
} as any as Column,
{
@@ -248,7 +227,6 @@ it("diff column list", () => {
type: "varchar",
comment: "",
nullable: false,
- hasDefault: true,
default: "",
status: "created",
} as Column,
@@ -262,7 +240,6 @@ it("diff column list", () => {
collation: "",
comment: "",
nullable: false,
- hasDefault: true,
default: "",
},
],
@@ -275,8 +252,7 @@ it("diff column list", () => {
collation: "",
comment: "this is id",
nullable: true,
- hasDefault: false,
- default: "",
+ default: undefined,
},
{
oldName: "name",
@@ -286,7 +262,6 @@ it("diff column list", () => {
collation: "",
comment: "",
nullable: false,
- hasDefault: true,
default: "",
},
],
diff --git a/frontend/src/utils/schemaEditor/diffTable.test.ts b/frontend/src/utils/schemaEditor/diffTable.test.ts
index 52942aebcc..809240cd17 100644
--- a/frontend/src/utils/schemaEditor/diffTable.test.ts
+++ b/frontend/src/utils/schemaEditor/diffTable.test.ts
@@ -38,8 +38,6 @@ it("diff create table list", () => {
type: "int",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
status: "created",
},
],
@@ -62,8 +60,7 @@ it("diff create table list", () => {
collation: "",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
+ default: undefined,
},
],
},
@@ -109,8 +106,6 @@ it("diff alter table list", () => {
collation: "",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
status: "normal",
},
],
@@ -133,8 +128,6 @@ it("diff alter table list", () => {
collation: "",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
status: "normal",
},
{
@@ -145,8 +138,6 @@ it("diff alter table list", () => {
collation: "",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
status: "created",
},
],
@@ -165,9 +156,8 @@ it("diff alter table list", () => {
characterSet: "",
collation: "",
comment: "",
+ default: undefined,
nullable: false,
- hasDefault: false,
- default: "",
},
],
changeColumnList: [],
@@ -214,8 +204,6 @@ it("diff drop table list", () => {
collation: "",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
},
],
status: "normal",
@@ -237,8 +225,6 @@ it("diff drop table list", () => {
collation: "",
comment: "",
nullable: false,
- hasDefault: false,
- default: "",
},
],
status: "dropped",
diff --git a/frontend/src/utils/schemaEditor/transform.ts b/frontend/src/utils/schemaEditor/transform.ts
index 4f009c12b1..be920a11b4 100644
--- a/frontend/src/utils/schemaEditor/transform.ts
+++ b/frontend/src/utils/schemaEditor/transform.ts
@@ -37,8 +37,7 @@ export const transformColumnDataToColumn = (
type: columnMetadata.type,
nullable: columnMetadata.nullable,
comment: columnMetadata.comment,
- hasDefault: columnMetadata.default !== undefined,
- default: columnMetadata.default === undefined ? "" : columnMetadata.default,
+ default: columnMetadata.default,
status: "normal",
};
};
@@ -51,8 +50,7 @@ export const transformColumnToAddColumnContext = (
type: defaultTo(column.type, ""),
comment: defaultTo(column.comment, ""),
nullable: defaultTo(column.nullable, false),
- hasDefault: defaultTo(column.hasDefault, false),
- default: defaultTo(column.default, ""),
+ default: defaultTo(column.default, undefined),
characterSet: "",
collation: "",
};
@@ -68,8 +66,7 @@ export const transformColumnToChangeColumnContext = (
type: defaultTo(column.type, ""),
comment: defaultTo(column.comment, ""),
nullable: defaultTo(column.nullable, false),
- hasDefault: defaultTo(column.hasDefault, false),
- default: defaultTo(column.default, ""),
+ default: defaultTo(column.default, undefined),
characterSet: "",
collation: "",
};
diff --git a/plugin/parser/edit/mysql/deparse.go b/plugin/parser/edit/mysql/deparse.go
index 7e5cfa221b..58019085b8 100644
--- a/plugin/parser/edit/mysql/deparse.go
+++ b/plugin/parser/edit/mysql/deparse.go
@@ -233,10 +233,10 @@ func transformAddColumnContext(addColumnContext *api.AddColumnContext) (*tidbast
StrValue: addColumnContext.Collation,
})
}
- if addColumnContext.HasDefault {
+ if addColumnContext.Default != nil {
columnOptionList = append(columnOptionList, &tidbast.ColumnOption{
Tp: tidbast.ColumnOptionDefaultValue,
- Expr: tidbast.NewValueExpr(interface{}(addColumnContext.Default), addColumnContext.CharacterSet, addColumnContext.Collation),
+ Expr: tidbast.NewValueExpr(interface{}(*addColumnContext.Default), addColumnContext.CharacterSet, addColumnContext.Collation),
})
}
if !addColumnContext.Nullable {
@@ -281,10 +281,10 @@ func transformChangeColumnContext(changeColumnContext *api.ChangeColumnContext)
Tp: tidbast.ColumnOptionNotNull,
})
}
- if changeColumnContext.HasDefault {
+ if changeColumnContext.Default != nil {
columnOptionList = append(columnOptionList, &tidbast.ColumnOption{
Tp: tidbast.ColumnOptionDefaultValue,
- Expr: tidbast.NewValueExpr(interface{}(changeColumnContext.Default), changeColumnContext.CharacterSet, changeColumnContext.Collation),
+ Expr: tidbast.NewValueExpr(interface{}(*changeColumnContext.Default), changeColumnContext.CharacterSet, changeColumnContext.Collation),
})
}
columnDef.Options = columnOptionList
diff --git a/plugin/parser/edit/mysql/deparse_test.go b/plugin/parser/edit/mysql/deparse_test.go
index 0cd34b1adb..d8041204e3 100644
--- a/plugin/parser/edit/mysql/deparse_test.go
+++ b/plugin/parser/edit/mysql/deparse_test.go
@@ -96,12 +96,11 @@ func TestDeparseCreateTable(t *testing.T) {
Engine: "InnoDB",
AddColumnList: []*api.AddColumnContext{
{
- Name: "id",
- Type: "int",
- Comment: "ID",
- HasDefault: true,
- Default: defaultValue,
- Nullable: true,
+ Name: "id",
+ Type: "int",
+ Comment: "ID",
+ Default: &defaultValue,
+ Nullable: true,
},
{
Name: "name",
diff --git a/plugin/parser/edit/mysql/validate.go b/plugin/parser/edit/mysql/validate.go
index 7c49532a5d..b5f7af9ba9 100644
--- a/plugin/parser/edit/mysql/validate.go
+++ b/plugin/parser/edit/mysql/validate.go
@@ -30,7 +30,7 @@ func (*SchemaEditor) ValidateDatabaseEdit(databaseEdit *api.DatabaseEdit) ([]*ap
Message: fmt.Sprintf("invalid column type `%s`", addColumnContext.Type),
})
}
- if addColumnContext.HasDefault {
+ if addColumnContext.Default != nil {
// TEXT will be regarded as mysql.TypeBlob in the TiDB parser.
if columnType.GetType() == mysql.TypeBlob || columnType.GetType() == mysql.TypeGeometry || columnType.GetType() == mysql.TypeJSON {
validateResultList = append(validateResultList, &api.ValidateResult{
@@ -49,7 +49,7 @@ func (*SchemaEditor) ValidateDatabaseEdit(databaseEdit *api.DatabaseEdit) ([]*ap
Message: fmt.Sprintf("invalid column type `%s`", changeColumnContext.Type),
})
}
- if changeColumnContext.HasDefault {
+ if changeColumnContext.Default != nil {
if columnType.GetType() == mysql.TypeBlob || columnType.GetType() == mysql.TypeGeometry || columnType.GetType() == mysql.TypeJSON {
validateResultList = append(validateResultList, &api.ValidateResult{
Type: api.ValidateErrorResult,
diff --git a/plugin/parser/edit/mysql/validate_test.go b/plugin/parser/edit/mysql/validate_test.go
index d3e5bd18a8..6d74863867 100644
--- a/plugin/parser/edit/mysql/validate_test.go
+++ b/plugin/parser/edit/mysql/validate_test.go
@@ -79,9 +79,8 @@ func TestValidateDatabaseEditColumnTypeAndDefault(t *testing.T) {
Type: "BASE TABLE",
AddColumnList: []*api.AddColumnContext{
{
- Name: "id",
- Type: "TEXT",
- HasDefault: false,
+ Name: "id",
+ Type: "TEXT",
},
},
},
@@ -98,10 +97,9 @@ func TestValidateDatabaseEditColumnTypeAndDefault(t *testing.T) {
Type: "BASE TABLE",
AddColumnList: []*api.AddColumnContext{
{
- Name: "id",
- Type: "TEXT",
- HasDefault: true,
- Default: defaultValue,
+ Name: "id",
+ Type: "TEXT",
+ Default: &defaultValue,
},
},
},